-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rs
193 lines (153 loc) · 5.45 KB
/
test.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
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
use super::*;
use std::time::Instant;
pub mod protocols {
pub mod ping_pong {
use crate::*;
pub struct Ping;
pub struct Pong;
// NOTE: This verison doesn't include looping.
pub type Server = Recv<Ping, Send<Pong, Eps>>;
pub type Client = <Server as HasDual>::Dual;
}
pub mod greetings {
use std::time::Instant;
use crate::*;
pub struct Hail(pub String);
pub struct Greetings(pub String);
pub struct TimeRequest;
pub struct TimeResponse(pub Instant);
pub struct AddRequest(pub u32);
pub struct AddResponse(pub u32);
pub struct Quit;
pub type TimeProtocol = Recv<TimeRequest, Send<TimeResponse, Var<Z>>>;
pub type AddProtocol = Recv<AddRequest, Recv<AddRequest, Send<AddResponse, Var<Z>>>>;
pub type QuitProtocol = Recv<Quit, Eps>;
pub type ProtocolChoices = Offer<TimeProtocol, Offer<AddProtocol, QuitProtocol>>;
pub type Server = Recv<Hail, Send<Greetings, Rec<ProtocolChoices>>>;
pub type Client = <Server as HasDual>::Dual;
}
}
#[tokio::test]
async fn ping_pong_basics() {
use protocols::ping_pong::*;
let t = Duration::from_millis(100);
let srv = async move |c: Chan<Server, (), DynMessage>| {
let (c, Ping) = c.recv(t).await?;
c.send(Pong)?.close()
};
let cli = async move |c: Chan<Client, (), DynMessage>| {
let c = c.send(Ping)?;
let (c, Pong) = c.recv(t).await?;
c.close()
};
let (server_chan, client_chan) = session_channel();
let srv_t = tokio::spawn(srv(server_chan));
let cli_t = tokio::spawn(cli(client_chan));
srv_t.await.unwrap().unwrap();
cli_t.await.unwrap().unwrap();
}
#[tokio::test]
async fn ping_pong_error() {
use protocols::ping_pong::*;
let t = Duration::from_secs(10);
type WrongClient = Send<String, Recv<u64, Eps>>;
let srv = async move |c: Chan<Server, (), DynMessage>| {
let (c, Ping) = c.recv(t).await?;
c.send(Pong)?.close()
};
let cli = async move |c: Chan<WrongClient, (), DynMessage>| {
let c = c.send("Hello".into())?;
let (c, _n) = c.recv(t).await?;
c.close()
};
let (server_chan, client_chan) = session_channel();
let wrong_client_chan = client_chan.cast::<WrongClient, ()>();
let srv_t = tokio::spawn(srv(server_chan));
let cli_t = tokio::spawn(cli(wrong_client_chan));
let sr = srv_t.await.unwrap();
let cr = cli_t.await.unwrap();
assert!(sr.is_err());
assert!(cr.is_err());
}
#[tokio::test]
async fn greetings() {
use protocols::greetings::*;
// It is at this point that an invalid protocol would fail to compile.
let (server_chan, client_chan) = session_channel::<Server, DynMessage>();
let srv = |c: Chan<Server, (), DynMessage>| async {
let t = Duration::from_millis(100);
let (c, Hail(cid)) = c.recv(t).await?;
let c = c.send(Greetings(format!("Hello {}!", cid)))?;
let mut c = c.enter();
loop {
c = offer! { c, t,
Time => {
let (c, TimeRequest) = c.recv(t).await?;
let c = c.send(TimeResponse(Instant::now()))?;
c.zero()?
},
Add => {
let (c, AddRequest(a)) = c.recv(t).await?;
let (c, AddRequest(b)) = c.recv(t).await?;
let c = c.send(AddResponse(a + b))?;
c.zero()?
},
Quit => {
let (c, Quit) = c.recv(t).await?;
c.close()?;
break;
}
};
}
ok(())
};
let cli = |c: Chan<Client, (), DynMessage>| async {
let t = Duration::from_millis(100);
let c = c.send(Hail("Rusty".into()))?;
let (c, Greetings(_)) = c.recv(t).await?;
let c = c.enter();
let (c, AddResponse(r)) = c
.sel2()
.sel1()
.send(AddRequest(1))?
.send(AddRequest(2))?
.recv(t)
.await?;
c.zero()?.sel2().sel2().send(Quit)?.close()?;
ok(r)
};
let srv_t = tokio::spawn(srv(server_chan));
let cli_t = tokio::spawn(cli(client_chan));
let sr = srv_t.await.unwrap();
let cr = cli_t.await.unwrap();
assert!(sr.is_ok());
assert_eq!(cr.unwrap(), 3);
}
#[tokio::test]
async fn ping_pong_generic_repr() {
use protocols::ping_pong::*;
// Define a wrapper type to be uses as Repr `R` in generic versions of `srv` and `cli`.
enum Wrapper {
Ping(Ping),
Pong(Pong),
}
repr_bound! { PingPongReprs [Ping, Pong] }
repr_impl! { Wrapper {
Ping: ( Wrapper::Ping, Wrapper::Ping(x) => x ),
Pong: ( |p| Wrapper::Pong(p), Wrapper::Pong(x) => x )
} }
async fn srv<R: PingPongReprs>(c: Chan<Server, (), R>) -> SessionResult<()> {
let (c, Ping) = c.recv(Duration::from_millis(100)).await?;
c.send(Pong)?.close()
}
async fn cli<R: PingPongReprs>(c: Chan<Client, (), R>) -> SessionResult<()> {
let c = c.send(Ping)?;
let (c, _pong) = c.recv(Duration::from_millis(100)).await?;
c.close()
}
let (server_chan, client_chan) = session_channel::<Server, Wrapper>();
let srv_t = tokio::spawn(srv(server_chan));
let cli_t = tokio::spawn(cli(client_chan));
srv_t.await.unwrap().unwrap();
cli_t.await.unwrap().unwrap();
}