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 response.drain().then((_) { |
| 48 Expect.fail("Expected error"); |
| 49 }, onError: (e) { |
| 50 // Expect error. |
| 51 }); |
| 52 })); |
| 53 } |
| 54 Future.wait(futures).then((_) => server.close()); |
| 55 }); |
| 56 } |
| 57 |
| 58 void testDeadlineAndDetach(int connections) { |
| 59 HttpServer.bind('localhost', 0).then((server) { |
| 60 server.listen((request) { |
| 61 request.response.deadline = const Duration(milliseconds: 0); |
| 62 request.response.contentLength = 5; |
| 63 request.response.detachSocket().then((socket) { |
| 64 new Timer(const Duration(milliseconds: 100), () { |
| 65 socket.write('stuff'); |
| 66 socket.close(); |
| 67 }); |
| 68 }); |
| 69 }); |
| 70 |
| 71 var futures = []; |
| 72 var client = new HttpClient(); |
| 73 for (int i = 0; i < connections; i++) { |
| 74 futures.add(client.get('localhost', server.port, '/') |
| 75 .then((request) => request.close()) |
| 76 .then((response) { |
| 77 return response.fold(new BytesBuilder(), (b, d) => b..add(d)) |
| 78 .then((builder) { |
| 79 Expect.equals('stuff', |
| 80 new String.fromCharCodes(builder.takeBytes())); |
| 81 }); |
| 82 })); |
| 83 } |
| 84 Future.wait(futures).then((_) => server.close()); |
| 85 }); |
| 86 } |
| 87 |
| 88 void main() { |
| 89 testSimpleDeadline(10); |
| 90 testExceedDeadline(10); |
| 91 testDeadlineAndDetach(10); |
| 92 } |
| 93 |
OLD | NEW |