-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchannel_time_range.rs
323 lines (262 loc) · 11.3 KB
/
channel_time_range.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
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
//! Test the various channel time range options.
//!
//! Create a cable manager by creating a TCP stream, invoke the cable listener
//! and then write and read requests and responses to and from the stream.
//!
//! Run the test with debug logging enabled in a terminal:
//!
//! `RUST_LOG=debug cargo test channel_time_range_request_response`
//!
//! An outline of the actions taken in this test:
//!
//! 1) Publish three posts to the "myco" channel.
//!
//! 2) Send a channel time range request for the "myco" channel with a start
//! and end time of `now()`. Ensure that no hashes are returned in the
//! response.
//!
//! 3) Send a channel time range request for the "myco" channel with a start
//! time before the three posts were published and an end time of `now()`.
//! Ensure that all three hashes are returned in the response.
//!
//! 4) Send a channel time range request for the "myco" channel with a start
//! time before the three posts were published, an end time of `now()` and a
//! limit of 2. Ensure that only two hashes are returned in the response.
//!
//! 5) Publish a post to the "books" channel.
//!
//! 6) Send a channel time range request for the "books" channel with a start
//! time before the post was published and an end time of 0. Ensure that
//! a single hash is returned in the response.
//!
//! 7) Publish a second post to the "books" channel.
//!
//! 8) Ensure that a hash response is received with two hashes.
use std::{thread, time::Duration};
use async_std::{
net::{TcpListener, TcpStream},
stream::StreamExt,
task,
};
use cable::{
constants::{HASH_RESPONSE, NO_CIRCUIT},
message::{MessageBody, ResponseBody},
ChannelOptions, Error, Message,
};
use desert::{FromBytes, ToBytes};
use futures::{AsyncReadExt, AsyncWriteExt};
use log::info;
use cable_core::{CableManager, MemoryStore};
// The circuit_id field is not currently in use; set to all zeros.
const CIRCUIT_ID: [u8; 4] = NO_CIRCUIT;
const TTL: u8 = 1;
// Initialise the logger in test mode.
//
// Set `is_test()` to `false` if you wish to see logging output during the
// test run.
fn init() {
let _ = env_logger::builder().is_test(false).try_init();
}
// Get the current system time in milliseconds since the UNIX epoch.
fn now() -> Result<u64, Error> {
let time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_millis()
.try_into()?;
Ok(time)
}
#[async_std::test]
async fn channel_time_range_request_response() -> Result<(), Error> {
init();
// Create a store and a cable manager.
let store = MemoryStore::default();
let mut cable = CableManager::new(store);
let cable_clone = cable.clone();
// Deploy a TCP listener.
//
// Assigning port to 0 means that the OS selects an available port for us.
let listener = TcpListener::bind("127.0.0.1:0").await?;
// Retrieve the address of the TCP listener to be able to connect later on.
let addr = listener.local_addr()?;
info!("Deployed TCP server on {}", addr);
task::spawn(async move {
// Listen for incoming TCP connections and pass any inbound streams to
// the cable manager.
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
if let Ok(stream) = stream {
let cable = cable_clone.clone();
task::spawn(async move {
cable.listen(stream).await.unwrap();
});
}
}
});
let mut stream = TcpStream::connect(addr).await?;
info!("Connected to TCP server on {}", addr);
// Create a timestamp for later use.
let time_before_posts_were_published = now()?;
// Publish a post to the "myco" channel.
let _post_hash_1 = cable
.post_text(
"myco",
"Listen, if you're a mushroom, you live cheap; besides, I'm telling you, this was a very nice neighborhood until the monkeys got out of control.",
)
.await?;
// Publish a second post to the "myco" channel.
let _post_hash_2 = cable.post_text("myco", "Ganoderma neo-japonicum").await?;
// Publish a third post to the "myco" channel.
let _post_hash_3 = cable.post_text("myco", "hyphal fusion").await?;
/* FIRST REQUEST */
// Generate a novel request ID.
let (_req_id, req_id_bytes) = cable.new_req_id().await?;
// Channel time range request parameters.
//
// These parameters should result in zero post hashes being returned, due
// to the fact that the three posts were published before the given
// `time_start` parameter.
let opts = ChannelOptions::new("myco", now()?, now()?, 10);
// Create a channel time range request.
let channel_time_range_req =
Message::channel_time_range_request(CIRCUIT_ID, req_id_bytes, TTL, opts);
let req_bytes = channel_time_range_req.to_bytes()?;
// Write the request bytes to the stream.
stream.write_all(&req_bytes).await?;
// Sleep briefly to allow time for the cable manager to respond.
let five_millis = Duration::from_millis(5);
thread::sleep(five_millis);
// Read the response from the stream.
let mut res_bytes = [0u8; 1024];
let _n = stream.read(&mut res_bytes).await?;
// Ensure that a hash response was returned by the listening peer.
let (_bytes_len, msg) = Message::from_bytes(&res_bytes)?;
assert_eq!(msg.message_type(), HASH_RESPONSE);
if let MessageBody::Response { body } = msg.body {
if let ResponseBody::Hash { hashes } = body {
// No post hashes should be returned.
assert_eq!(hashes.len(), 0);
}
}
/* SECOND REQUEST */
// Generate a novel request ID.
let (_req_id, req_id_bytes) = cable.new_req_id().await?;
// Sleep briefly to allow time to elapse since the posts were published.
let one_second = Duration::from_millis(1000);
thread::sleep(one_second);
// Channel time range request parameters.
//
// These parameters should result in three post hashes being returned, due
// to the fact that three posts were published after the given `time_start`
// parameter.
let opts = ChannelOptions::new("myco", time_before_posts_were_published, now()?, 5);
// Create a channel time range request.
let channel_time_range_req =
Message::channel_time_range_request(CIRCUIT_ID, req_id_bytes, TTL, opts);
let req_bytes = channel_time_range_req.to_bytes()?;
// Write the request bytes to the stream.
stream.write_all(&req_bytes).await?;
// Sleep briefly to allow time for the cable manager to respond.
let five_millis = Duration::from_millis(5);
thread::sleep(five_millis);
// Read the response from the stream.
let mut res_bytes = [0u8; 1024];
let _n = stream.read(&mut res_bytes).await?;
// Ensure that a hash response was returned by the listening peer.
let (_bytes_len, msg) = Message::from_bytes(&res_bytes)?;
assert_eq!(msg.message_type(), HASH_RESPONSE);
if let MessageBody::Response { body } = msg.body {
if let ResponseBody::Hash { hashes } = body {
// Three post hashes should be returned.
assert_eq!(hashes.len(), 3);
}
}
/* THIRD REQUEST */
// Generate a novel request ID.
let (_req_id, req_id_bytes) = cable.new_req_id().await?;
// Channel time range request parameters.
//
// These parameters should result in two post hashes being returned, due
// to the fact that three posts were published after the given `time_start`
// parameter but the `limit` parameter is set to 2.
let opts = ChannelOptions::new("myco", time_before_posts_were_published, now()?, 2);
// Create a channel time range request.
let channel_time_range_req =
Message::channel_time_range_request(CIRCUIT_ID, req_id_bytes, TTL, opts);
let req_bytes = channel_time_range_req.to_bytes()?;
// Write the request bytes to the stream.
stream.write_all(&req_bytes).await?;
// Sleep briefly to allow time for the cable manager to respond.
let five_millis = Duration::from_millis(5);
thread::sleep(five_millis);
// Read the response from the stream.
let mut res_bytes = [0u8; 1024];
let _n = stream.read(&mut res_bytes).await?;
// Ensure that a hash response was returned by the listening peer.
let (_bytes_len, msg) = Message::from_bytes(&res_bytes)?;
assert_eq!(msg.message_type(), HASH_RESPONSE);
if let MessageBody::Response { body } = msg.body {
if let ResponseBody::Hash { hashes } = body {
// Two post hashes should be returned.
assert_eq!(hashes.len(), 2);
}
}
/* FOURTH REQUEST */
// Publish a post to the "books" channel.
let _post_hash_1 = cable
.post_text("books", "Just started reading 'Strange Weather in Tokyo'")
.await?;
// Generate a novel request ID.
let (_req_id, req_id_bytes) = cable.new_req_id().await?;
// Channel time range request parameters.
//
// These parameters should result in a single post hash being returned, due
// to the fact that only one post was published after the given `time_start`
// parameter.
//
// The request should also result in additional responses being sent for
// every post subsequently published to the "books" channel, due to the
// fact that the `time_end` parameter is set to 0 (keep the request alive
// and continue to send new posts as they're published).
let opts = ChannelOptions::new("books", time_before_posts_were_published, 0, 10);
// Create a channel time range request.
let channel_time_range_req =
Message::channel_time_range_request(CIRCUIT_ID, req_id_bytes, TTL, opts);
let req_bytes = channel_time_range_req.to_bytes()?;
// Write the request bytes to the stream.
stream.write_all(&req_bytes).await?;
// Sleep briefly to allow time for the cable manager to respond.
let five_millis = Duration::from_millis(5);
thread::sleep(five_millis);
// Read the response from the stream.
let mut res_bytes = [0u8; 1024];
let _n = stream.read(&mut res_bytes).await?;
// Ensure that a hash response was returned by the listening peer.
let (_bytes_len, msg) = Message::from_bytes(&res_bytes)?;
assert_eq!(msg.message_type(), HASH_RESPONSE);
if let MessageBody::Response { body } = msg.body {
if let ResponseBody::Hash { hashes } = body {
// One post hash should be returned.
assert_eq!(hashes.len(), 1);
}
}
// Publish a second post to the "books" channel.
let _post_hash_2 = cable
.post_text("books", "Have you set any reading goals for the year?")
.await?;
// Sleep briefly to allow time for the cable manager to respond.
let five_millis = Duration::from_millis(5);
thread::sleep(five_millis);
// Read the response from the stream.
let mut res_bytes = [0u8; 1024];
let _n = stream.read(&mut res_bytes).await?;
// Ensure that a hash response was returned by the listening peer.
let (_bytes_len, msg) = Message::from_bytes(&res_bytes)?;
assert_eq!(msg.message_type(), HASH_RESPONSE);
if let MessageBody::Response { body } = msg.body {
if let ResponseBody::Hash { hashes } = body {
// Two post hashes should be returned.
assert_eq!(hashes.len(), 2);
}
}
Ok(())
}