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 | 13 |
14 void test1(int totalConnections) { | 14 void test1(int totalConnections) { |
15 // Server which just closes immediately. | 15 // Server which just closes immediately. |
16 HttpServer.bind("127.0.0.1", 0).then((server) { | 16 HttpServer.bind("127.0.0.1", 0).then((server) { |
17 server.listen((HttpRequest request) { | 17 server.listen((HttpRequest request) { |
18 request.response.close(); | 18 request.response.close(); |
19 }); | 19 }); |
20 | 20 |
21 int count = 0; | 21 int count = 0; |
22 HttpClient client = new HttpClient(); | 22 HttpClient client = new HttpClient(); |
23 for (int i = 0; i < totalConnections; i++) { | 23 for (int i = 0; i < totalConnections; i++) { |
24 client.get("127.0.0.1", server.port, "/") | 24 client |
25 .then((HttpClientRequest request) => request.close()) | 25 .get("127.0.0.1", server.port, "/") |
26 .then((HttpClientResponse response) { | 26 .then((HttpClientRequest request) => request.close()) |
27 response.listen((_) {}, onDone: () { | 27 .then((HttpClientResponse response) { |
28 count++; | 28 response.listen((_) {}, onDone: () { |
29 if (count == totalConnections) { | 29 count++; |
30 client.close(); | 30 if (count == totalConnections) { |
31 server.close(); | 31 client.close(); |
32 } | 32 server.close(); |
33 }); | 33 } |
34 }); | 34 }); |
| 35 }); |
35 } | 36 } |
36 }); | 37 }); |
37 } | 38 } |
38 | 39 |
39 | |
40 void test2(int totalConnections, int outputStreamWrites) { | 40 void test2(int totalConnections, int outputStreamWrites) { |
41 // Server which responds without waiting for request body. | 41 // Server which responds without waiting for request body. |
42 HttpServer.bind("127.0.0.1", 0).then((server) { | 42 HttpServer.bind("127.0.0.1", 0).then((server) { |
43 server.listen((HttpRequest request) { | 43 server.listen((HttpRequest request) { |
44 request.response.write("!dlrow ,olleH"); | 44 request.response.write("!dlrow ,olleH"); |
45 request.response.close(); | 45 request.response.close(); |
46 }); | 46 }); |
47 | 47 |
48 int count = 0; | 48 int count = 0; |
49 HttpClient client = new HttpClient(); | 49 HttpClient client = new HttpClient(); |
50 for (int i = 0; i < totalConnections; i++) { | 50 for (int i = 0; i < totalConnections; i++) { |
51 client.get("127.0.0.1", server.port, "/") | 51 client |
52 .then((HttpClientRequest request) { | 52 .get("127.0.0.1", server.port, "/") |
53 request.contentLength = -1; | 53 .then((HttpClientRequest request) { |
54 for (int i = 0; i < outputStreamWrites; i++) { | 54 request.contentLength = -1; |
55 request.write("Hello, world!"); | 55 for (int i = 0; i < outputStreamWrites; i++) { |
56 } | 56 request.write("Hello, world!"); |
57 request.done.catchError((_) {}); | 57 } |
58 return request.close(); | 58 request.done.catchError((_) {}); |
59 }) | 59 return request.close(); |
60 .then((HttpClientResponse response) { | 60 }).then((HttpClientResponse response) { |
61 response.listen( | 61 response.listen((_) {}, onDone: () { |
62 (_) {}, | |
63 onDone: () { | |
64 count++; | |
65 if (count == totalConnections) { | |
66 client.close(force: true); | |
67 server.close(); | |
68 } | |
69 }, | |
70 onError: (e) {} /* ignore */); | |
71 }) | |
72 .catchError((error) { | |
73 count++; | 62 count++; |
74 if (count == totalConnections) { | 63 if (count == totalConnections) { |
75 client.close(); | 64 client.close(force: true); |
76 server.close(); | 65 server.close(); |
77 } | 66 } |
78 }); | 67 }, onError: (e) {} /* ignore */); |
| 68 }).catchError((error) { |
| 69 count++; |
| 70 if (count == totalConnections) { |
| 71 client.close(); |
| 72 server.close(); |
| 73 } |
| 74 }); |
79 } | 75 } |
80 }); | 76 }); |
81 } | 77 } |
82 | 78 |
83 | |
84 void test3(int totalConnections) { | 79 void test3(int totalConnections) { |
85 // Server which responds when request body has been received. | 80 // Server which responds when request body has been received. |
86 HttpServer.bind("127.0.0.1", 0).then((server) { | 81 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 82 server.listen((HttpRequest request) { |
| 83 request.listen((_) {}, onDone: () { |
| 84 request.response.write("!dlrow ,olleH"); |
| 85 request.response.close(); |
| 86 }); |
| 87 }); |
87 | 88 |
88 server.listen((HttpRequest request) { | 89 int count = 0; |
89 request.listen((_) {}, onDone: () { | 90 HttpClient client = new HttpClient(); |
90 request.response.write("!dlrow ,olleH"); | 91 for (int i = 0; i < totalConnections; i++) { |
91 request.response.close(); | 92 client |
92 }); | 93 .get("127.0.0.1", server.port, "/") |
93 }); | 94 .then((HttpClientRequest request) { |
94 | |
95 int count = 0; | |
96 HttpClient client = new HttpClient(); | |
97 for (int i = 0; i < totalConnections; i++) { | |
98 client.get("127.0.0.1", server.port, "/") | |
99 .then((HttpClientRequest request) { | |
100 request.contentLength = -1; | 95 request.contentLength = -1; |
101 request.write("Hello, world!"); | 96 request.write("Hello, world!"); |
102 return request.close(); | 97 return request.close(); |
103 }) | 98 }).then((HttpClientResponse response) { |
104 .then((HttpClientResponse response) { | |
105 response.listen((_) {}, onDone: () { | 99 response.listen((_) {}, onDone: () { |
106 count++; | 100 count++; |
107 if (count == totalConnections) { | 101 if (count == totalConnections) { |
108 client.close(); | 102 client.close(); |
109 server.close(); | 103 server.close(); |
110 } | 104 } |
111 }); | 105 }); |
112 }); | 106 }); |
113 } | 107 } |
114 | |
115 }); | 108 }); |
116 } | 109 } |
117 | 110 |
118 | |
119 void test4() { | 111 void test4() { |
120 HttpServer.bind("127.0.0.1", 0).then((server) { | 112 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 113 server.listen((var request) { |
| 114 request.listen((_) {}, onDone: () { |
| 115 new Timer.periodic(new Duration(milliseconds: 100), (timer) { |
| 116 if (server.connectionsInfo().total == 0) { |
| 117 server.close(); |
| 118 timer.cancel(); |
| 119 } |
| 120 }); |
| 121 request.response.close(); |
| 122 }); |
| 123 }); |
121 | 124 |
122 server.listen((var request) { | 125 var client = new HttpClient(); |
123 request.listen((_) {}, onDone: () { | 126 client |
124 new Timer.periodic(new Duration(milliseconds: 100), (timer) { | 127 .get("127.0.0.1", server.port, "/") |
125 if (server.connectionsInfo().total == 0) { | 128 .then((request) => request.close()) |
126 server.close(); | 129 .then((response) { |
127 timer.cancel(); | |
128 } | |
129 }); | |
130 request.response.close(); | |
131 }); | |
132 }); | |
133 | |
134 var client= new HttpClient(); | |
135 client.get("127.0.0.1", server.port, "/") | |
136 .then((request) => request.close()) | |
137 .then((response) { | |
138 response.listen((_) {}, onDone: () { | 130 response.listen((_) {}, onDone: () { |
139 client.close(); | 131 client.close(); |
140 }); | 132 }); |
141 }); | 133 }); |
142 | |
143 }); | 134 }); |
144 } | 135 } |
145 | 136 |
146 | |
147 void test5(int totalConnections) { | 137 void test5(int totalConnections) { |
148 HttpServer.bind("127.0.0.1", 0).then((server) { | 138 HttpServer.bind("127.0.0.1", 0).then((server) { |
149 server.listen( | 139 server.listen((request) { |
150 (request) { | 140 request.listen((_) {}, onDone: () { |
151 request.listen( | 141 request.response.close(); |
152 (_) { }, | 142 request.response.done.catchError((e) {}); |
153 onDone: () { | 143 }, onError: (error) {}); |
154 request.response.close(); | 144 }, onError: (error) {}); |
155 request.response.done.catchError((e) {}); | |
156 }, | |
157 onError: (error) { }); | |
158 }, | |
159 onError: (error) { }); | |
160 | 145 |
161 // Create a number of client requests and keep then active. Then | 146 // Create a number of client requests and keep then active. Then |
162 // close the client and wait for the server to lose all active | 147 // close the client and wait for the server to lose all active |
163 // connections. | 148 // connections. |
164 var client= new HttpClient(); | 149 var client = new HttpClient(); |
165 client.maxConnectionsPerHost = totalConnections; | 150 client.maxConnectionsPerHost = totalConnections; |
166 for (int i = 0; i < totalConnections; i++) { | 151 for (int i = 0; i < totalConnections; i++) { |
167 client.post("127.0.0.1", server.port, "/") | 152 client |
168 .then((request) { | 153 .post("127.0.0.1", server.port, "/") |
| 154 .then((request) { |
169 request.add([0]); | 155 request.add([0]); |
170 // TODO(sgjesse): Make this test work with | 156 // TODO(sgjesse): Make this test work with |
171 //request.response instead of request.close() return | 157 //request.response instead of request.close() return |
172 //return request.response; | 158 //return request.response; |
173 request.done.catchError((e) {}); | 159 request.done.catchError((e) {}); |
174 return request.close(); | 160 return request.close(); |
175 }) | 161 }) |
176 .then((response) { }) | 162 .then((response) {}) |
177 .catchError((e) { }, test: (e) => e is HttpException); | 163 .catchError((e) {}, test: (e) => e is HttpException); |
178 } | 164 } |
179 bool clientClosed = false; | 165 bool clientClosed = false; |
180 new Timer.periodic(new Duration(milliseconds: 100), (timer) { | 166 new Timer.periodic(new Duration(milliseconds: 100), (timer) { |
181 if (!clientClosed) { | 167 if (!clientClosed) { |
182 if (server.connectionsInfo().total == totalConnections) { | 168 if (server.connectionsInfo().total == totalConnections) { |
183 clientClosed = true; | 169 clientClosed = true; |
184 client.close(force: true); | 170 client.close(force: true); |
185 } | 171 } |
186 } else { | 172 } else { |
187 if (server.connectionsInfo().total == 0) { | 173 if (server.connectionsInfo().total == 0) { |
188 server.close(); | 174 server.close(); |
189 timer.cancel(); | 175 timer.cancel(); |
190 } | 176 } |
191 } | 177 } |
192 }); | 178 }); |
193 }); | 179 }); |
194 } | 180 } |
195 | 181 |
196 | |
197 void main() { | 182 void main() { |
198 test1(1); | 183 test1(1); |
199 test1(10); | 184 test1(10); |
200 test2(1, 10); | 185 test2(1, 10); |
201 test2(10, 10); | 186 test2(10, 10); |
202 test2(10, 1000); | 187 test2(10, 1000); |
203 test3(1); | 188 test3(1); |
204 test3(10); | 189 test3(10); |
205 test4(); | 190 test4(); |
206 test5(1); | 191 test5(1); |
207 test5(10); | 192 test5(10); |
208 } | 193 } |
OLD | NEW |