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

Function test_subscribe_basic

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

Source from the content-addressed store, hash-verified

512#[mz_ore::test]
513#[allow(clippy::disallowed_methods)]
514fn test_subscribe_basic() {
515 let server = test_util::TestHarness::default()
516 .unsafe_mode()
517 .start_blocking();
518 let mut client_writes = server.connect(postgres::NoTls).unwrap();
519 let mut client_reads = server.connect(postgres::NoTls).unwrap();
520
521 server.enable_feature_flags(&["enable_index_options", "enable_logical_compaction_window"]);
522
523 // Create a table with disabled compaction.
524 client_writes
525 .batch_execute("CREATE TABLE t (data text) WITH (RETAIN HISTORY FOR '1000 hours')")
526 .unwrap();
527 client_writes.batch_execute("SELECT * FROM t").unwrap();
528 client_reads
529 .batch_execute(
530 "BEGIN;
531 DECLARE c CURSOR FOR SUBSCRIBE t;",
532 )
533 .unwrap();
534 // Locks the timestamp of the SUBSCRIBE to before any of the following INSERTs, which is required
535 // for mz_timestamp column to be accurate
536 let _ = client_reads.query_one("FETCH 0 c", &[]);
537
538 let mut events = vec![];
539
540 for i in 1..=3 {
541 let data = format!("line {}", i);
542 client_writes
543 .execute("INSERT INTO t VALUES ($1)", &[&data])
544 .unwrap();
545 let row = client_reads.query_one("FETCH ALL c", &[]).unwrap();
546 assert_eq!(row.get::<_, i64>("mz_diff"), 1);
547 assert_eq!(row.get::<_, String>("data"), data);
548 events.push((row.get::<_, MzTimestamp>("mz_timestamp").0, data));
549
550 if i > 1 {
551 // write timestamps should all increase
552 assert!(events[i - 1].0 > events[i - 2].0);
553 }
554 }
555
556 // Now subscribe without a snapshot as of each timestamp, verifying that when we do
557 // so we only see events that occur as of or later than that timestamp.
558 for (ts, _) in &events {
559 client_reads
560 .batch_execute(&*format!(
561 "COMMIT; BEGIN;
562 DECLARE c CURSOR FOR SUBSCRIBE t WITH (SNAPSHOT = false) AS OF {}",
563 ts - 1
564 ))
565 .unwrap();
566
567 // Skip by the things we won't be able to see.
568 for (_, expected) in events.iter().skip_while(|(inner_ts, _)| inner_ts < ts) {
569 let actual = client_reads.query_one("FETCH c", &[]).unwrap();
570 assert_eq!(actual.get::<_, String>("data"), *expected);
571 }

Callers

nothing calls this directly

Calls 14

start_blockingMethod · 0.80
unsafe_modeMethod · 0.80
unwrapMethod · 0.80
enable_feature_flagsMethod · 0.80
batch_executeMethod · 0.80
unwrap_db_errorMethod · 0.80
expectMethod · 0.80
connectMethod · 0.45
query_oneMethod · 0.45
executeMethod · 0.45
pushMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected