-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpc.rs
107 lines (94 loc) · 2.75 KB
/
rpc.rs
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
use std::cell::RefCell;
use std::rc::Rc;
use log::info;
use rpc_core::request::FinallyType;
#[test]
fn rpc() {
std::env::set_var("RUST_LOG", "trace");
env_logger::init();
// loopback connection
let (connection_s, connection_c) = rpc_core::connection::LoopbackConnection::new();
// rpc server
let rpc_s = rpc_core::rpc::Rpc::new(Some(connection_s));
rpc_s.set_timer(|ms: u32, _: Box<dyn Fn()>| {
info!("set_timer: {ms}");
});
rpc_s.set_ready(true);
rpc_s.subscribe("cmd", |msg: String| -> String {
assert_eq!(msg, "hello");
"world".to_string()
});
// rpc client
let rpc_c = rpc_core::rpc::Rpc::new(Some(connection_c));
rpc_c.set_timer(|ms: u32, _: Box<dyn Fn()>| {
info!("set_timer: {ms}");
});
rpc_c.set_ready(true);
// test code
let pass = Rc::new(RefCell::new(false));
let pass_clone = pass.clone();
rpc_c
.cmd("cmd")
.msg("hello")
.rsp(move |msg: String| {
assert_eq!(msg, "world");
*pass_clone.borrow_mut() = true;
})
.call();
assert!(*pass.borrow());
info!("--- test unsubscribe ---");
rpc_s.subscribe("x", |_: ()| {});
rpc_s.unsubscribe("x");
info!("--- test ping ---");
*pass.borrow_mut() = false;
let pass_clone = pass.clone();
rpc_c.ping();
rpc_c
.ping_msg("hello")
.rsp(move |msg: String| {
info!("rsp: {}", msg);
*pass_clone.borrow_mut() = true;
})
.call();
assert!(*pass.borrow());
info!("--- test request ---");
{
let request = rpc_core::request::Request::new();
let pass = Rc::new(RefCell::new(false));
let pass_clone = pass.clone();
request
.cmd("cmd")
.msg("hello")
.rsp(move |msg: String| {
assert_eq!(msg, "world");
*pass_clone.borrow_mut() = true;
})
.call_with_rpc(rpc_c.clone());
assert!(*pass.borrow());
}
info!("--- test dispose ---");
{
rpc_s.subscribe("cmd", |_: String| -> String {
assert!(false);
"".to_string()
});
let pass = Rc::new(RefCell::new(false));
let pass_clone = pass.clone();
let request = rpc_c.cmd("cmd");
request
.msg("hello")
.rsp(|_: String| {
assert!(false);
})
.finally(move |t| {
assert_eq!(t, FinallyType::Canceled);
*pass_clone.borrow_mut() = true;
});
{
let mut dispose = rpc_core::dispose::Dispose::new();
request.add_to(&mut dispose);
}
request.call();
assert!(*pass.borrow());
}
}