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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std;
use std::time::Duration;
use slog;
use specs;
use super::*;
// Nothing interesting in here!
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
struct TestMessage {
disposition: String,
}
impl GameMessage for TestMessage {}
// Network node helper. Contains all the network
// server bits and Specs bits required to simulate
// a network node, so we can easily play with multiple.
struct Node {
world: specs::World,
dispatcher: specs::Dispatcher<'static, 'static>,
}
impl Node {
pub fn new() -> Node {
use ::LogResource;
let drain = slog::Discard;
let root_log = slog::Logger::root(drain, o!("pk_version" => env!("CARGO_PKG_VERSION")));
let mut world = specs::World::new();
// Initialize common resources.
// These should be impossible to create from
// just a `World`; `pk::Resource` should be
// preferred to ensure those.
world.add_resource(LogResource::new(&root_log));
// Create core network systems.
let new_peer_system = NewPeerSystem::<TestMessage>::new(&root_log, &mut world);
let recv_system = RecvSystem::<TestMessage>::new(&root_log, &mut world);
let send_system = SendSystem::<TestMessage>::new(&root_log, &mut world);
// Make a dispatcher, with simplified execution order.
// TODO: replace this with waiting for specific things to be ready,
// so you don't need to fiddle with fine timings.
let dispatcher = specs::DispatcherBuilder::new()
.add(new_peer_system, "new_peer", &[])
.add_barrier()
.add(recv_system, "recv", &[])
.add_barrier()
.add(send_system, "send", &[])
.build();
Node {
world: world,
dispatcher: dispatcher,
}
}
pub fn new_server() -> Node {
let server_node = Node::new();
{
// NLL SVP
let server_resource = server_node.world.read_resource::<ServerResource<TestMessage>>();
let mut server = server_resource.server.lock().expect("Couldn't lock server");
server.start_listen(None);
}
server_node
}
pub fn new_client_connected_to(server_node: &Node) -> Node {
let client_node = Node::new();
let server_server_resource = server_node.world.read_resource::<ServerResource<TestMessage>>();
let server_server = server_server_resource.server.lock().expect("Couldn't lock server");
let connect_addr = format!("127.0.0.1:{}", server_server.port.expect("Should be listening"));
let connect_addr: SocketAddr = connect_addr.parse().unwrap();
{
// NLL SVP
let client_server_resource = client_node.world.read_resource::<ServerResource<TestMessage>>();
let mut client_server = client_server_resource.server.lock().expect("Couldn't lock server");
client_server.connect(connect_addr);
}
client_node
}
pub fn dispatch(&mut self) {
self.dispatcher.dispatch(&mut self.world.res);
}
pub fn enqueue_message(&mut self, message: SendMessage<TestMessage>) {
let send_queue = &mut self.world.write_resource::<SendMessageQueue<TestMessage>>().queue;
send_queue.push_back(message);
}
pub fn expect_message(&mut self, expected_message: TestMessage) {
let recv_queue = &mut self.world.write_resource::<RecvMessageQueue<TestMessage>>().queue;
assert!(recv_queue.len() >= 1);
let received_message = recv_queue.pop_front().unwrap().game_message;
assert_eq!(received_message, expected_message);
}
}
#[test]
fn client_sends_udp_message_to_server() {
let mut server_node = Node::new_server();
let mut client_node = Node::new_client_connected_to(&server_node);
// Give the server a chance to register the new peer,
// so that when first check for a message, there's one
// already received from the TCP stack.
//
// Client node should do this shortly before it sends its message,
// because it gets a chance to dispatch anyway.
//
// These are pretty low-level tests, so this is ok here,
// but higher-level tests should all have sensible timeouts
// etc. instead.
std::thread::sleep(Duration::from_millis(10));
server_node.dispatch();
// Put a message on the SendMessageQueue of the client node,
// to be sent over UDP.
client_node.enqueue_message(
SendMessage {
// Peer ID 0 is self.
destination: Destination::One(PeerId(1)),
game_message: TestMessage{
disposition: "Sunny!".to_string(),
},
transport: Transport::UDP,
}
);
// Step the world.
// This should send the message.
client_node.dispatch();
// Sleep a while to make sure we receive the message.
std::thread::sleep(Duration::from_millis(10));
// This should receive the message.
server_node.dispatch();
// Server should have received equivalent message.
server_node.expect_message(TestMessage {
disposition: "Sunny!".to_string(),
});
// TODO: gracefully shut down the server before the end of all tests;
// you don't want to leave the thread hanging around awkwardly.
}
#[test]
fn client_sends_tcp_messages_to_server() {
let mut server_node = Node::new_server();
let mut client_node = Node::new_client_connected_to(&server_node);
// Give the server a chance to register the new peer,
// so that when first check for a message, there's one
// already received from the TCP stack.
//
// Client node should do this shortly before it sends its message,
// because it gets a chance to dispatch anyway.
//
// These are pretty low-level tests, so this is ok here,
// but higher-level tests should all have sensible timeouts
// etc. instead.
std::thread::sleep(Duration::from_millis(10));
server_node.dispatch();
// Testing multiple TCP messages is kind of interesting
// because we need to make sure we don't corrupt the
// stream/buffer when receiving them, as opposed to UDP
// where we work with individual datagrams.
client_node.enqueue_message(
SendMessage {
// Peer ID 0 is self.
destination: Destination::One(PeerId(1)),
game_message: TestMessage{
disposition: "Cooperative!".to_string(),
},
transport: Transport::TCP,
}
);
client_node.enqueue_message(
SendMessage {
// Peer ID 0 is self.
destination: Destination::One(PeerId(1)),
game_message: TestMessage{
disposition: "Enthusiastic!".to_string(),
},
transport: Transport::TCP,
}
);
// This should send the message.
client_node.dispatch();
// Sleep a while to make sure we receive the message.
std::thread::sleep(Duration::from_millis(10));
// This should receive the message.
server_node.dispatch();
// Server should have received equivalent messages, in order.
server_node.expect_message(TestMessage {
disposition: "Cooperative!".to_string(),
});
server_node.expect_message(TestMessage {
disposition: "Enthusiastic!".to_string(),
});
// TODO: gracefully shut down the server before the end of all tests;
// you don't want to leave the thread hanging around awkwardly.
}
#[test]
fn server_sends_udp_message_to_client() {
let mut server_node = Node::new_server();
let mut client_node = Node::new_client_connected_to(&server_node);
// Give the client a chance to register the new peer,
// so that when first check for a message, there's one
// already received from the UDP stack.
//
// Server node should do this shortly before it sends its message,
// because it gets a chance to dispatch anyway.
//
// These are pretty low-level tests, so this is ok here,
// but higher-level tests should all have sensible timeouts
// etc. instead.
std::thread::sleep(Duration::from_millis(10));
client_node.dispatch();
// Put a message on the SendMessageQueue of the server node,
// to be sent over UDP.
server_node.enqueue_message(
SendMessage {
// Peer ID 0 is self.
destination: Destination::One(PeerId(1)),
game_message: TestMessage{
disposition: "Authoritative!".to_string(),
},
transport: Transport::UDP,
}
);
// Step the world.
// This should send the message.
server_node.dispatch();
// Sleep a while to make sure we receive the message.
std::thread::sleep(Duration::from_millis(10));
// This should receive the message.
client_node.dispatch();
// Client should have received equivalent message.
client_node.expect_message(TestMessage {
disposition: "Authoritative!".to_string(),
});
// TODO: gracefully shut down the server before the end of all tests;
// you don't want to leave the thread hanging around awkwardly.
}
#[test]
fn server_sends_tcp_messages_to_client() {
let mut server_node = Node::new_server();
let mut client_node = Node::new_client_connected_to(&server_node);
// Give the client a chance to register the new peer,
// so that when first check for a message, there's one
// already received from the UDP stack.
//
// Server node should do this shortly before it sends its message,
// because it gets a chance to dispatch anyway.
//
// These are pretty low-level tests, so this is ok here,
// but higher-level tests should all have sensible timeouts
// etc. instead.
std::thread::sleep(Duration::from_millis(10));
client_node.dispatch();
// Testing multiple TCP messages is kind of interesting
// because we need to make sure we don't corrupt the
// stream/buffer when receiving them, as opposed to UDP
// where we work with individual datagrams.
server_node.enqueue_message(
SendMessage {
// Peer ID 0 is self.
destination: Destination::One(PeerId(1)),
game_message: TestMessage{
disposition: "Oppressive!".to_string(),
},
transport: Transport::TCP,
}
);
server_node.enqueue_message(
SendMessage {
// Peer ID 0 is self.
destination: Destination::One(PeerId(1)),
game_message: TestMessage{
disposition: "Demanding!".to_string(),
},
transport: Transport::TCP,
}
);
// Step the world.
// This should send the message.
server_node.dispatch();
// Sleep a while to make sure we receive the message.
std::thread::sleep(Duration::from_millis(10));
// This should receive the message.
client_node.dispatch();
// Client should have received equivalent messages, in order.
client_node.expect_message(TestMessage {
disposition: "Oppressive!".to_string(),
});
client_node.expect_message(TestMessage {
disposition: "Demanding!".to_string(),
});
// TODO: gracefully shut down the server before the end of all tests;
// you don't want to leave the thread hanging around awkwardly.
}