From b3b9697668a57372746bc4e6c951dfe50ba95310 Mon Sep 17 00:00:00 2001 From: Studio Date: Wed, 2 Dec 2026 20:28:59 -0500 Subject: [PATCH] T-188: tests --- tools/curate-etl/src/ilp_send_tests.rs | 261 +++++++++++++++++++++++++ 1 file changed, 261 insertions(+) diff --git a/tools/curate-etl/src/ilp_send_tests.rs b/tools/curate-etl/src/ilp_send_tests.rs index bee7932b..fed8ce23 100644 --- a/tools/curate-etl/src/ilp_send_tests.rs +++ b/tools/curate-etl/src/ilp_send_tests.rs @@ -1039,3 +1039,264 @@ fn orders_report_and_lowest_failure_for_unsorted_input() { other => panic!("expected ShardIo(Connect) on shard 1, got {other:?}"), } } + +// --------------------------------------------------------------------------- +// QA (T-188) edge-case coverage. +// --------------------------------------------------------------------------- + +fn refused_loopback_addr() -> SocketAddr { + // Bind an ephemeral loopback port and immediately release it so connects are refused. + let listener = TcpListener::bind("127.0.0.1:0").expect("bind ephemeral loopback port"); + let addr = listener.local_addr().expect("ephemeral local addr"); + drop(listener); + addr +} + +#[test] +fn discovery_ignores_near_miss_shard_names_and_accepts_hyphen_prefix() { + let dir = TempDirGuard::new("ilp-send-near-miss"); + write_bytes(&dir.path().join("bars_1m.shard-00.ilp"), b"real"); + for near_miss in [ + "bars_1m.shard-.ilp", + "bars_1m.shard-0a.ilp", + "bars_1m.shard-+1.ilp", + "bars_1m.shard--1.ilp", + "bars_1m.shard-01.ilp.tmp", + "bars_1m.shard-01.ILP", + "BARS_1M.shard-01.ilp", + "xbars_1m.shard-01.ilp", + "bars_1m.shard01.ilp", + "bars_1m_qm.shard-01.ilp", + ] { + write_bytes(&dir.path().join(near_miss), b"ignored"); + } + + let found = discover_ilp_shard_files(dir.path(), "bars_1m").expect("discover bars_1m"); + assert_eq!( + found, + vec![IlpShardFile { + shard: 0, + path: dir.path().join("bars_1m.shard-00.ilp"), + }] + ); + + let hyphen = TempDirGuard::new("ilp-send-hyphen-prefix"); + write_bytes(&hyphen.path().join("fx-1m.shard-00.ilp"), b"fx"); + let found = discover_ilp_shard_files(hyphen.path(), "fx-1m").expect("hyphen prefix"); + assert_eq!(found.len(), 1); + assert_eq!(found[0].shard, 0); + + let missing = dir.path().join("does-not-exist"); + match discover_ilp_shard_files(&missing, "bars_1m").unwrap_err() { + IlpSendError::ReadDir { path, source } => { + assert_eq!(path, missing); + assert_eq!(source.kind(), io::ErrorKind::NotFound); + } + other => panic!("expected ReadDir(NotFound), got {other:?}"), + } +} + +#[test] +fn streams_multi_chunk_file_with_one_byte_buffer_and_handles_empty_slice() { + let dir = TempDirGuard::new("ilp-send-one-byte"); + // Deterministic pseudo-random payload covering every byte value, including 0x0A. + let mut state: u32 = 0x1234_5678; + let payload: Vec = (0..4099) + .map(|_| { + state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + (state >> 24) as u8 + }) + .chain(0u8..=255) + .collect(); + let path = dir.path().join("fx_1m.shard-00.ilp"); + write_bytes(&path, &payload); + + let files = discover_ilp_shard_files(dir.path(), "fx_1m").expect("discover"); + let state_cell = Arc::new(Mutex::new(RecordingSinkState { + bytes: Vec::new(), + flushes: 0, + })); + let report = send_ilp_shard_files( + &files, + |_shard| { + Ok(RecordingSink { + state: Arc::clone(&state_cell), + }) + }, + IlpSendOptions { buffer_bytes: 1 }, + ) + .expect("send one-byte buffer"); + let recorded = state_cell.lock().expect("lock recorded"); + assert_eq!(recorded.bytes, payload); + assert_eq!(recorded.flushes, 1); + assert_eq!(report.total_bytes_sent, payload.len() as u64); + + let calls = AtomicUsize::new(0); + let empty = send_ilp_shard_files( + &[], + |_shard| { + calls.fetch_add(1, Ordering::SeqCst); + Ok(Vec::::new()) + }, + IlpSendOptions::default(), + ) + .expect("empty slice is a no-op"); + assert_eq!(calls.load(Ordering::SeqCst), 0); + assert_eq!(empty.total_shards, 0); + assert_eq!(empty.total_bytes_sent, 0); + assert_eq!( + format_ilp_send_report(&empty), + "curate-etl ilp-send\n ilp_send: shards=0 total_bytes_sent=0 per_shard_bytes=none" + ); + assert_eq!(IlpSendOptions::default().buffer_bytes, 64 * 1024); +} + +#[test] +fn shard_io_error_display_and_source_name_shard_path_and_op() { + let dir = TempDirGuard::new("ilp-send-display"); + let p0 = dir.path().join("bars_1m.shard-00.ilp"); + let p1 = dir.path().join("bars_1m.shard-01.ilp"); + write_bytes(&p0, b"abc"); + write_bytes(&p1, b"defg"); + let files = discover_ilp_shard_files(dir.path(), "bars_1m").expect("discover"); + + let err = send_ilp_shard_files( + &files, + |shard| { + if shard == 1 { + Err(io::Error::new(io::ErrorKind::ConnectionRefused, "refused")) + } else { + Ok(Vec::::new()) + } + }, + IlpSendOptions { buffer_bytes: 2 }, + ) + .unwrap_err(); + + let text = err.to_string(); + assert!(text.contains("ILP shard 1"), "{text}"); + assert!(text.contains(&p1.display().to_string()), "{text}"); + assert!(text.contains("during connect"), "{text}"); + assert!(text.contains("total_bytes_sent=3"), "{text}"); + + let source = std::error::Error::source(&err).expect("ShardIo exposes io::Error source"); + let io_source = source + .downcast_ref::() + .expect("source is io::Error"); + assert_eq!(io_source.kind(), io::ErrorKind::ConnectionRefused); + + let partial = err.partial_report().expect("ShardIo carries partial report"); + assert_eq!( + format_ilp_send_report(partial), + "curate-etl ilp-send\n ilp_send: shards=2 total_bytes_sent=3 per_shard_bytes=00=3,01=0" + ); + assert!(IlpSendError::ZeroBufferBytes.partial_report().is_none()); +} + +#[test] +fn tcp_reports_refused_connection_as_connect_failure() { + let dir = TempDirGuard::new("ilp-send-tcp-refused"); + let p0 = dir.path().join("bars_1m.shard-00.ilp"); + write_bytes(&p0, b"bars_1m,listing_id=1 open=1.0 1700000000000000t\n"); + let files = discover_ilp_shard_files(dir.path(), "bars_1m").expect("discover"); + + let dead = refused_loopback_addr(); + let connector = tcp_ilp_connector( + &dead.to_string(), + IlpTcpOptions { + connect_timeout: Duration::from_secs(2), + write_timeout: Duration::from_secs(2), + }, + ) + .expect("connector creation must not connect"); + + match send_ilp_shard_files(&files, connector, IlpSendOptions::default()).unwrap_err() { + IlpSendError::ShardIo { + op, + shard, + path, + source, + partial_report, + } => { + assert_eq!(op, IlpSendOp::Connect); + assert_eq!(shard, 0); + assert_eq!(path, p0); + assert_eq!(source.kind(), io::ErrorKind::ConnectionRefused); + assert_eq!(partial_report.total_bytes_sent, 0); + } + other => panic!("expected ShardIo(Connect), got {other:?}"), + } +} + +#[test] +fn tcp_connector_skips_unreachable_address_then_pins_reachable_one_for_all_shards() { + let dir = TempDirGuard::new("ilp-send-tcp-pin-first-live"); + let payloads: Vec> = (0..3) + .map(|i| format!("bars_1m,listing_id={i} open=1.0 170000000000000{i}t\n").into_bytes()) + .collect(); + for (i, payload) in payloads.iter().enumerate() { + write_bytes(&dir.path().join(format!("bars_1m.shard-{i:02}.ilp")), payload); + } + let files = discover_ilp_shard_files(dir.path(), "bars_1m").expect("discover"); + + let dead = refused_loopback_addr(); + let live = TcpListener::bind("127.0.0.1:0").expect("bind live endpoint"); + let live_addr = live.local_addr().expect("live addr"); + live.set_nonblocking(true).expect("nonblocking live"); + + let server = thread::spawn(move || { + let deadline = Instant::now() + Duration::from_secs(3); + let mut readers = Vec::new(); + for accepted in 0..3 { + let (mut stream, _) = accept_with_deadline(&live, deadline, accepted, 3); + stream + .set_read_timeout(Some(Duration::from_secs(3))) + .expect("read timeout"); + readers.push(thread::spawn(move || { + let mut buf = Vec::new(); + stream.read_to_end(&mut buf).expect("read to EOF"); + buf + })); + } + readers + .into_iter() + .map(|h| h.join().expect("join reader")) + .collect::>() + }); + + let connector = tcp_ilp_connector_for_addrs( + vec![dead, live_addr], + IlpTcpOptions { + connect_timeout: Duration::from_secs(2), + write_timeout: Duration::from_secs(2), + }, + ) + .expect("connector"); + let peers = Mutex::new(Vec::::new()); + let report = send_ilp_shard_files( + &files, + |shard| { + let sink = connector(shard)?; + peers + .lock() + .expect("lock peers") + .push(sink.inner().peer_addr().expect("peer addr")); + Ok(sink) + }, + IlpSendOptions { buffer_bytes: 5 }, + ) + .expect("send via first reachable address"); + + let mut received = server.join().expect("join server"); + received.sort(); + let mut expected = payloads.clone(); + expected.sort(); + assert_eq!(received, expected); + let peers = peers.into_inner().expect("peers"); + assert_eq!(peers.len(), 3); + assert!(peers.iter().all(|p| *p == live_addr), "{peers:?}"); + assert_eq!( + report.total_bytes_sent, + payloads.iter().map(|p| p.len() as u64).sum::() + ); +}