OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:io'; | |
7 | |
8 | |
9 void testOneRequest(int connections) { | |
10 HttpServer.bind('127.0.0.1', 0).then((server) { | |
11 server.listen((request) => request.response.close()); | |
12 var client = new HttpClient(); | |
13 var futures = []; | |
14 for (int i = 0; i < connections; i++) { | |
15 futures.add( | |
16 client.get('127.0.0.1', server.port, '/') | |
17 .then((request) => request.close()) | |
18 .then((response) => response.fold(null, (x, y) {}))); | |
19 } | |
20 Future.wait(futures).then((_) { | |
21 new Timer.periodic(const Duration(milliseconds: 100), (timer) { | |
22 if (server.connectionsInfo().total == 0) { | |
23 timer.cancel(); | |
24 server.close(); | |
25 } | |
26 }); | |
27 }); | |
28 }); | |
29 } | |
30 | |
31 | |
32 void testIdleTimeout(int timeout) { | |
33 HttpServer.bind('127.0.0.1', 0).then((server1) { | |
34 HttpServer.bind('127.0.0.1', 0).then((server2) { | |
35 server1.listen((request) => request.pipe(request.response)); | |
36 server2.listen((request) => request.pipe(request.response)); | |
37 | |
38 var client = new HttpClient(); | |
39 client.idleTimeout = new Duration(milliseconds: timeout); | |
40 | |
41 // Create a 'slow' connection.. | |
42 Future connect(int port) { | |
43 return client.post('127.0.0.1', port, '/') | |
44 .then((request) { | |
45 request.write("data"); | |
46 new Timer(const Duration(milliseconds: 250), () { | |
47 request.close(); | |
48 }); | |
49 return request.done; | |
50 }) | |
51 .then((response) { | |
52 return response.fold(null, (x, y) {}); | |
53 }); | |
54 } | |
55 | |
56 // Create a single, slow request, to server1. | |
57 connect(server1.port); | |
58 | |
59 // Create a repeating connection to server2. | |
60 run() { | |
61 connect(server2.port).then((_) { | |
62 if (server1.connectionsInfo().total == 0) { | |
63 server1.close(); | |
64 server2.close(); | |
65 return; | |
66 } | |
67 Timer.run(run); | |
68 }); | |
69 } | |
70 run(); | |
71 }); | |
72 }); | |
73 } | |
74 | |
75 | |
76 main() { | |
77 testOneRequest(1); | |
78 testOneRequest(5); | |
79 testOneRequest(20); | |
80 testIdleTimeout(0); | |
81 testIdleTimeout(100); | |
82 testIdleTimeout(500); | |
83 testIdleTimeout(1000); | |
84 testIdleTimeout(2000); | |
85 } | |
OLD | NEW |