-
Notifications
You must be signed in to change notification settings - Fork 743
/
tcp_stream.rs
808 lines (639 loc) · 22.8 KB
/
tcp_stream.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
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#![cfg(all(feature = "os-poll", feature = "tcp"))]
use mio::net::TcpStream;
use mio::{Interest, Token};
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::{self, Shutdown, SocketAddr};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket};
use std::sync::{mpsc::channel, Arc, Barrier};
use std::thread;
use std::time::Duration;
#[macro_use]
mod util;
#[cfg(not(target_os = "windows"))]
use util::init;
use util::{
any_local_address, any_local_ipv6_address, assert_send, assert_socket_close_on_exec,
assert_socket_non_blocking, assert_sync, assert_would_block, expect_events, expect_no_events,
init_with_poll, ExpectEvent, Readiness,
};
const DATA1: &[u8] = b"Hello world!";
const DATA2: &[u8] = b"Hello mars!";
// TODO: replace with `DATA1.len()` once `const_slice_len` is stable.
const DATA1_LEN: usize = 12;
const DATA2_LEN: usize = 11;
const ID1: Token = Token(0);
const ID2: Token = Token(1);
#[test]
fn is_send_and_sync() {
assert_send::<TcpStream>();
assert_sync::<TcpStream>();
}
#[test]
fn tcp_stream_ipv4() {
smoke_test_tcp_stream(any_local_address(), TcpStream::connect);
}
#[test]
fn tcp_stream_ipv6() {
smoke_test_tcp_stream(any_local_ipv6_address(), TcpStream::connect);
}
#[test]
fn tcp_stream_std() {
smoke_test_tcp_stream(any_local_address(), |addr| {
let stream = net::TcpStream::connect(addr).unwrap();
// `std::net::TcpStream`s are blocking by default, so make sure it is
// in non-blocking mode before wrapping in a Mio equivalent.
stream.set_nonblocking(true).unwrap();
Ok(TcpStream::from_std(stream))
});
}
fn smoke_test_tcp_stream<F>(addr: SocketAddr, make_stream: F)
where
F: FnOnce(SocketAddr) -> io::Result<TcpStream>,
{
let (mut poll, mut events) = init_with_poll();
let (handle, addr) = echo_listener(addr, 1);
let mut stream = make_stream(addr).unwrap();
assert_socket_non_blocking(&stream);
assert_socket_close_on_exec(&stream);
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE.add(Interest::READABLE))
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
let mut buf = [0; 16];
assert_would_block(stream.peek(&mut buf));
assert_would_block(stream.read(&mut buf));
// NOTE: the call to `peer_addr` must happen after we received a writable
// event as the stream might not yet be connected.
assert_eq!(stream.peer_addr().unwrap(), addr);
assert!(stream.local_addr().unwrap().ip().is_loopback());
checked_write!(stream.write(&DATA1));
stream.flush().unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::READABLE)],
);
expect_read!(stream.peek(&mut buf), DATA1);
expect_read!(stream.read(&mut buf), DATA1);
assert!(stream.take_error().unwrap().is_none());
assert_would_block(stream.read(&mut buf));
let bufs = [IoSlice::new(&DATA1), IoSlice::new(&DATA2)];
let n = stream
.write_vectored(&bufs)
.expect("unable to write vectored to stream");
assert_eq!(n, DATA1.len() + DATA2.len());
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::READABLE)],
);
let mut buf1 = [1; DATA1_LEN];
let mut buf2 = [2; DATA2_LEN + 1];
let mut bufs = [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)];
let n = stream
.read_vectored(&mut bufs)
.expect("unable to read vectored from stream");
assert_eq!(n, DATA1.len() + DATA2.len());
assert_eq!(&buf1, DATA1);
assert_eq!(&buf2[..DATA2.len()], DATA2);
assert_eq!(buf2[DATA2.len()], 2); // Last byte should be unchanged.
// Close the connection to allow the listener to shutdown.
drop(stream);
handle.join().expect("unable to join thread");
}
#[test]
fn set_get_ttl() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (thread_handle, address) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(address).unwrap();
// on Windows: the stream must be connected before setting the ttl, otherwise
// it is undefined behavior, register and expect a WRITABLE here to make sure
// the stream is connected
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE)
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
// set TTL, get TTL, make sure it has the expected value
const TTL: u32 = 10;
stream.set_ttl(TTL).unwrap();
assert_eq!(stream.ttl().unwrap(), TTL);
assert!(stream.take_error().unwrap().is_none());
barrier.wait();
thread_handle.join().expect("unable to join thread");
}
#[test]
fn get_ttl_without_previous_set() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (thread_handle, address) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(address).unwrap();
// on Windows: the stream must be connected before getting the ttl, otherwise
// it is undefined behavior, register and expect a WRITABLE here to make sure
// the stream is connected
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE)
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
// expect a get TTL to work w/o any previous set_ttl
stream.ttl().expect("unable to get TTL for TCP stream");
assert!(stream.take_error().unwrap().is_none());
barrier.wait();
thread_handle.join().expect("unable to join thread");
}
#[test]
fn set_get_nodelay() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (thread_handle, address) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(address).unwrap();
// on Windows: the stream must be connected before setting the nodelay, otherwise
// it is undefined behavior, register and expect a WRITABLE here to make sure
// the stream is connected
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE)
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
// set nodelay, get nodelay, make sure it has the expected value
const NO_DELAY: bool = true;
stream.set_nodelay(NO_DELAY).unwrap();
assert_eq!(stream.nodelay().unwrap(), NO_DELAY);
assert!(stream.take_error().unwrap().is_none());
barrier.wait();
thread_handle.join().expect("unable to join thread");
}
#[test]
fn get_nodelay_without_previous_set() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (thread_handle, address) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(address).unwrap();
// on Windows: the stream must be connected before setting the nodelay, otherwise
// it is undefined behavior, register and expect a WRITABLE here to make sure
// the stream is connected
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE)
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
// expect a get nodelay to work w/o any previous set nodelay
stream
.nodelay()
.expect("Unable to get nodelay for TCP stream");
assert!(stream.take_error().unwrap().is_none());
barrier.wait();
thread_handle.join().expect("unable to join thread");
}
#[test]
fn shutdown_read() {
let (mut poll, mut events) = init_with_poll();
let (thread_handle, address) = echo_listener(any_local_address(), 1);
let mut stream = TcpStream::connect(address).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE.add(Interest::READABLE))
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
checked_write!(stream.write(&DATA2));
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::READABLE)],
);
stream.shutdown(Shutdown::Read).unwrap();
// Shutting down the reading side is different on each platform. For example
// on Linux based systems we can still read.
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
{
let mut buf = [0; 20];
expect_read!(stream.read(&mut buf), &[]);
}
drop(stream);
thread_handle.join().expect("unable to join thread");
}
#[test]
#[ignore = "This test is flaky, it doesn't always receive an event after shutting down the write side"]
fn shutdown_write() {
let (mut poll, mut events) = init_with_poll();
let (thread_handle, address) = echo_listener(any_local_address(), 1);
let mut stream = TcpStream::connect(address).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE.add(Interest::READABLE))
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
checked_write!(stream.write(&DATA1));
stream.shutdown(Shutdown::Write).unwrap();
let err = stream.write(DATA2).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::BrokenPipe);
// FIXME: we don't always receive the following event.
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::READABLE)],
);
// Read should be ok.
let mut buf = [0; 20];
expect_read!(stream.read(&mut buf), DATA1);
drop(stream);
thread_handle.join().expect("unable to join thread");
}
#[test]
fn shutdown_both() {
let (mut poll, mut events) = init_with_poll();
let (thread_handle, address) = echo_listener(any_local_address(), 1);
let mut stream = TcpStream::connect(address).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE.add(Interest::READABLE))
.expect("unable to register TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
checked_write!(stream.write(&DATA1));
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::READABLE)],
);
stream.shutdown(Shutdown::Both).unwrap();
// Shutting down the reading side is different on each platform. For example
// on Linux based systems we can still read.
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
{
let mut buf = [0; 20];
expect_read!(stream.read(&mut buf), &[]);
}
let err = stream.write(DATA2).unwrap_err();
#[cfg(unix)]
assert_eq!(err.kind(), io::ErrorKind::BrokenPipe);
#[cfg(windows)]
assert_eq!(err.kind(), io::ErrorKind::ConnectionAborted);
drop(stream);
thread_handle.join().expect("unable to join thread");
}
#[cfg(unix)]
#[test]
fn raw_fd() {
init();
let (thread_handle, address) = start_listener(1, None, false);
let stream = TcpStream::connect(address).unwrap();
let address = stream.local_addr().unwrap();
let raw_fd1 = stream.as_raw_fd();
let raw_fd2 = stream.into_raw_fd();
assert_eq!(raw_fd1, raw_fd2);
let stream = unsafe { TcpStream::from_raw_fd(raw_fd2) };
assert_eq!(stream.as_raw_fd(), raw_fd1);
assert_eq!(stream.local_addr().unwrap(), address);
thread_handle.join().expect("unable to join thread");
}
#[test]
fn registering() {
let (mut poll, mut events) = init_with_poll();
let (thread_handle, address) = echo_listener(any_local_address(), 1);
let mut stream = TcpStream::connect(address).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::READABLE)
.expect("unable to register TCP stream");
expect_no_events(&mut poll, &mut events);
// NOTE: more tests are done in the smoke tests above.
drop(stream);
thread_handle.join().expect("unable to join thread");
}
#[test]
fn reregistering() {
let (mut poll, mut events) = init_with_poll();
let (thread_handle, address) = echo_listener(any_local_address(), 1);
let mut stream = TcpStream::connect(address).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::READABLE)
.expect("unable to register TCP stream");
poll.registry()
.reregister(&mut stream, ID2, Interest::WRITABLE)
.expect("unable to reregister TCP stream");
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID2, Interest::WRITABLE)],
);
assert_eq!(stream.peer_addr().unwrap(), address);
drop(stream);
thread_handle.join().expect("unable to join thread");
}
#[test]
fn no_events_after_deregister() {
let (mut poll, mut events) = init_with_poll();
let (thread_handle, address) = echo_listener(any_local_address(), 1);
let mut stream = TcpStream::connect(address).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::WRITABLE.add(Interest::READABLE))
.expect("unable to register TCP stream");
poll.registry()
.deregister(&mut stream)
.expect("unable to deregister TCP stream");
expect_no_events(&mut poll, &mut events);
// We do expect to be connected.
assert_eq!(stream.peer_addr().unwrap(), address);
// Also, write should work
let mut buf = [0; 16];
assert_would_block(stream.peek(&mut buf));
assert_would_block(stream.read(&mut buf));
checked_write!(stream.write(&DATA1));
stream.flush().unwrap();
expect_no_events(&mut poll, &mut events);
drop(stream);
thread_handle.join().expect("unable to join thread");
}
#[test]
#[cfg_attr(
windows,
ignore = "fails on Windows; client read closed events are not triggered"
)]
fn tcp_shutdown_client_read_close_event() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (handle, sockaddr) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(sockaddr).unwrap();
let interests = Interest::READABLE | Interest::WRITABLE;
poll.registry()
.register(&mut stream, ID1, interests)
.unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
stream.shutdown(Shutdown::Read).unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Readiness::READ_CLOSED)],
);
barrier.wait();
handle.join().expect("failed to join thread");
}
#[test]
#[cfg_attr(windows, ignore = "fails; client write_closed events are not found")]
#[cfg_attr(
any(
target_os = "android",
target_os = "illumos",
target_os = "linux",
target_os = "solaris"
),
ignore = "fails; client write_closed events are not found"
)]
fn tcp_shutdown_client_write_close_event() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (handle, sockaddr) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(sockaddr).unwrap();
let interests = Interest::READABLE | Interest::WRITABLE;
poll.registry()
.register(&mut stream, ID1, interests)
.unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
stream.shutdown(Shutdown::Write).unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Readiness::WRITE_CLOSED)],
);
barrier.wait();
handle.join().expect("failed to join thread");
}
#[test]
fn tcp_shutdown_server_write_close_event() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (handle, sockaddr) = start_listener(1, Some(barrier.clone()), true);
let mut stream = TcpStream::connect(sockaddr).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::READABLE.add(Interest::WRITABLE))
.unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
barrier.wait();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Readiness::READ_CLOSED)],
);
barrier.wait();
handle.join().expect("failed to join thread");
}
#[test]
fn tcp_reset_close_event() {
let (mut poll, mut events) = init_with_poll();
let listener = net::TcpListener::bind(any_local_address()).unwrap();
let sockaddr = listener.local_addr().unwrap();
let mut stream = TcpStream::connect(sockaddr).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::READABLE.add(Interest::WRITABLE))
.unwrap();
let server_stream = listener.accept().unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
checked_write!(stream.write(DATA1));
// Try to read something.
assert_would_block(stream.read(&mut [0]));
// Server goes away.
drop(server_stream);
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Readiness::READ_CLOSED)],
);
// Make sure we quiesce. `expect_no_events` seems to flake sometimes on mac/freebsd.
loop {
poll.poll(&mut events, Some(Duration::from_millis(100)))
.expect("poll failed");
if events.iter().count() == 0 {
break;
}
}
}
#[test]
#[cfg_attr(
windows,
ignore = "fails on Windows; client close events are not found"
)]
fn tcp_shutdown_client_both_close_event() {
let (mut poll, mut events) = init_with_poll();
let barrier = Arc::new(Barrier::new(2));
let (handle, sockaddr) = start_listener(1, Some(barrier.clone()), false);
let mut stream = TcpStream::connect(sockaddr).unwrap();
poll.registry()
.register(&mut stream, ID1, Interest::READABLE.add(Interest::WRITABLE))
.unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Interest::WRITABLE)],
);
stream.shutdown(Shutdown::Both).unwrap();
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(ID1, Readiness::WRITE_CLOSED)],
);
barrier.wait();
handle.join().expect("failed to join thread");
}
/// Start a listener that accepts `n_connections` connections on the returned
/// address. It echos back any data it reads from the connection before
/// accepting another one.
fn echo_listener(addr: SocketAddr, n_connections: usize) -> (thread::JoinHandle<()>, SocketAddr) {
let (sender, receiver) = channel();
let thread_handle = thread::spawn(move || {
let listener = net::TcpListener::bind(addr).unwrap();
let local_address = listener.local_addr().unwrap();
sender.send(local_address).unwrap();
let mut buf = [0; 128];
for _ in 0..n_connections {
let (mut stream, _) = listener.accept().unwrap();
loop {
let n = stream
.read(&mut buf)
// On Linux based system it will cause a connection reset
// error when the reading side of the peer connection is
// shutdown, we don't consider it an actual here.
.or_else(|err| match err {
ref err if err.kind() == io::ErrorKind::ConnectionReset => Ok(0),
err => Err(err),
})
.expect("error reading");
if n == 0 {
break;
}
checked_write!(stream.write(&buf[..n]));
}
}
});
(thread_handle, receiver.recv().unwrap())
}
/// Start a listener that accepts `n_connections` connections on the returned
/// address. If a barrier is provided it will wait on it before closing the
/// connection.
fn start_listener(
n_connections: usize,
barrier: Option<Arc<Barrier>>,
shutdown_write: bool,
) -> (thread::JoinHandle<()>, SocketAddr) {
let (sender, receiver) = channel();
let thread_handle = thread::spawn(move || {
let listener = net::TcpListener::bind(any_local_address()).unwrap();
let local_address = listener.local_addr().unwrap();
sender.send(local_address).unwrap();
for _ in 0..n_connections {
let (stream, _) = listener.accept().unwrap();
if let Some(ref barrier) = barrier {
barrier.wait();
if shutdown_write {
stream.shutdown(Shutdown::Write).unwrap();
barrier.wait();
}
}
drop(stream);
}
});
(thread_handle, receiver.recv().unwrap())
}
#[test]
fn hup_event_on_disconnect() {
use mio::net::TcpListener;
let (mut poll, mut events) = init_with_poll();
let addr = "127.0.0.1:0".parse().unwrap();
let mut listener = TcpListener::bind(addr).unwrap();
let addr = listener.local_addr().unwrap();
poll.registry()
.register(&mut listener, Token(0), Interest::READABLE)
.unwrap();
let mut stream = TcpStream::connect(addr).unwrap();
poll.registry()
.register(
&mut stream,
Token(1),
Interest::READABLE | Interest::WRITABLE,
)
.unwrap();
expect_events(
&mut poll,
&mut events,
vec![
ExpectEvent::new(Token(0), Interest::READABLE),
ExpectEvent::new(Token(1), Interest::WRITABLE),
],
);
let (sock, _) = listener.accept().unwrap();
// Prevent the OS from performing a graceful shutdown
set_linger_zero(&sock);
drop(sock);
expect_events(
&mut poll,
&mut events,
vec![ExpectEvent::new(Token(1), Interest::READABLE)],
);
}
fn set_linger_zero(socket: &TcpStream) {
use mio::net::TcpSocket;
#[cfg(windows)]
let s = unsafe { TcpSocket::from_raw_socket(socket.as_raw_socket()) };
#[cfg(unix)]
let s = unsafe { TcpSocket::from_raw_fd(socket.as_raw_fd()) };
s.set_linger(Some(Duration::from_millis(0))).unwrap();
std::mem::drop(s);
}