1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#[cfg(test)]
pub mod test_server {
use crate::websocket_rpc::{RpcNotification, RpcRequest, RpcResponse};
use futures_util::{SinkExt, StreamExt};
use serde_json::json;
use tokio::net::TcpListener;
use tokio::time::{Duration, sleep, timeout};
use tokio_tungstenite::{accept_async, tungstenite::Message};
pub struct TestWebSocketServer {
pub port: u16,
pub url: String,
shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>,
server_handle: Option<tokio::task::JoinHandle<()>>,
}
impl TestWebSocketServer {
/// Start a test WebSocket server that simulates a script actor runtime
pub async fn start() -> Self {
// Bind to a random available port
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let port = addr.port();
let url = format!("ws://127.0.0.1:{}", port);
let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel();
// Start the server in a background task - don't await it!
let server_handle = tokio::spawn(async move {
loop {
// Use try_recv to check for shutdown without blocking
if shutdown_rx.try_recv().is_ok() {
break;
}
// Accept with a short timeout so we can check for shutdown periodically
match timeout(Duration::from_millis(100), listener.accept()).await {
Ok(Ok((stream, _))) => {
// Handle each connection in its own task
tokio::spawn(async move {
if let Ok(ws_stream) = accept_async(stream).await {
let (mut ws_sender, mut ws_receiver) = ws_stream.split();
while let Some(msg_result) = ws_receiver.next().await {
match msg_result {
Ok(Message::Text(text)) => {
println!("Test server received message: {}", text);
if let Ok(request) =
serde_json::from_str::<RpcRequest>(&text)
{
println!(
"Parsed request with method: {}",
request.method
);
TestWebSocketServer::handle_request(
&mut ws_sender,
request,
)
.await;
} else {
println!("Failed to parse request");
}
}
Ok(Message::Close(_)) => break,
_ => {}
}
}
}
});
}
Ok(Err(_)) => {
// Accept failed, continue
}
Err(_) => {
// Timeout, check for shutdown and continue
}
}
}
});
// Small delay to ensure the server is listening
tokio::time::sleep(Duration::from_millis(10)).await;
TestWebSocketServer {
port,
url,
shutdown_tx: Some(shutdown_tx),
server_handle: Some(server_handle),
}
}
async fn handle_request(
ws_sender: &mut futures_util::stream::SplitSink<
tokio_tungstenite::WebSocketStream<tokio::net::TcpStream>,
Message,
>,
request: RpcRequest,
) {
println!(
"Handling request with method: {} and id: {}",
request.method, request.id
);
match request.method.as_str() {
"process" => {
println!("Processing 'process' request");
// Send response with plain JSON values matching Message::from(Value)
let response = RpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id.clone(),
result: Some(json!({
"outputs": {
"output": "processed"
}
})),
error: None,
};
let response_text = serde_json::to_string(&response).unwrap();
println!("Sending response: {}", response_text);
let send_result = ws_sender.send(Message::text(response_text)).await;
println!("Response send result: {:?}", send_result);
// Send async output notification after a delay
sleep(Duration::from_millis(10)).await;
let notification = RpcNotification {
jsonrpc: "2.0".to_string(),
method: "output".to_string(),
params: json!({
"actor_id": "test_actor",
"port": "async_output",
"data": "async data",
"timestamp": 123456789
}),
};
let notification_text = serde_json::to_string(¬ification).unwrap();
let _ = ws_sender.send(Message::text(notification_text)).await;
}
"stream" => {
// Send initial response
let response = RpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id.clone(),
result: Some(json!({
"status": "streaming"
})),
error: None,
};
let response_text = serde_json::to_string(&response).unwrap();
println!("Sending response: {}", response_text);
let send_result = ws_sender.send(Message::text(response_text)).await;
println!("Response send result: {:?}", send_result);
// Send multiple streaming output notifications
for i in 0..3 {
sleep(Duration::from_millis(10)).await;
let notification = RpcNotification {
jsonrpc: "2.0".to_string(),
method: "output".to_string(),
params: json!({
"actor_id": "streaming_actor",
"port": "stream",
"data": { "value": i },
"timestamp": chrono::Utc::now().timestamp_millis() as u64
}),
};
let notification_text = serde_json::to_string(¬ification).unwrap();
let _ = ws_sender.send(Message::text(notification_text)).await;
}
}
_ => {
// Unknown method
let response = RpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id,
result: None,
error: Some(crate::websocket_rpc::RpcError {
code: -32601,
message: "Method not found".to_string(),
data: None,
}),
};
let response_text = serde_json::to_string(&response).unwrap();
println!("Sending response: {}", response_text);
let send_result = ws_sender.send(Message::text(response_text)).await;
println!("Response send result: {:?}", send_result);
}
}
}
/// Shutdown the server gracefully
pub async fn shutdown(mut self) {
if let Some(tx) = self.shutdown_tx.take() {
let _ = tx.send(());
}
if let Some(handle) = self.server_handle.take() {
// Wait for server to shutdown with timeout
let _ = timeout(Duration::from_secs(1), handle).await;
}
}
}
impl Drop for TestWebSocketServer {
fn drop(&mut self) {
// Send shutdown signal if not already sent
if let Some(tx) = self.shutdown_tx.take() {
let _ = tx.send(());
}
}
}
}