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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 }); | 59 }); |
60 }); | 60 }); |
61 } | 61 } |
62 | 62 |
63 Future testBindDifferentAddresses(InternetAddress addr1, | 63 Future testBindDifferentAddresses(InternetAddress addr1, |
64 InternetAddress addr2, | 64 InternetAddress addr2, |
65 bool addr1V6Only, | 65 bool addr1V6Only, |
66 bool addr2V6Only) async { | 66 bool addr2V6Only) async { |
67 int freePort = await freeIPv4AndIPv6Port(); | 67 int freePort = await freeIPv4AndIPv6Port(); |
68 | 68 |
69 asyncStart(); | 69 var socket = await ServerSocket.bind( |
70 return ServerSocket.bind( | 70 addr1, freePort, v6Only: addr1V6Only, shared: false); |
71 addr1, freePort, v6Only: addr1V6Only, shared: false).then((socket) { | 71 |
| 72 try { |
72 Expect.isTrue(socket.port > 0); | 73 Expect.isTrue(socket.port > 0); |
73 | 74 |
74 asyncStart(); | 75 var socket2 = await ServerSocket.bind( |
75 return ServerSocket.bind( | 76 addr2, freePort, v6Only: addr2V6Only, shared: false); |
76 addr2, freePort, v6Only: addr2V6Only, shared: false).then((socket2) { | 77 try { |
77 Expect.equals(socket.port, socket2.port); | 78 Expect.equals(socket.port, socket2.port); |
78 | 79 } finally { |
79 return Future.wait([ | 80 await socket2.close(); |
80 socket.close().whenComplete(asyncEnd), | 81 } |
81 socket2.close().whenComplete(asyncEnd), | 82 } finally { |
82 ]); | 83 await socket.close(); |
83 }); | 84 } |
84 }); | |
85 } | 85 } |
86 | 86 |
87 testListenCloseListenClose(String host) async { | 87 testListenCloseListenClose(String host) async { |
88 asyncStart(); | 88 asyncStart(); |
89 | 89 |
90 ServerSocket socket = | 90 ServerSocket socket = |
91 await ServerSocket.bind(host, 0, shared: true); | 91 await ServerSocket.bind(host, 0, shared: true); |
92 ServerSocket socket2 = | 92 ServerSocket socket2 = |
93 await ServerSocket.bind(host, socket.port, shared: true); | 93 await ServerSocket.bind(host, socket.port, shared: true); |
94 | 94 |
(...skipping 18 matching lines...) Expand all Loading... |
113 } | 113 } |
114 | 114 |
115 Future<int> freeIPv4AndIPv6Port() async { | 115 Future<int> freeIPv4AndIPv6Port() async { |
116 var socket = | 116 var socket = |
117 await ServerSocket.bind(InternetAddress.ANY_IP_V6, 0, v6Only: false); | 117 await ServerSocket.bind(InternetAddress.ANY_IP_V6, 0, v6Only: false); |
118 int port = socket.port; | 118 int port = socket.port; |
119 await socket.close(); | 119 await socket.close(); |
120 return port; | 120 return port; |
121 } | 121 } |
122 | 122 |
| 123 Future retry(Future fun(), {int maxCount: 10}) async { |
| 124 for (int i = 0; i < maxCount; i++) { |
| 125 try { |
| 126 // If there is no exception this will simply return, otherwise we keep |
| 127 // trying. |
| 128 return await fun(); |
| 129 } catch (_) {} |
| 130 print("Failed to execute test closure in attempt $i " |
| 131 "(${maxCount - i} retries left)."); |
| 132 } |
| 133 return await fun(); |
| 134 } |
| 135 |
123 main() async { | 136 main() async { |
124 asyncStart(); | 137 asyncStart(); |
125 await testBindDifferentAddresses(InternetAddress.ANY_IP_V6, | 138 |
126 InternetAddress.ANY_IP_V4, | 139 await retry(() async { |
127 true, | 140 await testBindDifferentAddresses(InternetAddress.ANY_IP_V6, |
128 false); | 141 InternetAddress.ANY_IP_V4, |
129 await testBindDifferentAddresses(InternetAddress.ANY_IP_V4, | 142 true, |
130 InternetAddress.ANY_IP_V6, | 143 false); |
131 false, | 144 }); |
132 true); | 145 await retry(() async { |
| 146 await testBindDifferentAddresses(InternetAddress.ANY_IP_V4, |
| 147 InternetAddress.ANY_IP_V6, |
| 148 false, |
| 149 true); |
| 150 }); |
133 | 151 |
134 for (var host in ['127.0.0.1', '::1']) { | 152 for (var host in ['127.0.0.1', '::1']) { |
135 testBindShared(host, false); | 153 testBindShared(host, false); |
136 testBindShared(host, true); | 154 testBindShared(host, true); |
137 | 155 |
138 negTestBindSharedMismatch(host, false); | 156 negTestBindSharedMismatch(host, false); |
139 negTestBindSharedMismatch(host, true); | 157 negTestBindSharedMismatch(host, true); |
140 | 158 |
141 negTestBindV6OnlyMismatch(host, true); | 159 negTestBindV6OnlyMismatch(host, true); |
142 negTestBindV6OnlyMismatch(host, false); | 160 negTestBindV6OnlyMismatch(host, false); |
143 | 161 |
144 testListenCloseListenClose(host); | 162 testListenCloseListenClose(host); |
145 } | 163 } |
146 asyncEnd(); | 164 asyncEnd(); |
147 } | 165 } |
OLD | NEW |