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 // VMOptions= | |
6 // VMOptions=--short_socket_read | |
7 // VMOptions=--short_socket_write | |
8 // VMOptions=--short_socket_read --short_socket_write | |
9 | |
10 import "package:expect/expect.dart"; | |
11 import "dart:async"; | |
12 import "dart:io"; | |
13 | |
14 void testSimpleDeadline(int connections) { | |
15 HttpServer.bind('localhost', 0).then((server) { | |
16 server.listen((request) { | |
17 request.response.deadline = const Duration(seconds: 1); | |
18 request.response.write("stuff"); | |
19 request.response.close(); | |
20 }); | |
21 | |
22 var futures = []; | |
23 var client = new HttpClient(); | |
24 for (int i = 0; i < connections; i++) { | |
25 futures.add(client.get('localhost', server.port, '/') | |
26 .then((request) => request.close()) | |
27 .then((response) => response.drain())); | |
28 } | |
29 Future.wait(futures).then((_) => server.close()); | |
30 }); | |
31 } | |
32 | |
33 void testExceedDeadline(int connections) { | |
34 HttpServer.bind('localhost', 0).then((server) { | |
35 server.listen((request) { | |
36 request.response.deadline = const Duration(milliseconds: 100); | |
37 request.response.contentLength = 10000; | |
38 request.response.write("stuff"); | |
39 }); | |
40 | |
41 var futures = []; | |
42 var client = new HttpClient(); | |
43 for (int i = 0; i < connections; i++) { | |
44 futures.add(client.get('localhost', server.port, '/') | |
45 .then((request) => request.close()) | |
46 .then((response) { | |
47 return new Future.delayed(const Duration(milliseconds: 500), () { | |
Søren Gjesse
2013/07/24 12:29:51
Even though we are in the same VM timing issues co
Anders Johnsen
2013/07/24 12:46:23
Reworked the timers a bit.
| |
48 return response.drain().then((_) { | |
49 Expect.fail("Expected error"); | |
50 }, onError: (e) { | |
51 // Expect error. | |
52 }); | |
53 }); | |
54 })); | |
55 } | |
56 Future.wait(futures).then((_) => server.close()); | |
57 }); | |
58 } | |
59 | |
60 void testDeadlineAndDetach(int connections) { | |
61 HttpServer.bind('localhost', 0).then((server) { | |
62 server.listen((request) { | |
63 request.response.deadline = const Duration(milliseconds: 100); | |
64 request.response.contentLength = 5; | |
65 request.response.detachSocket().then((socket) { | |
66 new Timer(const Duration(milliseconds: 500), () { | |
67 socket.write('stuff'); | |
68 socket.close(); | |
69 }); | |
70 }); | |
71 }); | |
72 | |
73 var futures = []; | |
74 var client = new HttpClient(); | |
75 for (int i = 0; i < connections; i++) { | |
76 futures.add(client.get('localhost', server.port, '/') | |
77 .then((request) => request.close()) | |
78 .then((response) { | |
79 return response.fold(new BytesBuilder(), (b, d) => b..add(d)) | |
80 .then((builder) { | |
81 Expect.equals('stuff', | |
82 new String.fromCharCodes(builder.takeBytes())); | |
83 }); | |
84 })); | |
85 } | |
86 Future.wait(futures).then((_) => server.close()); | |
87 }); | |
88 } | |
89 | |
90 void main() { | |
91 testSimpleDeadline(10); | |
92 testExceedDeadline(10); | |
93 testDeadlineAndDetach(10); | |
94 } | |
95 | |
OLD | NEW |