diff --git a/tools/curate-etl/src/cli.rs b/tools/curate-etl/src/cli.rs index c94e5047..9bed0782 100644 --- a/tools/curate-etl/src/cli.rs +++ b/tools/curate-etl/src/cli.rs @@ -5073,7 +5073,17 @@ mod tests { ); // Unsupported non-empty prefixes must list all three allowed values. - for bad_prefix in ["bars_1d", "../x", "BARS_1M", "fx_1d", "fx_1m_extra"] { + for bad_prefix in [ + "bars_1d", + "../x", + "BARS_1M", + "fx_1d", + "fx_1m_extra", + " bars_1m", + "bars_1m ", + "./bars_1m", + "dir/fx_1m", + ] { let err = parse_args(&to_args(&[ "send-ilp", "--dir", @@ -5090,4 +5100,84 @@ mod tests { ); } } + + #[test] + fn parse_send_ilp_edge_cases_bare_empty_space_form_order_equals_in_dir_and_ipv6() { + // Bare `send-ilp` with no flags fails with missing required flag. + let err = parse_args(&to_args(&["send-ilp"])).unwrap_err(); + assert!(err.contains("missing required flag '--dir'"), "{err}"); + + // Empty values in space form. + for (argv, flag) in [ + ( + &[ + "send-ilp", + "--dir", + "", + "--prefix", + "fx_1m", + "--addr", + "127.0.0.1:9009", + ][..], + "--dir", + ), + ( + &[ + "send-ilp", + "--dir", + "/tmp/shards", + "--prefix", + "", + "--addr", + "127.0.0.1:9009", + ][..], + "--prefix", + ), + ( + &[ + "send-ilp", + "--dir", + "/tmp/shards", + "--prefix", + "fx_1m", + "--addr", + "", + ][..], + "--addr", + ), + ] { + let err = parse_args(&to_args(argv)).unwrap_err(); + assert!( + err.contains(&format!("empty value for flag '{flag}'")), + "expected empty value error for {flag}, got: {err}" + ); + } + + // Mixed flag order, `=` inside --dir path, and IPv6 --addr in both forms. + let expected = CliAction::SendIlp(SendIlpArgs { + dir: PathBuf::from("/tmp/year=2025/shards"), + prefix: "bars_1m_qm".to_string(), + addr: "[::1]:9009".to_string(), + }); + let parsed_mixed = parse_args(&to_args(&[ + "send-ilp", + "--addr", + "[::1]:9009", + "--prefix=bars_1m_qm", + "--dir=/tmp/year=2025/shards", + ])) + .expect("mixed order with equals in dir and IPv6 addr"); + assert_eq!(parsed_mixed, expected); + + let parsed_space = parse_args(&to_args(&[ + "send-ilp", + "--prefix", + "bars_1m_qm", + "--addr=[::1]:9009", + "--dir", + "/tmp/year=2025/shards", + ])) + .expect("space dir with equals and IPv6 equals addr"); + assert_eq!(parsed_space, expected); + } } diff --git a/tools/curate-etl/src/ilp_send_tests.rs b/tools/curate-etl/src/ilp_send_tests.rs index c9351180..863c994f 100644 --- a/tools/curate-etl/src/ilp_send_tests.rs +++ b/tools/curate-etl/src/ilp_send_tests.rs @@ -1543,3 +1543,129 @@ fn run_send_ilp_refused_connection_returns_connect_error_and_zero_partial_report other => panic!("expected ShardIo(Connect), got {other:?}"), } } + +#[test] +fn run_send_ilp_mixed_folder_isolates_bars_1m_bars_1m_qm_and_fx_1m() { + let dir = TempDirGuard::new("run-send-ilp-mixed-folder"); + let bars0 = b"bars_1m,listing_id=1 close=10.0 1700000000000000t\n".to_vec(); + let bars1 = b"bars_1m,listing_id=2 close=20.0 1700000060000000t\n".to_vec(); + let qm0 = b"bars_1m,listing_id=1,source=qm_raw close=10.5 1700000000000000t\n".to_vec(); + let fx0 = b"fx_1m,pair=USDCAD close=1.38 1700000000000000t\n".to_vec(); + + write_bytes(&dir.path().join("bars_1m.shard-00.ilp"), &bars0); + write_bytes(&dir.path().join("bars_1m.shard-01.ilp"), &bars1); + write_bytes(&dir.path().join("bars_1m_qm.shard-00.ilp"), &qm0); + write_bytes(&dir.path().join("fx_1m.shard-00.ilp"), &fx0); + write_bytes(&dir.path().join("notes.txt"), b"unrelated"); + + let cases: [(&str, Vec>); 3] = [ + ("bars_1m", vec![bars0.clone(), bars1.clone()]), + ("bars_1m_qm", vec![qm0.clone()]), + ("fx_1m", vec![fx0.clone()]), + ]; + + for (prefix, mut expected) in cases { + let count = expected.len(); + let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback listener"); + let listen_addr = listener.local_addr().expect("local addr"); + listener + .set_nonblocking(true) + .expect("set loopback listener nonblocking"); + + let server_handle = thread::spawn(move || { + let deadline = Instant::now() + Duration::from_secs(3); + let mut readers = Vec::with_capacity(count); + for accepted in 0..count { + let (mut stream, _) = accept_with_deadline(&listener, deadline, accepted, count); + stream + .set_read_timeout(Some(Duration::from_secs(3))) + .expect("set server read timeout"); + readers.push(thread::spawn(move || { + let mut buf = Vec::new(); + stream.read_to_end(&mut buf).expect("read shard to EOF"); + buf + })); + } + let received: Vec> = readers + .into_iter() + .map(|h| h.join().expect("join reader")) + .collect(); + (listener, received) + }); + + let report = run_send_ilp(&SendIlpArgs { + dir: dir.path().to_path_buf(), + prefix: prefix.to_string(), + addr: listen_addr.to_string(), + }) + .expect("run_send_ilp should send only matching prefix"); + + let (listener, mut received) = server_handle.join().expect("join server"); + match listener.accept() { + Err(err) if err.kind() == io::ErrorKind::WouldBlock => {} + Ok((_, peer)) => panic!("{prefix}: unexpected extra connection from {peer}"), + Err(err) => panic!("{prefix}: unexpected accept error: {err}"), + } + + received.sort(); + expected.sort(); + assert_eq!(received, expected, "payload mismatch for prefix {prefix}"); + assert_eq!(report.total_shards, count); + let expected_bytes: u64 = expected.iter().map(|b| b.len() as u64).sum(); + assert_eq!(report.total_bytes_sent, expected_bytes); + } +} + +#[test] +fn run_send_ilp_gap_precedes_malformed_addr_and_file_as_dir_rejected_with_zero_connections() { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback listener"); + let listen_addr = listener.local_addr().expect("local addr"); + listener + .set_nonblocking(true) + .expect("set listener nonblocking"); + + let assert_zero_accepts = |label: &str| match listener.accept() { + Err(err) if err.kind() == io::ErrorKind::WouldBlock => {} + Ok((_, peer)) => panic!("{label}: unexpectedly accepted connection from {peer}"), + Err(err) => panic!("{label}: unexpected accept error: {err}"), + }; + + // Shard gap paired with malformed address -> MissingShardIndex precedes InvalidAddress. + let gap_dir = TempDirGuard::new("run-send-ilp-gap-precedence"); + write_bytes(&gap_dir.path().join("bars_1m.shard-00.ilp"), b"a"); + write_bytes(&gap_dir.path().join("bars_1m.shard-02.ilp"), b"c"); + let err = run_send_ilp(&SendIlpArgs { + dir: gap_dir.path().to_path_buf(), + prefix: "bars_1m".to_string(), + addr: "not-an-addr".to_string(), + }) + .unwrap_err(); + assert!( + matches!( + err, + IlpSendError::MissingShardIndex { + expected: 1, + found: 2, + .. + } + ), + "expected MissingShardIndex(1, 2) before InvalidAddress, got {err:?}" + ); + assert_zero_accepts("shard gap with malformed addr"); + + // Regular file passed as --dir -> InvalidDirectory, zero connections. + let file_dir = TempDirGuard::new("run-send-ilp-file-as-dir"); + let file_path = file_dir.path().join("not_a_dir.ilp"); + write_bytes(&file_path, b"not a directory"); + let err = run_send_ilp(&SendIlpArgs { + dir: file_path, + prefix: "bars_1m".to_string(), + addr: listen_addr.to_string(), + }) + .unwrap_err(); + assert!( + matches!(err, IlpSendError::InvalidDirectory { .. }), + "expected InvalidDirectory, got {err:?}" + ); + assert_zero_accepts("file passed as dir"); +} diff --git a/tools/curate-etl/tests/cli.rs b/tools/curate-etl/tests/cli.rs index 0fd7677c..bf6ffa7e 100644 --- a/tools/curate-etl/tests/cli.rs +++ b/tools/curate-etl/tests/cli.rs @@ -1872,3 +1872,65 @@ fn send_ilp_cli_help_missing_flag_success_and_partial_error_routing() { ); assert!(!fail_err.contains("Usage:"), "{fail_err}"); } + +#[test] +fn send_ilp_cli_shard_gap_exits_without_partial_report_or_connect_and_bad_prefix_prints_usage() { + use std::io; + use std::net::TcpListener; + + // 1. Shard gap -> exits 1, empty stdout, error on stderr, no partial report, zero connections. + let gap_dir = CliTempDir::new("send-ilp-bin-gap"); + write_file(&gap_dir.path.join("bars_1m.shard-00.ilp"), b"a"); + write_file(&gap_dir.path.join("bars_1m.shard-02.ilp"), b"c"); + + let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback listener"); + let listen_addr = listener.local_addr().expect("local addr"); + listener + .set_nonblocking(true) + .expect("set listener nonblocking"); + + let gap_out = run(&[ + "send-ilp", + "--dir", + gap_dir.path.to_str().unwrap(), + "--prefix", + "bars_1m", + "--addr", + &listen_addr.to_string(), + ]); + assert!(!gap_out.status.success()); + assert_eq!(gap_out.status.code(), Some(1)); + assert!(stdout(&gap_out).is_empty()); + let gap_err = stderr(&gap_out); + assert!(gap_err.contains("error:"), "{gap_err}"); + assert!(!gap_err.contains("curate-etl ilp-send"), "{gap_err}"); + assert!(!gap_err.contains("Usage:"), "{gap_err}"); + match listener.accept() { + Err(err) if err.kind() == io::ErrorKind::WouldBlock => {} + Ok((_, peer)) => panic!("shard gap unexpectedly connected from {peer}"), + Err(err) => panic!("unexpected accept error: {err}"), + } + + // 2. Bad prefix -> exits 1, empty stdout, error + Usage on stderr. + let bad_prefix_out = run(&[ + "send-ilp", + "--dir", + gap_dir.path.to_str().unwrap(), + "--prefix", + "bars_1d", + "--addr", + &listen_addr.to_string(), + ]); + assert!(!bad_prefix_out.status.success()); + assert_eq!(bad_prefix_out.status.code(), Some(1)); + assert!(stdout(&bad_prefix_out).is_empty()); + let bad_prefix_err = stderr(&bad_prefix_out); + assert!(bad_prefix_err.contains("error:"), "{bad_prefix_err}"); + assert!( + bad_prefix_err.contains("bars_1m") + && bad_prefix_err.contains("bars_1m_qm") + && bad_prefix_err.contains("fx_1m"), + "{bad_prefix_err}" + ); + assert!(bad_prefix_err.contains("Usage:"), "{bad_prefix_err}"); +}