()
| 937 | #[mz_ore::test] |
| 938 | #[allow(clippy::disallowed_methods)] |
| 939 | fn test_subscribe_fetch_timeout() { |
| 940 | let server = test_util::TestHarness::default().start_blocking(); |
| 941 | let mut client = server.connect(postgres::NoTls).unwrap(); |
| 942 | |
| 943 | client.batch_execute("CREATE TABLE t (i INT8)").unwrap(); |
| 944 | client |
| 945 | .batch_execute("INSERT INTO t VALUES (1), (2), (3);") |
| 946 | .unwrap(); |
| 947 | client |
| 948 | .batch_execute( |
| 949 | "BEGIN; |
| 950 | DECLARE c CURSOR FOR SUBSCRIBE t;", |
| 951 | ) |
| 952 | .unwrap(); |
| 953 | |
| 954 | let expected: Vec<i64> = vec![1, 2, 3]; |
| 955 | let mut expected_iter = expected.iter(); |
| 956 | let mut next = expected_iter.next(); |
| 957 | |
| 958 | // Test 0s timeouts. |
| 959 | while let Some(expect) = next { |
| 960 | let rows = client.query("FETCH c WITH (TIMEOUT = '0s')", &[]).unwrap(); |
| 961 | // It is fine for there to be no rows ready yet. Immediately try again because |
| 962 | // they should be ready soon. |
| 963 | if rows.len() != 1 { |
| 964 | continue; |
| 965 | } |
| 966 | assert_eq!(rows[0].get::<_, i64>(2), *expect); |
| 967 | |
| 968 | next = expected_iter.next(); |
| 969 | } |
| 970 | |
| 971 | // Test a 1s timeout and make sure we waited for at least that long. |
| 972 | let before = Instant::now(); |
| 973 | let rows = client.query("FETCH c WITH (TIMEOUT = '1s')", &[]).unwrap(); |
| 974 | let duration = before.elapsed(); |
| 975 | assert_eq!(rows.len(), 0); |
| 976 | // Make sure we waited at least 1s but also not too long. |
| 977 | assert!(duration >= Duration::from_secs(1)); |
| 978 | assert!(duration < Duration::from_secs(10)); |
| 979 | |
| 980 | // Make a new cursor. Try to fetch more rows from it than exist. Verify that |
| 981 | // we got all the rows we expect and also waited for at least the timeout |
| 982 | // duration. Cursor may take a moment to be ready, so do it in a loop. |
| 983 | client |
| 984 | .batch_execute( |
| 985 | "COMMIT; BEGIN; |
| 986 | DECLARE c CURSOR FOR SUBSCRIBE t", |
| 987 | ) |
| 988 | .unwrap(); |
| 989 | loop { |
| 990 | let before = Instant::now(); |
| 991 | let rows = client |
| 992 | .query("FETCH 4 c WITH (TIMEOUT = '1s')", &[]) |
| 993 | .unwrap(); |
| 994 | let duration = before.elapsed(); |
| 995 | if rows.len() != 0 { |
| 996 | assert_eq!(rows.len(), expected.len()); |
nothing calls this directly
no test coverage detected