1#![warn(rust_2018_idioms)]
2#![cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi does not support bind
3
4use tokio::net::TcpListener;
5use tokio::runtime;
6use tokio_test::{assert_err, assert_pending, assert_ready, task};
7
8#[test]
9fn tcp_doesnt_block() {
10 let rt = rt();
11
12 let listener = {
13 let _enter = rt.enter();
14 let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
15 TcpListener::from_std(listener).unwrap()
16 };
17
18 drop(rt);
19
20 let mut task = task::spawn(async move {
21 assert_err!(listener.accept().await);
22 });
23
24 assert_ready!(task.poll());
25}
26
27#[test]
28fn drop_wakes() {
29 let rt = rt();
30
31 let listener = {
32 let _enter = rt.enter();
33 let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
34 TcpListener::from_std(listener).unwrap()
35 };
36
37 let mut task = task::spawn(async move {
38 assert_err!(listener.accept().await);
39 });
40
41 assert_pending!(task.poll());
42
43 drop(rt);
44
45 assert!(task.is_woken());
46 assert_ready!(task.poll());
47}
48
49fn rt() -> runtime::Runtime {
50 runtime::Builder::new_current_thread()
51 .enable_all()
52 .build()
53 .unwrap()
54}
55