MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / port_forward_echo

Function port_forward_echo

e2e/rust/tests/port_forward.rs:54–115  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

52/// data through it, and verify the echoed response.
53#[tokio::test]
54async fn port_forward_echo() {
55 let port = find_free_port();
56 let script = echo_server_script(port);
57
58 // ---------------------------------------------------------------
59 // Step 1 — Create a sandbox with the echo server running.
60 // ---------------------------------------------------------------
61 let mut guard =
62 SandboxGuard::create_keep(&["python3", "-c", &script], "echo-server-ready")
63 .await
64 .expect("sandbox create with echo server");
65
66 // ---------------------------------------------------------------
67 // Step 2 — Start port forwarding in the background.
68 // ---------------------------------------------------------------
69 let mut forward_child = guard
70 .spawn_forward(port)
71 .expect("spawn port forward");
72
73 // Wait for the local port to accept connections.
74 wait_for_port("127.0.0.1", port, Duration::from_secs(30))
75 .await
76 .expect("local port should open for forwarding");
77
78 // Give the SSH tunnel a moment to fully establish the direct-tcpip channel.
79 tokio::time::sleep(Duration::from_secs(2)).await;
80
81 // ---------------------------------------------------------------
82 // Step 3 — Send data through the forwarded port and verify response.
83 // ---------------------------------------------------------------
84 let expected = "echo:hello-nav";
85 let mut last_response = String::new();
86
87 for attempt in 1..=5 {
88 match try_echo(port).await {
89 Ok(resp) if resp.starts_with(expected) => {
90 last_response = resp;
91 break;
92 }
93 Ok(resp) => {
94 last_response = resp;
95 eprintln!("attempt {attempt}: unexpected response '{last_response}', retrying...");
96 }
97 Err(e) => {
98 eprintln!("attempt {attempt}: connection error: {e}, retrying...");
99 }
100 }
101 tokio::time::sleep(Duration::from_secs(2)).await;
102 }
103
104 assert!(
105 last_response.starts_with(expected),
106 "expected response starting with '{expected}', got '{last_response}'"
107 );
108
109 // ---------------------------------------------------------------
110 // Cleanup — kill forward process, then sandbox guard handles the rest.
111 // ---------------------------------------------------------------

Callers

nothing calls this directly

Calls 8

find_free_portFunction · 0.85
echo_server_scriptFunction · 0.85
wait_for_portFunction · 0.85
try_echoFunction · 0.85
spawn_forwardMethod · 0.80
killMethod · 0.80
waitMethod · 0.80
cleanupMethod · 0.80

Tested by

no test coverage detected