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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
use std::cmp;
use std::io::prelude::*;
use std::io;
use std::net;
use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;
use net2::{self, TcpStreamExt};
use {TryRead, TryWrite};
use mio::{Token, Ready, PollOpt, Poll, Events};
use iovec::IoVec;
use mio::net::{TcpListener, TcpStream};
#[test]
fn accept() {
struct H { hit: bool, listener: TcpListener, shutdown: bool }
let l = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
net::TcpStream::connect(&addr).unwrap();
});
let poll = Poll::new().unwrap();
poll.register(&l, Token(1), Ready::readable(), PollOpt::edge()).unwrap();
let mut events = Events::with_capacity(128);
let mut h = H { hit: false, listener: l, shutdown: false };
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
h.hit = true;
assert_eq!(event.token(), Token(1));
assert!(event.readiness().is_readable());
assert!(h.listener.accept().is_ok());
h.shutdown = true;
}
}
assert!(h.hit);
assert!(h.listener.accept().unwrap_err().kind() == io::ErrorKind::WouldBlock);
t.join().unwrap();
}
#[test]
fn connect() {
struct H { hit: u32, shutdown: bool }
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let (tx, rx) = channel();
let (tx2, rx2) = channel();
let t = thread::spawn(move || {
let s = l.accept().unwrap();
rx.recv().unwrap();
drop(s);
tx2.send(()).unwrap();
});
let poll = Poll::new().unwrap();
let s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::readable() | Ready::writable(), PollOpt::edge()).unwrap();
let mut events = Events::with_capacity(128);
let mut h = H { hit: 0, shutdown: false };
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
assert_eq!(event.token(), Token(1));
match h.hit {
0 => assert!(event.readiness().is_writable()),
1 => assert!(event.readiness().is_readable()),
_ => panic!(),
}
h.hit += 1;
h.shutdown = true;
}
}
assert_eq!(h.hit, 1);
tx.send(()).unwrap();
rx2.recv().unwrap();
h.shutdown = false;
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
assert_eq!(event.token(), Token(1));
match h.hit {
0 => assert!(event.readiness().is_writable()),
1 => assert!(event.readiness().is_readable()),
_ => panic!(),
}
h.hit += 1;
h.shutdown = true;
}
}
assert_eq!(h.hit, 2);
t.join().unwrap();
}
#[test]
fn read() {
const N: usize = 16 * 1024 * 1024;
struct H { amt: usize, socket: TcpStream, shutdown: bool }
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
let mut s = l.accept().unwrap().0;
let b = [0; 1024];
let mut amt = 0;
while amt < N {
amt += s.write(&b).unwrap();
}
});
let poll = Poll::new().unwrap();
let s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::readable(), PollOpt::edge()).unwrap();
let mut events = Events::with_capacity(128);
let mut h = H { amt: 0, socket: s, shutdown: false };
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
assert_eq!(event.token(), Token(1));
let mut b = [0; 1024];
loop {
if let Some(amt) = h.socket.try_read(&mut b).unwrap() {
h.amt += amt;
} else {
break
}
if h.amt >= N {
h.shutdown = true;
break
}
}
}
}
t.join().unwrap();
}
#[test]
fn peek() {
const N: usize = 16 * 1024 * 1024;
struct H { amt: usize, socket: TcpStream, shutdown: bool }
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
let mut s = l.accept().unwrap().0;
let b = [0; 1024];
let mut amt = 0;
while amt < N {
amt += s.write(&b).unwrap();
}
});
let poll = Poll::new().unwrap();
let s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::readable(), PollOpt::edge()).unwrap();
let mut events = Events::with_capacity(128);
let mut h = H { amt: 0, socket: s, shutdown: false };
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
assert_eq!(event.token(), Token(1));
let mut b = [0; 1024];
match h.socket.peek(&mut b) {
Ok(_) => (),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
continue
},
Err(e) => panic!("unexpected error: {:?}", e),
}
loop {
if let Some(amt) = h.socket.try_read(&mut b).unwrap() {
h.amt += amt;
} else {
break
}
if h.amt >= N {
h.shutdown = true;
break
}
}
}
}
t.join().unwrap();
}
#[test]
fn read_bufs() {
const N: usize = 16 * 1024 * 1024;
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
let mut s = l.accept().unwrap().0;
let b = [1; 1024];
let mut amt = 0;
while amt < N {
amt += s.write(&b).unwrap();
}
});
let poll = Poll::new().unwrap();
let mut events = Events::with_capacity(128);
let s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::readable(), PollOpt::level()).unwrap();
let b1 = &mut [0; 10][..];
let b2 = &mut [0; 383][..];
let b3 = &mut [0; 28][..];
let b4 = &mut [0; 8][..];
let b5 = &mut [0; 128][..];
let mut b: [&mut IoVec; 5] = [
b1.into(),
b2.into(),
b3.into(),
b4.into(),
b5.into(),
];
let mut so_far = 0;
loop {
for buf in b.iter_mut() {
for byte in buf.as_mut_bytes() {
*byte = 0;
}
}
poll.poll(&mut events, None).unwrap();
match s.read_bufs(&mut b) {
Ok(0) => {
assert_eq!(so_far, N);
break
}
Ok(mut n) => {
so_far += n;
for buf in b.iter() {
let buf = buf.as_bytes();
for byte in buf[..cmp::min(n, buf.len())].iter() {
assert_eq!(*byte, 1);
}
n = n.saturating_sub(buf.len());
if n == 0 {
break
}
}
assert_eq!(n, 0);
}
Err(e) => assert_eq!(e.kind(), io::ErrorKind::WouldBlock),
}
}
t.join().unwrap();
}
#[test]
fn write() {
const N: usize = 16 * 1024 * 1024;
struct H { amt: usize, socket: TcpStream, shutdown: bool }
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
let mut s = l.accept().unwrap().0;
let mut b = [0; 1024];
let mut amt = 0;
while amt < N {
amt += s.read(&mut b).unwrap();
}
});
let poll = Poll::new().unwrap();
let s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::writable(), PollOpt::edge()).unwrap();
let mut events = Events::with_capacity(128);
let mut h = H { amt: 0, socket: s, shutdown: false };
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
assert_eq!(event.token(), Token(1));
let b = [0; 1024];
loop {
if let Some(amt) = h.socket.try_write(&b).unwrap() {
h.amt += amt;
} else {
break
}
if h.amt >= N {
h.shutdown = true;
break
}
}
}
}
t.join().unwrap();
}
#[test]
fn write_bufs() {
const N: usize = 16 * 1024 * 1024;
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
let mut s = l.accept().unwrap().0;
let mut b = [0; 1024];
let mut amt = 0;
while amt < N {
for byte in b.iter_mut() {
*byte = 0;
}
let n = s.read(&mut b).unwrap();
amt += n;
for byte in b[..n].iter() {
assert_eq!(*byte, 1);
}
}
});
let poll = Poll::new().unwrap();
let mut events = Events::with_capacity(128);
let s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::writable(), PollOpt::level()).unwrap();
let b1 = &[1; 10][..];
let b2 = &[1; 383][..];
let b3 = &[1; 28][..];
let b4 = &[1; 8][..];
let b5 = &[1; 128][..];
let b: [&IoVec; 5] = [
b1.into(),
b2.into(),
b3.into(),
b4.into(),
b5.into(),
];
let mut so_far = 0;
while so_far < N {
poll.poll(&mut events, None).unwrap();
match s.write_bufs(&b) {
Ok(n) => so_far += n,
Err(e) => assert_eq!(e.kind(), io::ErrorKind::WouldBlock),
}
}
t.join().unwrap();
}
#[test]
fn connect_then_close() {
struct H { listener: TcpListener, shutdown: bool }
let poll = Poll::new().unwrap();
let l = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let s = TcpStream::connect(&l.local_addr().unwrap()).unwrap();
poll.register(&l, Token(1), Ready::readable(), PollOpt::edge()).unwrap();
poll.register(&s, Token(2), Ready::readable(), PollOpt::edge()).unwrap();
let mut events = Events::with_capacity(128);
let mut h = H { listener: l, shutdown: false };
while !h.shutdown {
poll.poll(&mut events, None).unwrap();
for event in &events {
if event.token() == Token(1) {
let s = h.listener.accept().unwrap().0;
poll.register(&s, Token(3), Ready::readable() | Ready::writable(),
PollOpt::edge()).unwrap();
drop(s);
} else if event.token() == Token(2) {
h.shutdown = true;
}
}
}
}
#[test]
fn listen_then_close() {
let poll = Poll::new().unwrap();
let l = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
poll.register(&l, Token(1), Ready::readable(), PollOpt::edge()).unwrap();
drop(l);
let mut events = Events::with_capacity(128);
poll.poll(&mut events, Some(Duration::from_millis(100))).unwrap();
for event in &events {
if event.token() == Token(1) {
panic!("recieved ready() on a closed TcpListener")
}
}
}
fn assert_send<T: Send>() {
}
fn assert_sync<T: Sync>() {
}
#[test]
fn test_tcp_sockets_are_send() {
assert_send::<TcpListener>();
assert_send::<TcpStream>();
assert_sync::<TcpListener>();
assert_sync::<TcpStream>();
}
#[test]
fn bind_twice_bad() {
let l1 = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = l1.local_addr().unwrap();
assert!(TcpListener::bind(&addr).is_err());
}
#[test]
fn multiple_writes_immediate_success() {
const N: usize = 16;
let l = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap();
let t = thread::spawn(move || {
let mut s = l.accept().unwrap().0;
let mut b = [0; 1024];
let mut amt = 0;
while amt < 1024*N {
for byte in b.iter_mut() {
*byte = 0;
}
let n = s.read(&mut b).unwrap();
amt += n;
for byte in b[..n].iter() {
assert_eq!(*byte, 1);
}
}
});
let poll = Poll::new().unwrap();
let mut s = TcpStream::connect(&addr).unwrap();
poll.register(&s, Token(1), Ready::writable(), PollOpt::level()).unwrap();
let mut events = Events::with_capacity(16);
// Wait for our TCP stream to connect
'outer: loop {
poll.poll(&mut events, None).unwrap();
for event in events.iter() {
if event.token() == Token(1) && event.readiness().is_writable() {
break 'outer
}
}
}
for _ in 0..N {
s.write_all(&[1; 1024]).unwrap();
}
t.join().unwrap();
}
#[test]
fn connection_reset_by_peer() {
let poll = Poll::new().unwrap();
let mut events = Events::with_capacity(16);
let mut buf = [0u8; 16];
// Create listener
let l = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = l.local_addr().unwrap();
// Connect client
let client = net2::TcpBuilder::new_v4().unwrap()
.to_tcp_stream().unwrap();
client.set_linger(Some(Duration::from_millis(0))).unwrap();
client.connect(&addr).unwrap();
// Convert to Mio stream
let client = TcpStream::from_stream(client).unwrap();
// Register server
poll.register(&l, Token(0), Ready::readable(), PollOpt::edge()).unwrap();
// Register interest in the client
poll.register(&client, Token(1), Ready::readable() | Ready::writable(), PollOpt::edge()).unwrap();
// Wait for listener to be ready
let mut server;
'outer:
loop {
poll.poll(&mut events, None).unwrap();
for event in &events {
if event.token() == Token(0) {
match l.accept() {
Ok((sock, _)) => {
server = sock;
break 'outer;
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
Err(e) => panic!("unexpected error {:?}", e),
}
}
}
}
// Close the connection
drop(client);
// Wait a moment
thread::sleep(Duration::from_millis(100));
// Register interest in the server socket
poll.register(&server, Token(3), Ready::readable(), PollOpt::edge()).unwrap();
loop {
poll.poll(&mut events, None).unwrap();
for event in &events {
if event.token() == Token(3) {
assert!(event.readiness().is_readable());
match server.read(&mut buf) {
Ok(0) |
Err(_) => {},
Ok(x) => panic!("expected empty buffer but read {} bytes", x),
}
return;
}
}
}
}
#[test]
#[cfg_attr(target_os = "fuchsia", ignore)]
fn connect_error() {
let poll = Poll::new().unwrap();
let mut events = Events::with_capacity(16);
// Pick a "random" port that shouldn't be in use.
let l = match TcpStream::connect(&"127.0.0.1:38381".parse().unwrap()) {
Ok(l) => l,
Err(ref e) if e.kind() == io::ErrorKind::ConnectionRefused => {
// Connection failed synchronously. This is not a bug, but it
// unfortunately doesn't get us the code coverage we want.
return;
},
Err(e) => panic!("TcpStream::connect unexpected error {:?}", e)
};
poll.register(&l, Token(0), Ready::writable(), PollOpt::edge()).unwrap();
'outer:
loop {
poll.poll(&mut events, None).unwrap();
for event in &events {
if event.token() == Token(0) {
assert!(event.readiness().is_writable());
break 'outer
}
}
}
assert!(l.take_error().unwrap().is_some());
}
#[test]
fn write_error() {
let poll = Poll::new().unwrap();
let mut events = Events::with_capacity(16);
let (tx, rx) = channel();
let listener = net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let t = thread::spawn(move || {
let (conn, _addr) = listener.accept().unwrap();
rx.recv().unwrap();
drop(conn);
});
let mut s = TcpStream::connect(&addr).unwrap();
poll.register(&s,
Token(0),
Ready::readable() | Ready::writable(),
PollOpt::edge()).unwrap();
let mut wait_writable = || {
'outer:
loop {
poll.poll(&mut events, None).unwrap();
for event in &events {
if event.token() == Token(0) && event.readiness().is_writable() {
break 'outer
}
}
}
};
wait_writable();
tx.send(()).unwrap();
t.join().unwrap();
let buf = [0; 1024];
loop {
match s.write(&buf) {
Ok(_) => {}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
wait_writable()
}
Err(e) => {
println!("good error: {}", e);
break
}
}
}
}
|