8
8
// at https://www.apache.org/licenses/LICENSE-2.0 and a copy of the MIT license
9
9
// at https://opensource.org/licenses/MIT.
10
10
11
- use async_std:: { net:: { TcpStream , TcpListener } , task} ;
12
11
use crate :: { Config , Connection , ConnectionError , Mode , Control , connection:: State } ;
13
12
use futures:: { future, prelude:: * } ;
14
13
use quickcheck:: { Arbitrary , Gen , QuickCheck , TestResult } ;
15
14
use rand:: Rng ;
16
15
use std:: { fmt:: Debug , io, net:: { Ipv4Addr , SocketAddr , SocketAddrV4 } } ;
16
+ use tokio:: { net:: { TcpStream , TcpListener } , runtime:: Runtime , task} ;
17
+ use tokio_util:: compat:: { Compat , Tokio02AsyncReadCompatExt } ;
17
18
18
19
#[ test]
19
20
fn prop_send_recv ( ) {
20
21
fn prop ( msgs : Vec < Msg > ) -> TestResult {
21
22
if msgs. is_empty ( ) {
22
23
return TestResult :: discard ( )
23
24
}
24
- task:: block_on ( async move {
25
+ let mut rt = Runtime :: new ( ) . unwrap ( ) ;
26
+ rt. block_on ( async move {
25
27
let num_requests = msgs. len ( ) ;
26
28
let iter = msgs. into_iter ( ) . map ( |m| m. 0 ) ;
27
29
28
- let ( listener, address) = bind ( ) . await . expect ( "bind" ) ;
30
+ let ( mut listener, address) = bind ( ) . await . expect ( "bind" ) ;
29
31
30
32
let server = async {
31
- let socket = listener. accept ( ) . await . expect ( "accept" ) . 0 ;
33
+ let socket = listener. accept ( ) . await . expect ( "accept" ) . 0 . compat ( ) ;
32
34
let connection = Connection :: new ( socket, Config :: default ( ) , Mode :: Server ) ;
33
35
repeat_echo ( connection) . await . expect ( "repeat_echo" )
34
36
} ;
35
37
36
38
let client = async {
37
- let socket = TcpStream :: connect ( address) . await . expect ( "connect" ) ;
39
+ let socket = TcpStream :: connect ( address) . await . expect ( "connect" ) . compat ( ) ;
38
40
let connection = Connection :: new ( socket, Config :: default ( ) , Mode :: Client ) ;
39
41
let control = connection. control ( ) ;
40
42
task:: spawn ( crate :: into_stream ( connection) . for_each ( |_| future:: ready ( ( ) ) ) ) ;
@@ -55,19 +57,20 @@ fn prop_max_streams() {
55
57
let mut cfg = Config :: default ( ) ;
56
58
cfg. set_max_num_streams ( max_streams) ;
57
59
58
- task:: block_on ( async move {
59
- let ( listener, address) = bind ( ) . await . expect ( "bind" ) ;
60
+ let mut rt = Runtime :: new ( ) . unwrap ( ) ;
61
+ rt. block_on ( async move {
62
+ let ( mut listener, address) = bind ( ) . await . expect ( "bind" ) ;
60
63
61
64
let cfg_s = cfg. clone ( ) ;
62
65
let server = async move {
63
- let socket = listener. accept ( ) . await . expect ( "accept" ) . 0 ;
66
+ let socket = listener. accept ( ) . await . expect ( "accept" ) . 0 . compat ( ) ;
64
67
let connection = Connection :: new ( socket, cfg_s, Mode :: Server ) ;
65
68
repeat_echo ( connection) . await
66
69
} ;
67
70
68
71
task:: spawn ( server) ;
69
72
70
- let socket = TcpStream :: connect ( address) . await . expect ( "connect" ) ;
73
+ let socket = TcpStream :: connect ( address) . await . expect ( "connect" ) . compat ( ) ;
71
74
let connection = Connection :: new ( socket, cfg, Mode :: Client ) ;
72
75
let mut control = connection. control ( ) ;
73
76
task:: spawn ( crate :: into_stream ( connection) . for_each ( |_| future:: ready ( ( ) ) ) ) ;
@@ -89,12 +92,13 @@ fn prop_max_streams() {
89
92
fn prop_send_recv_half_closed ( ) {
90
93
fn prop ( msg : Msg ) {
91
94
let msg_len = msg. 0 . len ( ) ;
92
- task:: block_on ( async move {
93
- let ( listener, address) = bind ( ) . await . expect ( "bind" ) ;
95
+ let mut rt = Runtime :: new ( ) . unwrap ( ) ;
96
+ rt. block_on ( async move {
97
+ let ( mut listener, address) = bind ( ) . await . expect ( "bind" ) ;
94
98
95
99
// Server should be able to write on a stream shutdown by the client.
96
100
let server = async {
97
- let socket = listener. accept ( ) . await . expect ( "accept" ) . 0 ;
101
+ let socket = listener. accept ( ) . await . expect ( "accept" ) . 0 . compat ( ) ;
98
102
let mut connection = Connection :: new ( socket, Config :: default ( ) , Mode :: Server ) ;
99
103
let mut stream = connection. next_stream ( ) . await
100
104
. expect ( "S: next_stream" )
@@ -108,7 +112,7 @@ fn prop_send_recv_half_closed() {
108
112
109
113
// Client should be able to read after shutting down the stream.
110
114
let client = async {
111
- let socket = TcpStream :: connect ( address) . await . expect ( "connect" ) ;
115
+ let socket = TcpStream :: connect ( address) . await . expect ( "connect" ) . compat ( ) ;
112
116
let connection = Connection :: new ( socket, Config :: default ( ) , Mode :: Client ) ;
113
117
let mut control = connection. control ( ) ;
114
118
task:: spawn ( crate :: into_stream ( connection) . for_each ( |_| future:: ready ( ( ) ) ) ) ;
@@ -150,7 +154,7 @@ async fn bind() -> io::Result<(TcpListener, SocketAddr)> {
150
154
}
151
155
152
156
/// For each incoming stream of `c` echo back to the sender.
153
- async fn repeat_echo ( c : Connection < TcpStream > ) -> Result < ( ) , ConnectionError > {
157
+ async fn repeat_echo ( c : Connection < Compat < TcpStream > > ) -> Result < ( ) , ConnectionError > {
154
158
let c = crate :: into_stream ( c) ;
155
159
c. try_for_each_concurrent ( None , |mut stream| async move {
156
160
{
0 commit comments