Skip to content

Commit 2c1ad15

Browse files
committed
Add test for max_backend_message_size
1 parent 4f780bf commit 2c1ad15

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

tokio-postgres/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,14 @@ impl Config {
599599
self.replication_mode(mode);
600600
}
601601
}
602+
"max_backend_message_size" => {
603+
let limit = value.parse::<usize>().map_err(|_| {
604+
Error::config_parse(Box::new(InvalidValue("max_backend_message_size")))
605+
})?;
606+
if limit > 0 {
607+
self.max_backend_message_size(limit);
608+
}
609+
}
602610
key => {
603611
return Err(Error::config_parse(Box::new(UnknownOption(
604612
key.to_string(),

tokio-postgres/src/connect_raw.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,12 @@ where
9090
let stream = connect_tls(stream, config.ssl_mode, tls).await?;
9191

9292
let mut stream = StartupStream {
93-
inner: Framed::new(stream, PostgresCodec {
94-
max_message_size: config.max_backend_message_size,
95-
}),
93+
inner: Framed::new(
94+
stream,
95+
PostgresCodec {
96+
max_message_size: config.max_backend_message_size,
97+
},
98+
),
9699
buf: BackendMessages::empty(),
97100
delayed: VecDeque::new(),
98101
};

tokio-postgres/tests/test/main.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,30 @@ async fn query_raw_txt() {
280280
assert!(rows[0].body_len() > 0);
281281
}
282282

283+
#[tokio::test]
284+
async fn limit_max_backend_message_size() {
285+
let client = connect("user=postgres max_backend_message_size=10000").await;
286+
let small: Vec<tokio_postgres::Row> = client
287+
.query_raw_txt("SELECT REPEAT('a', 20)", [])
288+
.await
289+
.unwrap()
290+
.try_collect()
291+
.await
292+
.unwrap();
293+
294+
assert_eq!(small.len(), 1);
295+
assert_eq!(small[0].as_text(0).unwrap().unwrap().len(), 20);
296+
297+
let large: Result<Vec<tokio_postgres::Row>, Error> = client
298+
.query_raw_txt("SELECT REPEAT('a', 2000000)", [])
299+
.await
300+
.unwrap()
301+
.try_collect()
302+
.await;
303+
304+
assert!(large.is_err());
305+
}
306+
283307
#[tokio::test]
284308
async fn command_tag() {
285309
let client = connect("user=postgres").await;

0 commit comments

Comments
 (0)