starpc 0.41.2 → 0.43.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -20
- package/dist/mock/mock.pb.d.ts +1 -1
- package/dist/mock/mock.pb.js +4 -5
- package/dist/mock/mock_srpc.pb.d.ts +3 -3
- package/dist/mock/mock_srpc.pb.js +5 -5
- package/echo/Cargo.toml +21 -0
- package/echo/build.rs +15 -0
- package/echo/echo.pb.cc +405 -0
- package/echo/echo.pb.go +9 -27
- package/echo/echo.pb.h +364 -0
- package/echo/echo_srpc.pb.go +1 -1
- package/echo/gen/mod.rs +3 -0
- package/echo/main.rs +162 -0
- package/go.mod +18 -10
- package/go.sum +28 -18
- package/mock/mock.pb.cc +394 -0
- package/mock/mock.pb.go +9 -27
- package/mock/mock.pb.h +366 -0
- package/mock/mock.pb.ts +11 -13
- package/mock/mock_srpc.pb.go +1 -1
- package/mock/mock_srpc.pb.ts +12 -9
- package/package.json +27 -25
- package/srpc/Cargo.toml +26 -0
- package/srpc/build.rs +15 -0
- package/srpc/client.rs +356 -0
- package/srpc/codec.rs +225 -0
- package/srpc/error.rs +177 -0
- package/srpc/handler.rs +163 -0
- package/srpc/invoker.rs +192 -0
- package/srpc/lib.rs +107 -0
- package/srpc/message.rs +9 -0
- package/srpc/mux.rs +353 -0
- package/srpc/packet.rs +334 -0
- package/srpc/proto/mod.rs +10 -0
- package/srpc/rpc.rs +777 -0
- package/srpc/rpcproto.pb.cc +1381 -0
- package/srpc/rpcproto.pb.go +75 -183
- package/srpc/rpcproto.pb.h +1451 -0
- package/srpc/server.rs +337 -0
- package/srpc/stream.rs +304 -0
- package/srpc/testing.rs +290 -0
- package/srpc/tests/integration_test.rs +495 -0
- package/srpc/transport.rs +218 -0
- package/Makefile +0 -154
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
//! Integration tests for starpc, mirroring the Go/JS test patterns.
|
|
2
|
+
//!
|
|
3
|
+
//! These tests verify:
|
|
4
|
+
//! 1. Unary RPC
|
|
5
|
+
//! 2. Server streaming (5 messages)
|
|
6
|
+
//! 3. Client streaming
|
|
7
|
+
//! 4. Bidirectional streaming
|
|
8
|
+
//! 5. Error handling
|
|
9
|
+
//! 6. Wire format compatibility
|
|
10
|
+
|
|
11
|
+
use async_trait::async_trait;
|
|
12
|
+
use prost::Message;
|
|
13
|
+
use std::sync::Arc;
|
|
14
|
+
use std::time::Duration;
|
|
15
|
+
|
|
16
|
+
use starpc::error::{Error, Result};
|
|
17
|
+
use starpc::handler::Handler;
|
|
18
|
+
use starpc::invoker::Invoker;
|
|
19
|
+
use starpc::mux::Mux;
|
|
20
|
+
use starpc::server::Server;
|
|
21
|
+
use starpc::stream::{Stream, StreamExt};
|
|
22
|
+
use starpc::testing::{create_test_pair, SingleInMemoryOpener};
|
|
23
|
+
use starpc::Client;
|
|
24
|
+
|
|
25
|
+
// Simple EchoMsg for testing
|
|
26
|
+
#[derive(Clone, PartialEq, Message)]
|
|
27
|
+
struct EchoMsg {
|
|
28
|
+
#[prost(string, tag = "1")]
|
|
29
|
+
body: String,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const BODY_TXT: &str = "hello world via starpc e2e test";
|
|
33
|
+
|
|
34
|
+
/// Echo server implementation matching Go's echo/server.go
|
|
35
|
+
struct EchoServer;
|
|
36
|
+
|
|
37
|
+
#[async_trait]
|
|
38
|
+
impl Invoker for EchoServer {
|
|
39
|
+
async fn invoke_method(
|
|
40
|
+
&self,
|
|
41
|
+
_service_id: &str,
|
|
42
|
+
method_id: &str,
|
|
43
|
+
stream: Box<dyn Stream>,
|
|
44
|
+
) -> (bool, Result<()>) {
|
|
45
|
+
match method_id {
|
|
46
|
+
"Echo" => (true, self.echo(stream).await),
|
|
47
|
+
"EchoServerStream" => (true, self.echo_server_stream(stream).await),
|
|
48
|
+
"EchoClientStream" => (true, self.echo_client_stream(stream).await),
|
|
49
|
+
"EchoBidiStream" => (true, self.echo_bidi_stream(stream).await),
|
|
50
|
+
_ => (false, Err(Error::Unimplemented)),
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
impl Handler for EchoServer {
|
|
56
|
+
fn service_id(&self) -> &'static str {
|
|
57
|
+
"echo.Echoer"
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn method_ids(&self) -> &'static [&'static str] {
|
|
61
|
+
&[
|
|
62
|
+
"Echo",
|
|
63
|
+
"EchoServerStream",
|
|
64
|
+
"EchoClientStream",
|
|
65
|
+
"EchoBidiStream",
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
impl EchoServer {
|
|
71
|
+
/// Unary echo - returns the same message
|
|
72
|
+
async fn echo(&self, stream: Box<dyn Stream>) -> Result<()> {
|
|
73
|
+
let msg: EchoMsg = stream.msg_recv().await?;
|
|
74
|
+
stream.msg_send(&msg).await?;
|
|
75
|
+
Ok(())
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Server streaming - sends 5 copies of the message
|
|
79
|
+
async fn echo_server_stream(&self, stream: Box<dyn Stream>) -> Result<()> {
|
|
80
|
+
let msg: EchoMsg = stream.msg_recv().await?;
|
|
81
|
+
|
|
82
|
+
// Send 5 responses with delays
|
|
83
|
+
for _ in 0..5 {
|
|
84
|
+
if stream.context().is_cancelled() {
|
|
85
|
+
return Err(Error::Cancelled);
|
|
86
|
+
}
|
|
87
|
+
stream.msg_send(&msg).await?;
|
|
88
|
+
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
89
|
+
}
|
|
90
|
+
Ok(())
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// Client streaming - returns first message received
|
|
94
|
+
async fn echo_client_stream(&self, stream: Box<dyn Stream>) -> Result<()> {
|
|
95
|
+
let msg: EchoMsg = stream.msg_recv().await?;
|
|
96
|
+
stream.msg_send(&msg).await?;
|
|
97
|
+
Ok(())
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// Bidirectional streaming - server sends first, then echoes all messages
|
|
101
|
+
async fn echo_bidi_stream(&self, stream: Box<dyn Stream>) -> Result<()> {
|
|
102
|
+
// Server sends initial message
|
|
103
|
+
let initial = EchoMsg {
|
|
104
|
+
body: "hello from server".to_string(),
|
|
105
|
+
};
|
|
106
|
+
stream.msg_send(&initial).await?;
|
|
107
|
+
|
|
108
|
+
// Echo all received messages
|
|
109
|
+
loop {
|
|
110
|
+
match stream.msg_recv::<EchoMsg>().await {
|
|
111
|
+
Ok(msg) => {
|
|
112
|
+
if msg.body.is_empty() {
|
|
113
|
+
return Err(Error::Remote("got message with empty body".to_string()));
|
|
114
|
+
}
|
|
115
|
+
stream.msg_send(&msg).await?;
|
|
116
|
+
}
|
|
117
|
+
Err(Error::StreamClosed) => break,
|
|
118
|
+
Err(e) => return Err(e),
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
Ok(())
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/// Test infrastructure: creates connected client and server
|
|
126
|
+
async fn setup_e2e() -> (starpc::SrpcClient<SingleInMemoryOpener>, tokio::task::JoinHandle<()>) {
|
|
127
|
+
let (opener, server_stream) = create_test_pair();
|
|
128
|
+
|
|
129
|
+
// Set up the server
|
|
130
|
+
let mux = Arc::new(Mux::new());
|
|
131
|
+
mux.register(Arc::new(EchoServer)).unwrap();
|
|
132
|
+
let server = Server::with_arc(mux);
|
|
133
|
+
|
|
134
|
+
// Spawn server handler
|
|
135
|
+
let server_handle = tokio::spawn(async move {
|
|
136
|
+
let _ = server.handle_stream(server_stream).await;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Create client
|
|
140
|
+
let client = starpc::SrpcClient::new(opener);
|
|
141
|
+
|
|
142
|
+
(client, server_handle)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ============================================================================
|
|
146
|
+
// Tests matching Go's server_test.go
|
|
147
|
+
// ============================================================================
|
|
148
|
+
|
|
149
|
+
#[tokio::test]
|
|
150
|
+
async fn test_e2e_unary() {
|
|
151
|
+
let (client, server_handle) = setup_e2e().await;
|
|
152
|
+
|
|
153
|
+
// Make unary call
|
|
154
|
+
let request = EchoMsg {
|
|
155
|
+
body: BODY_TXT.to_string(),
|
|
156
|
+
};
|
|
157
|
+
let response: EchoMsg = client
|
|
158
|
+
.exec_call("echo.Echoer", "Echo", &request)
|
|
159
|
+
.await
|
|
160
|
+
.expect("exec_call failed");
|
|
161
|
+
|
|
162
|
+
assert_eq!(response.body, BODY_TXT);
|
|
163
|
+
|
|
164
|
+
server_handle.abort();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#[tokio::test]
|
|
168
|
+
async fn test_e2e_server_stream() {
|
|
169
|
+
let (client, server_handle) = setup_e2e().await;
|
|
170
|
+
|
|
171
|
+
// Send request and open stream
|
|
172
|
+
let request = EchoMsg {
|
|
173
|
+
body: BODY_TXT.to_string(),
|
|
174
|
+
};
|
|
175
|
+
let data = request.encode_to_vec();
|
|
176
|
+
let stream = client
|
|
177
|
+
.new_stream("echo.Echoer", "EchoServerStream", Some(&data))
|
|
178
|
+
.await
|
|
179
|
+
.expect("new_stream failed");
|
|
180
|
+
|
|
181
|
+
// Close send side
|
|
182
|
+
stream.close_send().await.expect("close_send failed");
|
|
183
|
+
|
|
184
|
+
// Expect to receive 5 messages
|
|
185
|
+
let expected_rx = 5;
|
|
186
|
+
let mut received = 0;
|
|
187
|
+
|
|
188
|
+
loop {
|
|
189
|
+
match stream.msg_recv::<EchoMsg>().await {
|
|
190
|
+
Ok(msg) => {
|
|
191
|
+
assert_eq!(msg.body, BODY_TXT);
|
|
192
|
+
received += 1;
|
|
193
|
+
}
|
|
194
|
+
Err(Error::StreamClosed) => break,
|
|
195
|
+
Err(e) => panic!("unexpected error: {}", e),
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
assert_eq!(
|
|
200
|
+
received, expected_rx,
|
|
201
|
+
"expected {} messages, got {}",
|
|
202
|
+
expected_rx, received
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
server_handle.abort();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#[tokio::test]
|
|
209
|
+
async fn test_e2e_client_stream() {
|
|
210
|
+
let (client, server_handle) = setup_e2e().await;
|
|
211
|
+
|
|
212
|
+
// Open stream without initial message
|
|
213
|
+
let stream = client
|
|
214
|
+
.new_stream("echo.Echoer", "EchoClientStream", None)
|
|
215
|
+
.await
|
|
216
|
+
.expect("new_stream failed");
|
|
217
|
+
|
|
218
|
+
// Send a message
|
|
219
|
+
let request = EchoMsg {
|
|
220
|
+
body: BODY_TXT.to_string(),
|
|
221
|
+
};
|
|
222
|
+
stream.msg_send(&request).await.expect("msg_send failed");
|
|
223
|
+
|
|
224
|
+
// Close send side
|
|
225
|
+
stream.close_send().await.expect("close_send failed");
|
|
226
|
+
|
|
227
|
+
// Receive response
|
|
228
|
+
let response: EchoMsg = stream.msg_recv().await.expect("msg_recv failed");
|
|
229
|
+
assert_eq!(response.body, BODY_TXT);
|
|
230
|
+
|
|
231
|
+
stream.close().await.ok();
|
|
232
|
+
server_handle.abort();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
#[tokio::test]
|
|
236
|
+
async fn test_e2e_bidi_stream() {
|
|
237
|
+
let (client, server_handle) = setup_e2e().await;
|
|
238
|
+
|
|
239
|
+
// Open bidirectional stream
|
|
240
|
+
let stream = client
|
|
241
|
+
.new_stream("echo.Echoer", "EchoBidiStream", None)
|
|
242
|
+
.await
|
|
243
|
+
.expect("new_stream failed");
|
|
244
|
+
|
|
245
|
+
// Receive server's initial message
|
|
246
|
+
let initial: EchoMsg = stream.msg_recv().await.expect("msg_recv failed");
|
|
247
|
+
assert_eq!(initial.body, "hello from server");
|
|
248
|
+
|
|
249
|
+
// Send a message from client
|
|
250
|
+
let client_msg = EchoMsg {
|
|
251
|
+
body: "hello from client".to_string(),
|
|
252
|
+
};
|
|
253
|
+
stream.msg_send(&client_msg).await.expect("msg_send failed");
|
|
254
|
+
|
|
255
|
+
// Receive echoed message
|
|
256
|
+
let echoed: EchoMsg = stream.msg_recv().await.expect("msg_recv failed");
|
|
257
|
+
assert_eq!(echoed.body, "hello from client");
|
|
258
|
+
|
|
259
|
+
// Close the stream
|
|
260
|
+
stream.close().await.expect("close failed");
|
|
261
|
+
server_handle.abort();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[tokio::test]
|
|
265
|
+
async fn test_e2e_multiple_bidi_messages() {
|
|
266
|
+
let (client, server_handle) = setup_e2e().await;
|
|
267
|
+
|
|
268
|
+
let stream = client
|
|
269
|
+
.new_stream("echo.Echoer", "EchoBidiStream", None)
|
|
270
|
+
.await
|
|
271
|
+
.expect("new_stream failed");
|
|
272
|
+
|
|
273
|
+
// Receive server's initial message
|
|
274
|
+
let _: EchoMsg = stream.msg_recv().await.expect("initial recv failed");
|
|
275
|
+
|
|
276
|
+
// Send and receive multiple messages
|
|
277
|
+
for i in 0..10 {
|
|
278
|
+
let msg = EchoMsg {
|
|
279
|
+
body: format!("message {}", i),
|
|
280
|
+
};
|
|
281
|
+
stream.msg_send(&msg).await.expect("msg_send failed");
|
|
282
|
+
|
|
283
|
+
let echoed: EchoMsg = stream.msg_recv().await.expect("msg_recv failed");
|
|
284
|
+
assert_eq!(echoed.body, format!("message {}", i));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
stream.close().await.expect("close failed");
|
|
288
|
+
server_handle.abort();
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
#[tokio::test]
|
|
292
|
+
async fn test_e2e_unary_empty_message() {
|
|
293
|
+
let (client, server_handle) = setup_e2e().await;
|
|
294
|
+
|
|
295
|
+
// Send empty message
|
|
296
|
+
let request = EchoMsg {
|
|
297
|
+
body: String::new(),
|
|
298
|
+
};
|
|
299
|
+
let response: EchoMsg = client
|
|
300
|
+
.exec_call("echo.Echoer", "Echo", &request)
|
|
301
|
+
.await
|
|
302
|
+
.expect("exec_call failed");
|
|
303
|
+
|
|
304
|
+
assert_eq!(response.body, "");
|
|
305
|
+
|
|
306
|
+
server_handle.abort();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
#[tokio::test]
|
|
310
|
+
async fn test_e2e_unimplemented_method() {
|
|
311
|
+
let (client, server_handle) = setup_e2e().await;
|
|
312
|
+
|
|
313
|
+
let request = EchoMsg {
|
|
314
|
+
body: "test".to_string(),
|
|
315
|
+
};
|
|
316
|
+
let result: Result<EchoMsg> = client
|
|
317
|
+
.exec_call("echo.Echoer", "NonExistentMethod", &request)
|
|
318
|
+
.await;
|
|
319
|
+
|
|
320
|
+
assert!(result.is_err());
|
|
321
|
+
|
|
322
|
+
server_handle.abort();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
#[tokio::test]
|
|
326
|
+
async fn test_codec_wire_format() {
|
|
327
|
+
use starpc::codec::PacketCodec;
|
|
328
|
+
use starpc::proto::{packet::Body, CallData, CallStart, Packet};
|
|
329
|
+
use tokio_util::codec::{Decoder, Encoder};
|
|
330
|
+
|
|
331
|
+
let mut codec = PacketCodec::new();
|
|
332
|
+
let mut buf = bytes::BytesMut::new();
|
|
333
|
+
|
|
334
|
+
// Test CallStart encoding
|
|
335
|
+
let call_start = Packet {
|
|
336
|
+
body: Some(Body::CallStart(CallStart {
|
|
337
|
+
rpc_service: "test.Service".into(),
|
|
338
|
+
rpc_method: "TestMethod".into(),
|
|
339
|
+
data: vec![1, 2, 3, 4],
|
|
340
|
+
data_is_zero: false,
|
|
341
|
+
})),
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
codec
|
|
345
|
+
.encode(call_start.clone(), &mut buf)
|
|
346
|
+
.expect("encode failed");
|
|
347
|
+
|
|
348
|
+
// Verify length prefix (little-endian u32)
|
|
349
|
+
let len = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as usize;
|
|
350
|
+
assert_eq!(len, buf.len() - 4);
|
|
351
|
+
|
|
352
|
+
// Decode and verify
|
|
353
|
+
let decoded = codec
|
|
354
|
+
.decode(&mut buf)
|
|
355
|
+
.expect("decode failed")
|
|
356
|
+
.expect("no packet");
|
|
357
|
+
assert_eq!(decoded, call_start);
|
|
358
|
+
|
|
359
|
+
// Test CallData encoding
|
|
360
|
+
buf.clear();
|
|
361
|
+
let call_data = Packet {
|
|
362
|
+
body: Some(Body::CallData(CallData {
|
|
363
|
+
data: vec![5, 6, 7, 8],
|
|
364
|
+
data_is_zero: false,
|
|
365
|
+
complete: true,
|
|
366
|
+
error: String::new(),
|
|
367
|
+
})),
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
codec
|
|
371
|
+
.encode(call_data.clone(), &mut buf)
|
|
372
|
+
.expect("encode failed");
|
|
373
|
+
let decoded = codec
|
|
374
|
+
.decode(&mut buf)
|
|
375
|
+
.expect("decode failed")
|
|
376
|
+
.expect("no packet");
|
|
377
|
+
assert_eq!(decoded, call_data);
|
|
378
|
+
|
|
379
|
+
// Test empty data with data_is_zero flag
|
|
380
|
+
buf.clear();
|
|
381
|
+
let empty_data = Packet {
|
|
382
|
+
body: Some(Body::CallData(CallData {
|
|
383
|
+
data: vec![],
|
|
384
|
+
data_is_zero: true,
|
|
385
|
+
complete: false,
|
|
386
|
+
error: String::new(),
|
|
387
|
+
})),
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
codec
|
|
391
|
+
.encode(empty_data.clone(), &mut buf)
|
|
392
|
+
.expect("encode failed");
|
|
393
|
+
let decoded = codec
|
|
394
|
+
.decode(&mut buf)
|
|
395
|
+
.expect("decode failed")
|
|
396
|
+
.expect("no packet");
|
|
397
|
+
assert_eq!(decoded, empty_data);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#[tokio::test]
|
|
401
|
+
async fn test_packet_validation() {
|
|
402
|
+
use starpc::packet::Validate;
|
|
403
|
+
use starpc::proto::{packet::Body, CallData, CallStart, Packet};
|
|
404
|
+
|
|
405
|
+
// Valid CallStart
|
|
406
|
+
let valid_start = Packet {
|
|
407
|
+
body: Some(Body::CallStart(CallStart {
|
|
408
|
+
rpc_service: "svc".into(),
|
|
409
|
+
rpc_method: "method".into(),
|
|
410
|
+
data: vec![],
|
|
411
|
+
data_is_zero: false,
|
|
412
|
+
})),
|
|
413
|
+
};
|
|
414
|
+
assert!(valid_start.validate().is_ok());
|
|
415
|
+
|
|
416
|
+
// Invalid CallStart - empty method
|
|
417
|
+
let invalid_start = Packet {
|
|
418
|
+
body: Some(Body::CallStart(CallStart {
|
|
419
|
+
rpc_service: "svc".into(),
|
|
420
|
+
rpc_method: String::new(),
|
|
421
|
+
data: vec![],
|
|
422
|
+
data_is_zero: false,
|
|
423
|
+
})),
|
|
424
|
+
};
|
|
425
|
+
assert!(invalid_start.validate().is_err());
|
|
426
|
+
|
|
427
|
+
// Valid CallData with data
|
|
428
|
+
let valid_data = Packet {
|
|
429
|
+
body: Some(Body::CallData(CallData {
|
|
430
|
+
data: vec![1, 2, 3],
|
|
431
|
+
data_is_zero: false,
|
|
432
|
+
complete: false,
|
|
433
|
+
error: String::new(),
|
|
434
|
+
})),
|
|
435
|
+
};
|
|
436
|
+
assert!(valid_data.validate().is_ok());
|
|
437
|
+
|
|
438
|
+
// Invalid CallData - empty everything
|
|
439
|
+
let invalid_data = Packet {
|
|
440
|
+
body: Some(Body::CallData(CallData {
|
|
441
|
+
data: vec![],
|
|
442
|
+
data_is_zero: false,
|
|
443
|
+
complete: false,
|
|
444
|
+
error: String::new(),
|
|
445
|
+
})),
|
|
446
|
+
};
|
|
447
|
+
assert!(invalid_data.validate().is_err());
|
|
448
|
+
|
|
449
|
+
// Empty packet
|
|
450
|
+
let empty_packet = Packet { body: None };
|
|
451
|
+
assert!(empty_packet.validate().is_err());
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
#[tokio::test]
|
|
455
|
+
async fn test_mux_registration_and_lookup() {
|
|
456
|
+
let mux = Mux::new();
|
|
457
|
+
|
|
458
|
+
// Register handler
|
|
459
|
+
mux.register(Arc::new(EchoServer)).unwrap();
|
|
460
|
+
|
|
461
|
+
// Check service exists
|
|
462
|
+
assert!(mux.has_service("echo.Echoer"));
|
|
463
|
+
assert!(!mux.has_service("nonexistent"));
|
|
464
|
+
|
|
465
|
+
// Check methods exist
|
|
466
|
+
assert!(mux.has_service_method("echo.Echoer", "Echo"));
|
|
467
|
+
assert!(mux.has_service_method("echo.Echoer", "EchoServerStream"));
|
|
468
|
+
assert!(!mux.has_service_method("echo.Echoer", "NonExistent"));
|
|
469
|
+
|
|
470
|
+
// Empty strings should return false
|
|
471
|
+
assert!(!mux.has_service(""));
|
|
472
|
+
assert!(!mux.has_service_method("", "Echo"));
|
|
473
|
+
assert!(!mux.has_service_method("echo.Echoer", ""));
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#[tokio::test]
|
|
477
|
+
async fn test_error_types() {
|
|
478
|
+
use starpc::error::codes;
|
|
479
|
+
|
|
480
|
+
// Test error predicates
|
|
481
|
+
assert!(Error::Aborted.is_abort());
|
|
482
|
+
assert!(Error::Cancelled.is_abort());
|
|
483
|
+
assert!(!Error::StreamClosed.is_abort());
|
|
484
|
+
|
|
485
|
+
assert!(Error::StreamClosed.is_closed());
|
|
486
|
+
assert!(Error::Cancelled.is_closed());
|
|
487
|
+
|
|
488
|
+
assert!(Error::StreamIdle.is_timeout());
|
|
489
|
+
|
|
490
|
+
assert!(Error::Unimplemented.is_unimplemented());
|
|
491
|
+
|
|
492
|
+
// Test error codes
|
|
493
|
+
assert_eq!(codes::ERR_RPC_ABORT, "ERR_RPC_ABORT");
|
|
494
|
+
assert_eq!(codes::ERR_STREAM_IDLE, "ERR_STREAM_IDLE");
|
|
495
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
//! Transport utilities for starpc.
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides common transport-related functionality including
|
|
4
|
+
//! packet writers and stream reading helpers.
|
|
5
|
+
|
|
6
|
+
use async_trait::async_trait;
|
|
7
|
+
use bytes::{Bytes, BytesMut};
|
|
8
|
+
use futures::StreamExt;
|
|
9
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
10
|
+
use std::sync::Arc;
|
|
11
|
+
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
|
|
12
|
+
use tokio::sync::Mutex;
|
|
13
|
+
use tokio_util::codec::{Encoder, FramedRead};
|
|
14
|
+
|
|
15
|
+
use crate::codec::PacketCodec;
|
|
16
|
+
use crate::error::{Error, Result};
|
|
17
|
+
use crate::proto::Packet;
|
|
18
|
+
use crate::rpc::PacketWriter;
|
|
19
|
+
|
|
20
|
+
/// A packet writer over an async write transport.
|
|
21
|
+
///
|
|
22
|
+
/// This is the canonical implementation of `PacketWriter` for any transport
|
|
23
|
+
/// that implements `AsyncWrite`. It handles length-prefix framing and
|
|
24
|
+
/// thread-safe access to the underlying writer.
|
|
25
|
+
pub struct TransportPacketWriter<W> {
|
|
26
|
+
writer: Mutex<W>,
|
|
27
|
+
closed: AtomicBool,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl<W: AsyncWrite + Send + Unpin> TransportPacketWriter<W> {
|
|
31
|
+
/// Creates a new transport packet writer.
|
|
32
|
+
pub fn new(writer: W) -> Self {
|
|
33
|
+
Self {
|
|
34
|
+
writer: Mutex::new(writer),
|
|
35
|
+
closed: AtomicBool::new(false),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// Returns true if the writer has been closed.
|
|
40
|
+
pub fn is_closed(&self) -> bool {
|
|
41
|
+
self.closed.load(Ordering::SeqCst)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#[async_trait]
|
|
46
|
+
impl<W: AsyncWrite + Send + Unpin + 'static> PacketWriter for TransportPacketWriter<W> {
|
|
47
|
+
async fn write_packet(&self, packet: Packet) -> Result<()> {
|
|
48
|
+
if self.closed.load(Ordering::SeqCst) {
|
|
49
|
+
return Err(Error::StreamClosed);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let mut buf = BytesMut::new();
|
|
53
|
+
let mut codec = PacketCodec::new();
|
|
54
|
+
codec.encode(packet, &mut buf)?;
|
|
55
|
+
|
|
56
|
+
let mut writer = self.writer.lock().await;
|
|
57
|
+
writer.write_all(&buf).await?;
|
|
58
|
+
writer.flush().await?;
|
|
59
|
+
|
|
60
|
+
Ok(())
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async fn close(&self) -> Result<()> {
|
|
64
|
+
self.closed.store(true, Ordering::SeqCst);
|
|
65
|
+
let mut writer = self.writer.lock().await;
|
|
66
|
+
writer.shutdown().await?;
|
|
67
|
+
Ok(())
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// Receiver for incoming packets from a transport.
|
|
72
|
+
pub type PacketReceiver = tokio::sync::mpsc::Receiver<Packet>;
|
|
73
|
+
|
|
74
|
+
/// Sender for incoming packets to be processed.
|
|
75
|
+
pub type PacketSender = tokio::sync::mpsc::Sender<Packet>;
|
|
76
|
+
|
|
77
|
+
/// Default channel buffer size for packet channels.
|
|
78
|
+
pub const DEFAULT_CHANNEL_BUFFER: usize = 32;
|
|
79
|
+
|
|
80
|
+
/// Spawns a task that reads packets from a transport and sends them through a channel.
|
|
81
|
+
///
|
|
82
|
+
/// This is a common pattern used by both client and server to handle incoming packets.
|
|
83
|
+
/// The task will run until the transport is closed or an error occurs.
|
|
84
|
+
///
|
|
85
|
+
/// # Arguments
|
|
86
|
+
/// * `reader` - The async reader to read packets from
|
|
87
|
+
/// * `sender` - The channel sender to forward packets to
|
|
88
|
+
///
|
|
89
|
+
/// # Returns
|
|
90
|
+
/// A `JoinHandle` for the spawned task.
|
|
91
|
+
pub fn spawn_packet_reader<R>(
|
|
92
|
+
reader: R,
|
|
93
|
+
sender: PacketSender,
|
|
94
|
+
) -> tokio::task::JoinHandle<()>
|
|
95
|
+
where
|
|
96
|
+
R: AsyncRead + Send + Unpin + 'static,
|
|
97
|
+
{
|
|
98
|
+
tokio::spawn(async move {
|
|
99
|
+
let mut framed = FramedRead::new(reader, PacketCodec::new());
|
|
100
|
+
while let Some(result) = framed.next().await {
|
|
101
|
+
match result {
|
|
102
|
+
Ok(packet) => {
|
|
103
|
+
if sender.send(packet).await.is_err() {
|
|
104
|
+
// Receiver dropped, stop reading
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
Err(_) => {
|
|
109
|
+
// Read error, stop reading
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Creates a packet writer and receiver from a split transport.
|
|
118
|
+
///
|
|
119
|
+
/// This is a convenience function that:
|
|
120
|
+
/// 1. Creates a `TransportPacketWriter` from the write half
|
|
121
|
+
/// 2. Spawns a packet reader task for the read half
|
|
122
|
+
/// 3. Returns the writer and receiver channel
|
|
123
|
+
///
|
|
124
|
+
/// # Arguments
|
|
125
|
+
/// * `read_half` - The read half of the transport
|
|
126
|
+
/// * `write_half` - The write half of the transport
|
|
127
|
+
///
|
|
128
|
+
/// # Returns
|
|
129
|
+
/// A tuple of (packet writer, packet receiver).
|
|
130
|
+
pub fn create_packet_channel<R, W>(
|
|
131
|
+
read_half: R,
|
|
132
|
+
write_half: W,
|
|
133
|
+
) -> (Arc<dyn PacketWriter>, PacketReceiver)
|
|
134
|
+
where
|
|
135
|
+
R: AsyncRead + Send + Unpin + 'static,
|
|
136
|
+
W: AsyncWrite + Send + Unpin + 'static,
|
|
137
|
+
{
|
|
138
|
+
let writer: Arc<dyn PacketWriter> = Arc::new(TransportPacketWriter::new(write_half));
|
|
139
|
+
let (tx, rx) = tokio::sync::mpsc::channel(DEFAULT_CHANNEL_BUFFER);
|
|
140
|
+
spawn_packet_reader(read_half, tx);
|
|
141
|
+
(writer, rx)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Encodes optional data for protobuf messages.
|
|
145
|
+
///
|
|
146
|
+
/// Handles the `data_is_zero` flag convention used in starpc:
|
|
147
|
+
/// - `None` -> empty data, `data_is_zero = false`
|
|
148
|
+
/// - `Some(empty)` -> empty data, `data_is_zero = true`
|
|
149
|
+
/// - `Some(data)` -> data bytes, `data_is_zero = false`
|
|
150
|
+
///
|
|
151
|
+
/// # Returns
|
|
152
|
+
/// A tuple of (data bytes, data_is_zero flag).
|
|
153
|
+
pub fn encode_optional_data(data: Option<Bytes>) -> (Vec<u8>, bool) {
|
|
154
|
+
match data {
|
|
155
|
+
Some(d) if d.is_empty() => (vec![], true),
|
|
156
|
+
Some(d) => (d.to_vec(), false),
|
|
157
|
+
None => (vec![], false),
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// Decodes optional data from protobuf messages.
|
|
162
|
+
///
|
|
163
|
+
/// Inverse of `encode_optional_data`.
|
|
164
|
+
///
|
|
165
|
+
/// # Returns
|
|
166
|
+
/// `Some(Bytes)` if data was present (including empty data with `data_is_zero`),
|
|
167
|
+
/// `None` if no data was included.
|
|
168
|
+
pub fn decode_optional_data(data: Vec<u8>, data_is_zero: bool) -> Option<Bytes> {
|
|
169
|
+
if !data.is_empty() || data_is_zero {
|
|
170
|
+
Some(Bytes::from(data))
|
|
171
|
+
} else {
|
|
172
|
+
None
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#[cfg(test)]
|
|
177
|
+
mod tests {
|
|
178
|
+
use super::*;
|
|
179
|
+
|
|
180
|
+
#[test]
|
|
181
|
+
fn test_encode_optional_data_none() {
|
|
182
|
+
let (data, is_zero) = encode_optional_data(None);
|
|
183
|
+
assert!(data.is_empty());
|
|
184
|
+
assert!(!is_zero);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[test]
|
|
188
|
+
fn test_encode_optional_data_empty() {
|
|
189
|
+
let (data, is_zero) = encode_optional_data(Some(Bytes::new()));
|
|
190
|
+
assert!(data.is_empty());
|
|
191
|
+
assert!(is_zero);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#[test]
|
|
195
|
+
fn test_encode_optional_data_with_content() {
|
|
196
|
+
let (data, is_zero) = encode_optional_data(Some(Bytes::from(vec![1, 2, 3])));
|
|
197
|
+
assert_eq!(data, vec![1, 2, 3]);
|
|
198
|
+
assert!(!is_zero);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
#[test]
|
|
202
|
+
fn test_decode_optional_data_none() {
|
|
203
|
+
let result = decode_optional_data(vec![], false);
|
|
204
|
+
assert!(result.is_none());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
#[test]
|
|
208
|
+
fn test_decode_optional_data_empty() {
|
|
209
|
+
let result = decode_optional_data(vec![], true);
|
|
210
|
+
assert_eq!(result, Some(Bytes::new()));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#[test]
|
|
214
|
+
fn test_decode_optional_data_with_content() {
|
|
215
|
+
let result = decode_optional_data(vec![1, 2, 3], false);
|
|
216
|
+
assert_eq!(result, Some(Bytes::from(vec![1, 2, 3])));
|
|
217
|
+
}
|
|
218
|
+
}
|