OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "dart:async"; | 10 import "dart:async"; |
(...skipping 13 matching lines...) Expand all Loading... |
24 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { | 24 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { |
25 Expect.isTrue(s.port > 0); | 25 Expect.isTrue(s.port > 0); |
26 s.close(); | 26 s.close(); |
27 asyncEnd(); | 27 asyncEnd(); |
28 }); | 28 }); |
29 } | 29 } |
30 | 30 |
31 void testInvalidBind() { | 31 void testInvalidBind() { |
32 // Bind to a unknown DNS name. | 32 // Bind to a unknown DNS name. |
33 asyncStart(); | 33 asyncStart(); |
34 ServerSocket.bind("ko.faar.__hest__", 0) | 34 ServerSocket.bind("ko.faar.__hest__", 0).then((_) { |
35 .then((_) { Expect.fail("Failure expected"); } ) | 35 Expect.fail("Failure expected"); |
36 .catchError((error) { | 36 }).catchError((error) { |
37 Expect.isTrue(error is SocketException); | 37 Expect.isTrue(error is SocketException); |
38 asyncEnd(); | 38 asyncEnd(); |
39 }); | 39 }); |
40 | 40 |
41 // Bind to an unavaliable IP-address. | 41 // Bind to an unavaliable IP-address. |
42 asyncStart(); | 42 asyncStart(); |
43 ServerSocket.bind("8.8.8.8", 0) | 43 ServerSocket.bind("8.8.8.8", 0).then((_) { |
44 .then((_) { Expect.fail("Failure expected"); } ) | 44 Expect.fail("Failure expected"); |
45 .catchError((error) { | 45 }).catchError((error) { |
46 Expect.isTrue(error is SocketException); | 46 Expect.isTrue(error is SocketException); |
47 asyncEnd(); | 47 asyncEnd(); |
48 }); | 48 }); |
49 | 49 |
50 // Bind to a port already in use. | 50 // Bind to a port already in use. |
51 asyncStart(); | 51 asyncStart(); |
52 ServerSocket.bind("127.0.0.1", 0) | 52 ServerSocket.bind("127.0.0.1", 0).then((s) { |
53 .then((s) { | 53 ServerSocket.bind("127.0.0.1", s.port).then((t) { |
54 ServerSocket.bind("127.0.0.1", s.port) | 54 Expect.fail("Multiple listens on same port"); |
55 .then((t) { | 55 }).catchError((error) { |
56 Expect.fail("Multiple listens on same port"); | 56 Expect.isTrue(error is SocketException); |
57 }) | 57 s.close(); |
58 .catchError((error) { | 58 asyncEnd(); |
59 Expect.isTrue(error is SocketException); | 59 }); |
60 s.close(); | 60 }); |
61 asyncEnd(); | |
62 }); | |
63 }); | |
64 } | 61 } |
65 | 62 |
66 void testConnectImmediateDestroy() { | 63 void testConnectImmediateDestroy() { |
67 asyncStart(); | 64 asyncStart(); |
68 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 65 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
69 server.listen((_) { }); | 66 server.listen((_) {}); |
70 Socket.connect("127.0.0.1", server.port).then((socket) { | 67 Socket.connect("127.0.0.1", server.port).then((socket) { |
71 socket.destroy(); | 68 socket.destroy(); |
72 server.close(); | 69 server.close(); |
73 asyncEnd(); | 70 asyncEnd(); |
74 }); | 71 }); |
75 }); | 72 }); |
76 } | 73 } |
77 | 74 |
78 void testConnectConsumerClose() { | 75 void testConnectConsumerClose() { |
79 // Connect socket then immediate close the consumer without | 76 // Connect socket then immediate close the consumer without |
80 // listening on the stream. | 77 // listening on the stream. |
81 asyncStart(); | 78 asyncStart(); |
82 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 79 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
83 server.listen((_) { }); | 80 server.listen((_) {}); |
84 Socket.connect("127.0.0.1", server.port).then((socket) { | 81 Socket.connect("127.0.0.1", server.port).then((socket) { |
85 socket.close(); | 82 socket.close(); |
86 socket.done.then((_) { | 83 socket.done.then((_) { |
87 socket.destroy(); | 84 socket.destroy(); |
88 server.close(); | 85 server.close(); |
89 asyncEnd(); | 86 asyncEnd(); |
90 }); | 87 }); |
91 }); | 88 }); |
92 }); | 89 }); |
93 } | 90 } |
94 | 91 |
95 void testConnectConsumerWriteClose() { | 92 void testConnectConsumerWriteClose() { |
96 // Connect socket write some data immediate close the consumer | 93 // Connect socket write some data immediate close the consumer |
97 // without listening on the stream. | 94 // without listening on the stream. |
98 asyncStart(); | 95 asyncStart(); |
99 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 96 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
100 server.listen((_) { }); | 97 server.listen((_) {}); |
101 Socket.connect("127.0.0.1", server.port).then((socket) { | 98 Socket.connect("127.0.0.1", server.port).then((socket) { |
102 socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 99 socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
103 socket.close(); | 100 socket.close(); |
104 socket.done.then((_) { | 101 socket.done.then((_) { |
105 socket.destroy(); | 102 socket.destroy(); |
106 server.close(); | 103 server.close(); |
107 asyncEnd(); | 104 asyncEnd(); |
108 }); | 105 }); |
109 }); | 106 }); |
110 }); | 107 }); |
111 } | 108 } |
112 | 109 |
113 void testConnectStreamClose() { | 110 void testConnectStreamClose() { |
114 // Connect socket and listen on the stream. The server closes | 111 // Connect socket and listen on the stream. The server closes |
115 // immediately so only a done event is received. | 112 // immediately so only a done event is received. |
116 asyncStart(); | 113 asyncStart(); |
117 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 114 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
118 server.listen((client) { | 115 server.listen((client) { |
119 client.close(); | 116 client.close(); |
120 client.done.then((_) => client.destroy()); | 117 client.done.then((_) => client.destroy()); |
121 }); | 118 }); |
122 Socket.connect("127.0.0.1", server.port).then((socket) { | 119 Socket.connect("127.0.0.1", server.port).then((socket) { |
123 bool onDoneCalled = false; | 120 bool onDoneCalled = false; |
124 socket.listen((_) { Expect.fail("Unexpected data"); }, | 121 socket.listen((_) { |
125 onDone: () { | 122 Expect.fail("Unexpected data"); |
126 Expect.isFalse(onDoneCalled); | 123 }, onDone: () { |
127 onDoneCalled = true; | 124 Expect.isFalse(onDoneCalled); |
128 socket.close(); | 125 onDoneCalled = true; |
129 server.close(); | 126 socket.close(); |
130 asyncEnd(); | 127 server.close(); |
131 }); | 128 asyncEnd(); |
| 129 }); |
132 }); | 130 }); |
133 }); | 131 }); |
134 } | 132 } |
135 | 133 |
136 void testConnectStreamDataClose(bool useDestroy) { | 134 void testConnectStreamDataClose(bool useDestroy) { |
137 // Connect socket and listen on the stream. The server sends data | 135 // Connect socket and listen on the stream. The server sends data |
138 // and then closes so both data and a done event is received. | 136 // and then closes so both data and a done event is received. |
139 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 137 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
140 asyncStart(); | 138 asyncStart(); |
141 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 139 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
142 server.listen( | 140 server.listen((client) { |
143 (client) { | 141 client.add(sendData); |
144 client.add(sendData); | 142 if (useDestroy) { |
145 if (useDestroy) { | 143 client.destroy(); |
146 client.destroy(); | 144 } else { |
147 } else { | 145 client.close(); |
148 client.close(); | 146 } |
149 } | 147 client.done.then((_) { |
150 client.done.then((_) { if (!useDestroy) { client.destroy(); } }); | 148 if (!useDestroy) { |
151 }); | 149 client.destroy(); |
| 150 } |
| 151 }); |
| 152 }); |
152 Socket.connect("127.0.0.1", server.port).then((socket) { | 153 Socket.connect("127.0.0.1", server.port).then((socket) { |
153 List<int> data = []; | 154 List<int> data = []; |
154 bool onDoneCalled = false; | 155 bool onDoneCalled = false; |
155 socket.listen(data.addAll, | 156 socket.listen(data.addAll, onDone: () { |
156 onDone: () { | 157 Expect.isFalse(onDoneCalled); |
157 Expect.isFalse(onDoneCalled); | 158 onDoneCalled = true; |
158 onDoneCalled = true; | 159 if (!useDestroy) Expect.listEquals(sendData, data); |
159 if (!useDestroy) Expect.listEquals(sendData, data); | 160 socket.add([0]); |
160 socket.add([0]); | 161 socket.close(); |
161 socket.close(); | 162 server.close(); |
162 server.close(); | 163 asyncEnd(); |
163 asyncEnd(); | 164 }); |
164 }); | |
165 }); | 165 }); |
166 }); | 166 }); |
167 } | 167 } |
168 | 168 |
169 void testConnectStreamDataCloseCancel(bool useDestroy) { | 169 void testConnectStreamDataCloseCancel(bool useDestroy) { |
170 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 170 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
171 asyncStart(); | 171 asyncStart(); |
172 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 172 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
173 server.listen( | 173 server.listen((client) { |
174 (client) { | 174 client.add(sendData); |
175 client.add(sendData); | 175 if (useDestroy) { |
176 if (useDestroy) { | 176 client.destroy(); |
177 client.destroy(); | 177 } else { |
178 } else { | 178 client.close(); |
179 client.close(); | 179 } |
180 } | 180 client.done.then((_) { |
181 client.done | 181 if (!useDestroy) client.destroy(); |
182 .then((_) { | 182 }).catchError((e) {/* can happen with short writes */}); |
183 if (!useDestroy) client.destroy(); | 183 }); |
184 }) | |
185 .catchError((e) { /* can happen with short writes */ }); | |
186 }); | |
187 Socket.connect("127.0.0.1", server.port).then((socket) { | 184 Socket.connect("127.0.0.1", server.port).then((socket) { |
188 List<int> data = []; | 185 List<int> data = []; |
189 bool onDoneCalled = false; | 186 bool onDoneCalled = false; |
190 var subscription; | 187 var subscription; |
191 subscription = socket.listen( | 188 subscription = socket.listen((_) { |
192 (_) { | 189 subscription.cancel(); |
193 subscription.cancel(); | 190 socket.close(); |
194 socket.close(); | 191 server.close(); |
195 server.close(); | 192 asyncEnd(); |
196 asyncEnd(); | 193 }, onDone: () { |
197 }, | 194 Expect.fail("Unexpected pipe completion"); |
198 onDone: () { Expect.fail("Unexpected pipe completion"); }); | 195 }); |
199 }); | 196 }); |
200 }); | 197 }); |
201 } | 198 } |
202 | 199 |
203 main() { | 200 main() { |
204 testArguments(); | 201 testArguments(); |
205 testSimpleBind(); | 202 testSimpleBind(); |
206 testInvalidBind(); | 203 testInvalidBind(); |
207 testConnectImmediateDestroy(); | 204 testConnectImmediateDestroy(); |
208 testConnectConsumerClose(); | 205 testConnectConsumerClose(); |
209 testConnectConsumerWriteClose(); | 206 testConnectConsumerWriteClose(); |
210 testConnectStreamClose(); | 207 testConnectStreamClose(); |
211 testConnectStreamDataClose(true); | 208 testConnectStreamDataClose(true); |
212 testConnectStreamDataClose(false); | 209 testConnectStreamDataClose(false); |
213 testConnectStreamDataCloseCancel(true); | 210 testConnectStreamDataCloseCancel(true); |
214 testConnectStreamDataCloseCancel(false); | 211 testConnectStreamDataCloseCancel(false); |
215 } | 212 } |
OLD | NEW |