|
| 1 | +//! Integration tests that exercise the full input -> crosspoint -> output |
| 2 | +//! relay path over *real* SRT connections (real handshake, real protocol |
| 3 | +//! traffic via srt-tokio acting as the test's "encoder"/"decoder" clients) |
| 4 | +//! rather than just asserting a UDP socket is bound. This is the strongest |
| 5 | +//! local verification available without real third-party SRT hardware. |
| 6 | +
|
| 7 | +use std::time::{Duration, Instant}; |
| 8 | + |
| 9 | +use bytes::Bytes; |
| 10 | +use crosspoint_core::Crosspoint; |
| 11 | +use futures::SinkExt; |
| 12 | +use srt_io::{spawn_input, spawn_output, Endpoint}; |
| 13 | +use srt_tokio::SrtSocket; |
| 14 | +use tokio::time::{sleep, timeout}; |
| 15 | +use tokio_stream::StreamExt; |
| 16 | + |
| 17 | +const RECV_TIMEOUT: Duration = Duration::from_secs(5); |
| 18 | +/// Buffer for connection setup / task scheduling on localhost before we |
| 19 | +/// start relying on ordering between "peer connected" and "relay task |
| 20 | +/// subscribed to its source" — see the module doc on why this matters. |
| 21 | +const SETTLE: Duration = Duration::from_millis(400); |
| 22 | + |
| 23 | +async fn call(addr: &str) -> SrtSocket { |
| 24 | + SrtSocket::builder() |
| 25 | + .call(addr, None) |
| 26 | + .await |
| 27 | + .unwrap_or_else(|e| panic!("failed to connect to {addr}: {e}")) |
| 28 | +} |
| 29 | + |
| 30 | +#[tokio::test] |
| 31 | +async fn relays_bytes_end_to_end_over_real_srt() { |
| 32 | + let crosspoint = Crosspoint::new(); |
| 33 | + |
| 34 | + spawn_input( |
| 35 | + "test-source".into(), |
| 36 | + Endpoint::Listener { |
| 37 | + bind: "127.0.0.1:18601".into(), |
| 38 | + }, |
| 39 | + crosspoint.clone(), |
| 40 | + ); |
| 41 | + spawn_output( |
| 42 | + "test-output".into(), |
| 43 | + Endpoint::Listener { |
| 44 | + bind: "127.0.0.1:18602".into(), |
| 45 | + }, |
| 46 | + "test-source".into(), |
| 47 | + crosspoint.clone(), |
| 48 | + ); |
| 49 | + sleep(SETTLE).await; |
| 50 | + |
| 51 | + // Real SRT clients standing in for an encoder and a decoder, exactly as |
| 52 | + // a real one would connect to this router. |
| 53 | + let mut encoder = call("127.0.0.1:18601").await; |
| 54 | + let mut decoder = call("127.0.0.1:18602").await; |
| 55 | + sleep(SETTLE).await; |
| 56 | + |
| 57 | + let payload = Bytes::from_static(b"hello from a real srt sender"); |
| 58 | + encoder |
| 59 | + .send((Instant::now(), payload.clone())) |
| 60 | + .await |
| 61 | + .expect("encoder send"); |
| 62 | + |
| 63 | + let (_t, received) = timeout(RECV_TIMEOUT, decoder.try_next()) |
| 64 | + .await |
| 65 | + .expect("timed out waiting for the relayed payload") |
| 66 | + .expect("decoder read error") |
| 67 | + .expect("decoder stream ended before receiving anything"); |
| 68 | + |
| 69 | + assert_eq!(received, payload); |
| 70 | +} |
| 71 | + |
| 72 | +#[tokio::test] |
| 73 | +async fn output_switches_source_live_over_real_srt() { |
| 74 | + let crosspoint = Crosspoint::new(); |
| 75 | + |
| 76 | + spawn_input( |
| 77 | + "source-a".into(), |
| 78 | + Endpoint::Listener { |
| 79 | + bind: "127.0.0.1:18611".into(), |
| 80 | + }, |
| 81 | + crosspoint.clone(), |
| 82 | + ); |
| 83 | + spawn_input( |
| 84 | + "source-b".into(), |
| 85 | + Endpoint::Listener { |
| 86 | + bind: "127.0.0.1:18612".into(), |
| 87 | + }, |
| 88 | + crosspoint.clone(), |
| 89 | + ); |
| 90 | + spawn_output( |
| 91 | + "test-output".into(), |
| 92 | + Endpoint::Listener { |
| 93 | + bind: "127.0.0.1:18613".into(), |
| 94 | + }, |
| 95 | + "source-a".into(), |
| 96 | + crosspoint.clone(), |
| 97 | + ); |
| 98 | + sleep(SETTLE).await; |
| 99 | + |
| 100 | + let mut encoder_a = call("127.0.0.1:18611").await; |
| 101 | + let mut encoder_b = call("127.0.0.1:18612").await; |
| 102 | + let mut decoder = call("127.0.0.1:18613").await; |
| 103 | + sleep(SETTLE).await; |
| 104 | + |
| 105 | + // Routed to source-a by default: a payload from A arrives, one from B |
| 106 | + // (sent but never selected) does not. |
| 107 | + encoder_a |
| 108 | + .send((Instant::now(), Bytes::from_static(b"from-a-1"))) |
| 109 | + .await |
| 110 | + .unwrap(); |
| 111 | + encoder_b |
| 112 | + .send((Instant::now(), Bytes::from_static(b"from-b-ignored"))) |
| 113 | + .await |
| 114 | + .unwrap(); |
| 115 | + |
| 116 | + let (_t, first) = timeout(RECV_TIMEOUT, decoder.try_next()) |
| 117 | + .await |
| 118 | + .expect("timed out waiting for first payload") |
| 119 | + .expect("decoder read error") |
| 120 | + .expect("decoder stream ended early"); |
| 121 | + assert_eq!(first, Bytes::from_static(b"from-a-1")); |
| 122 | + |
| 123 | + // Live re-route to source-b, same output connection, no reconnect. |
| 124 | + assert!(crosspoint.route("test-output", "source-b")); |
| 125 | + sleep(SETTLE).await; |
| 126 | + |
| 127 | + encoder_b |
| 128 | + .send((Instant::now(), Bytes::from_static(b"from-b-1"))) |
| 129 | + .await |
| 130 | + .unwrap(); |
| 131 | + |
| 132 | + let (_t, second) = timeout(RECV_TIMEOUT, decoder.try_next()) |
| 133 | + .await |
| 134 | + .expect("timed out waiting for second payload") |
| 135 | + .expect("decoder read error") |
| 136 | + .expect("decoder stream ended early"); |
| 137 | + assert_eq!(second, Bytes::from_static(b"from-b-1")); |
| 138 | +} |
0 commit comments