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 "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import "dart:async"; | 11 import "dart:async"; |
12 import "dart:io"; | 12 import "dart:io"; |
13 import "dart:isolate"; | 13 import "dart:isolate"; |
14 | 14 |
| 15 const SERVER_ADDRESS = "127.0.0.1"; |
15 const HOST_NAME = "localhost"; | 16 const HOST_NAME = "localhost"; |
16 const CERTIFICATE = "localhost_cert"; | 17 const CERTIFICATE = "localhost_cert"; |
17 | 18 |
18 void testCloseOneEnd(String toClose) { | 19 void testCloseOneEnd(String toClose) { |
19 ReceivePort port = new ReceivePort(); | 20 ReceivePort port = new ReceivePort(); |
20 Completer serverDone = new Completer(); | 21 Completer serverDone = new Completer(); |
21 Completer serverEndDone = new Completer(); | 22 Completer serverEndDone = new Completer(); |
22 Completer clientEndDone = new Completer(); | 23 Completer clientEndDone = new Completer(); |
23 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) | 24 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) |
24 .then((_) { | 25 .then((_) { |
25 port.close(); | 26 port.close(); |
26 }); | 27 }); |
27 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { | 28 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
28 server.listen((serverConnection) { | 29 server.listen((serverConnection) { |
29 serverConnection.listen((event) { | 30 serverConnection.listen((event) { |
30 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { | 31 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { |
31 serverConnection.shutdown(SocketDirection.SEND); | 32 serverConnection.shutdown(SocketDirection.SEND); |
32 } | 33 } |
33 }, | 34 }, |
34 onDone: () { | 35 onDone: () { |
35 serverEndDone.complete(null); | 36 serverEndDone.complete(null); |
36 }); | 37 }); |
37 }, | 38 }, |
38 onDone: () { | 39 onDone: () { |
39 serverDone.complete(null); | 40 serverDone.complete(null); |
40 }); | 41 }); |
41 RawSecureSocket.connect(HOST_NAME, server.port).then((clientConnection) { | 42 RawSecureSocket.connect(HOST_NAME, server.port).then((clientConnection) { |
42 clientConnection.listen((event){ | 43 clientConnection.listen((event){ |
43 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { | 44 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { |
44 clientConnection.shutdown(SocketDirection.SEND); | 45 clientConnection.shutdown(SocketDirection.SEND); |
45 } | 46 } |
46 }, | 47 }, |
47 onDone: () { | 48 onDone: () { |
48 clientEndDone.complete(null); | 49 clientEndDone.complete(null); |
49 server.close(); | 50 server.close(); |
50 }); | 51 }); |
51 }); | 52 }); |
52 }); | 53 }); |
53 } | 54 } |
54 | 55 |
55 void testCloseBothEnds() { | 56 void testCloseBothEnds() { |
56 ReceivePort port = new ReceivePort(); | 57 ReceivePort port = new ReceivePort(); |
57 RawSecureServerSocket.bind(HOST_NAME, 0, 5, CERTIFICATE).then((server) { | 58 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 5, CERTIFICATE).then((server) { |
58 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); | 59 var clientEndFuture = RawSecureSocket.connect(HOST_NAME, server.port); |
59 server.listen((serverEnd) { | 60 server.listen((serverEnd) { |
60 clientEndFuture.then((clientEnd) { | 61 clientEndFuture.then((clientEnd) { |
61 clientEnd.close(); | 62 clientEnd.close(); |
62 serverEnd.close(); | 63 serverEnd.close(); |
63 server.close(); | 64 server.close(); |
64 port.close(); | 65 port.close(); |
65 }); | 66 }); |
66 }); | 67 }); |
67 }); | 68 }); |
68 } | 69 } |
69 | 70 |
70 testPauseServerSocket() { | 71 testPauseServerSocket() { |
71 const int socketCount = 10; | 72 const int socketCount = 10; |
72 var acceptCount = 0; | 73 var acceptCount = 0; |
73 var resumed = false; | 74 var resumed = false; |
74 | 75 |
75 ReceivePort port = new ReceivePort(); | 76 ReceivePort port = new ReceivePort(); |
76 | 77 |
77 RawSecureServerSocket.bind(HOST_NAME, | 78 RawSecureServerSocket.bind(SERVER_ADDRESS, |
78 0, | 79 0, |
79 2 * socketCount, | 80 2 * socketCount, |
80 CERTIFICATE).then((server) { | 81 CERTIFICATE).then((server) { |
81 Expect.isTrue(server.port > 0); | 82 Expect.isTrue(server.port > 0); |
82 var subscription; | 83 var subscription; |
83 subscription = server.listen((connection) { | 84 subscription = server.listen((connection) { |
84 Expect.isTrue(resumed); | 85 Expect.isTrue(resumed); |
85 connection.shutdown(SocketDirection.SEND); | 86 connection.shutdown(SocketDirection.SEND); |
86 if (++acceptCount == 2 * socketCount) { | 87 if (++acceptCount == 2 * socketCount) { |
87 server.close(); | 88 server.close(); |
(...skipping 21 matching lines...) Expand all Loading... |
109 } | 110 } |
110 }); | 111 }); |
111 }); | 112 }); |
112 } | 113 } |
113 | 114 |
114 testCloseServer() { | 115 testCloseServer() { |
115 const int socketCount = 3; | 116 const int socketCount = 3; |
116 ReceivePort port = new ReceivePort(); | 117 ReceivePort port = new ReceivePort(); |
117 List ends = []; | 118 List ends = []; |
118 | 119 |
119 RawSecureServerSocket.bind(HOST_NAME, 0, 15, CERTIFICATE).then((server) { | 120 RawSecureServerSocket.bind(SERVER_ADDRESS, 0, 15, CERTIFICATE).then((server) { |
120 Expect.isTrue(server.port > 0); | 121 Expect.isTrue(server.port > 0); |
121 void checkDone() { | 122 void checkDone() { |
122 if (ends.length < 2 * socketCount) return; | 123 if (ends.length < 2 * socketCount) return; |
123 for (var end in ends) { | 124 for (var end in ends) { |
124 end.close(); | 125 end.close(); |
125 } | 126 } |
126 server.close(); | 127 server.close(); |
127 port.close(); | 128 port.close(); |
128 } | 129 } |
129 | 130 |
(...skipping 20 matching lines...) Expand all Loading... |
150 useBuiltinRoots: false); | 151 useBuiltinRoots: false); |
151 | 152 |
152 testCloseOneEnd("client"); | 153 testCloseOneEnd("client"); |
153 testCloseOneEnd("server"); | 154 testCloseOneEnd("server"); |
154 testCloseBothEnds(); | 155 testCloseBothEnds(); |
155 testCloseServer(); | 156 testCloseServer(); |
156 testPauseServerSocket(); | 157 testPauseServerSocket(); |
157 // TODO(whesse): Add testPauseSocket from raw_socket_test.dart. | 158 // TODO(whesse): Add testPauseSocket from raw_socket_test.dart. |
158 // TODO(whesse): Add testCancelResubscribeSocket from raw_socket_test.dart. | 159 // TODO(whesse): Add testCancelResubscribeSocket from raw_socket_test.dart. |
159 } | 160 } |
OLD | NEW |