MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / test_subscribe_fetch_wait

Function test_subscribe_fetch_wait

src/environmentd/tests/sql.rs:1036–1095  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1034#[mz_ore::test]
1035#[allow(clippy::disallowed_methods)]
1036fn test_subscribe_fetch_wait() {
1037 let server = test_util::TestHarness::default().start_blocking();
1038 let mut client = server.connect(postgres::NoTls).unwrap();
1039
1040 client.batch_execute("CREATE TABLE t (i INT8)").unwrap();
1041 client
1042 .batch_execute("INSERT INTO t VALUES (1), (2), (3)")
1043 .unwrap();
1044 client
1045 .batch_execute(
1046 "BEGIN;
1047 DECLARE c CURSOR FOR SUBSCRIBE t;",
1048 )
1049 .unwrap();
1050
1051 let expected: Vec<i64> = vec![1, 2, 3];
1052 let mut expected_iter = expected.iter();
1053 let mut next = expected_iter.next();
1054
1055 while let Some(expect) = next {
1056 // FETCH with no timeout will wait for at least 1 result.
1057 let rows = client.query("FETCH c", &[]).unwrap();
1058 assert_eq!(rows.len(), 1);
1059 assert_eq!(rows[0].get::<_, i64>(2), *expect);
1060 next = expected_iter.next();
1061 }
1062
1063 // Try again with FETCH ALL. ALL only guarantees that all available rows will
1064 // be returned, but it's up to the system to decide what is available. This
1065 // means that we could still get only one row per request, and we won't know
1066 // how many rows will come back otherwise.
1067 client
1068 .batch_execute(
1069 "COMMIT; BEGIN;
1070 DECLARE c CURSOR FOR SUBSCRIBE t;",
1071 )
1072 .unwrap();
1073 let mut expected_iter = expected.iter().peekable();
1074 while expected_iter.peek().is_some() {
1075 let rows = client.query("FETCH ALL c", &[]).unwrap();
1076 assert!(rows.len() > 0);
1077 for row in rows {
1078 let next = expected_iter.next().unwrap();
1079 assert_eq!(*next, row.get::<_, i64>(2));
1080 }
1081 }
1082
1083 // Verify that the wait only happens for SUBSCRIBE. A SELECT with 0 rows should not
1084 // block.
1085 client.batch_execute("COMMIT").unwrap();
1086 client.batch_execute("CREATE TABLE empty ()").unwrap();
1087 client
1088 .batch_execute(
1089 "BEGIN;
1090 DECLARE c CURSOR FOR SELECT * FROM empty;",
1091 )
1092 .unwrap();
1093 let rows = client.query("FETCH c", &[]).unwrap();

Callers

nothing calls this directly

Calls 9

start_blockingMethod · 0.80
unwrapMethod · 0.80
batch_executeMethod · 0.80
is_someMethod · 0.80
connectMethod · 0.45
iterMethod · 0.45
nextMethod · 0.45
queryMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected