| 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 import "dart:typed_data"; | 13 import "dart:typed_data"; |
| 14 import "dart:math"; |
| 14 | 15 |
| 15 | 16 |
| 16 void testClientAndServerCloseNoListen(int connections) { | 17 void testClientAndServerCloseNoListen(int connections) { |
| 17 HttpServer.bind("127.0.0.1", 0).then((server) { | 18 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 18 int closed = 0; | 19 int closed = 0; |
| 19 server.listen((request) { | 20 server.listen((request) { |
| 20 request.response.close(); | 21 request.response.close(); |
| 21 request.response.done.then((_) { | 22 request.response.done.then((_) { |
| 22 closed++; | 23 closed++; |
| 23 if (closed == connections) { | 24 if (closed == connections) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 for (int i = 0; i < connections; i++) { | 64 for (int i = 0; i < connections; i++) { |
| 64 client.get("127.0.0.1", server.port, "/") | 65 client.get("127.0.0.1", server.port, "/") |
| 65 .then((request) => request.close()) | 66 .then((request) => request.close()) |
| 66 .then((response) => check()); | 67 .then((response) => check()); |
| 67 } | 68 } |
| 68 }); | 69 }); |
| 69 } | 70 } |
| 70 | 71 |
| 71 | 72 |
| 72 void testClientCloseSendingResponse(int connections) { | 73 void testClientCloseSendingResponse(int connections) { |
| 74 var buffer = new Uint8List(64 * 1024); |
| 75 var rand = new Random(); |
| 76 for (int i = 0; i < buffer.length; i++) { |
| 77 buffer[i] = rand.nextInt(256); |
| 78 } |
| 73 HttpServer.bind("127.0.0.1", 0).then((server) { | 79 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 74 int closed = 0; | 80 int closed = 0; |
| 75 void check() { | 81 void check() { |
| 76 closed++; | 82 closed++; |
| 77 // Wait for both server and client to see the connections as closed. | 83 // Wait for both server and client to see the connections as closed. |
| 78 if (closed == connections * 2) { | 84 if (closed == connections * 2) { |
| 79 Expect.equals(0, server.connectionsInfo().active); | 85 Expect.equals(0, server.connectionsInfo().active); |
| 80 Expect.equals(server.connectionsInfo().total, | 86 Expect.equals(server.connectionsInfo().total, |
| 81 server.connectionsInfo().idle); | 87 server.connectionsInfo().idle); |
| 82 server.close(); | 88 server.close(); |
| 83 } | 89 } |
| 84 } | 90 } |
| 85 server.listen((request) { | 91 server.listen((request) { |
| 86 var timer = new Timer.periodic(const Duration(milliseconds: 20), (_) { | 92 var timer = new Timer.periodic(const Duration(milliseconds: 50), (_) { |
| 87 request.response.add(new Uint8List(16 * 1024)); | 93 request.response.add(buffer); |
| 88 }); | 94 }); |
| 89 request.response.done | 95 request.response.done |
| 90 .catchError((_) {}) | 96 .catchError((_) {}) |
| 91 .whenComplete(() { | 97 .whenComplete(() { |
| 92 check(); | 98 check(); |
| 93 timer.cancel(); | 99 timer.cancel(); |
| 94 }); | 100 }); |
| 95 }); | 101 }); |
| 96 var client = new HttpClient(); | 102 var client = new HttpClient(); |
| 97 for (int i = 0; i < connections; i++) { | 103 for (int i = 0; i < connections; i++) { |
| 98 client.get("127.0.0.1", server.port, "/") | 104 client.get("127.0.0.1", server.port, "/") |
| 99 .then((request) => request.close()) | 105 .then((request) => request.close()) |
| 100 .then((response) { | 106 .then((response) { |
| 101 // Ensure we don't accept the response until we have send the entire | 107 // Ensure we don't accept the response until we have send the entire |
| 102 // request. | 108 // request. |
| 103 var subscription = response.listen((_) {}); | 109 var subscription = response.listen((_) {}); |
| 104 new Timer(const Duration(milliseconds: 200), () { | 110 new Timer(const Duration(milliseconds: 20), () { |
| 105 subscription.cancel(); | 111 subscription.cancel(); |
| 106 check(); | 112 check(); |
| 107 }); | 113 }); |
| 108 }); | 114 }); |
| 109 } | 115 } |
| 110 }); | 116 }); |
| 111 } | 117 } |
| 112 | 118 |
| 113 | 119 |
| 114 void testClientCloseWhileSendingRequest(int connections) { | 120 void testClientCloseWhileSendingRequest(int connections) { |
| 115 HttpServer.bind("127.0.0.1", 0).then((server) { | 121 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 116 int errors = 0; | 122 int errors = 0; |
| 117 server.listen((request) { | 123 server.listen((request) { |
| 118 request.listen((_) {}, onError: (e) { errors++; }); | 124 request.listen((_) {}); |
| 119 }); | 125 }); |
| 120 var client = new HttpClient(); | 126 var client = new HttpClient(); |
| 127 int closed = 0; |
| 121 for (int i = 0; i < connections; i++) { | 128 for (int i = 0; i < connections; i++) { |
| 122 client.post("127.0.0.1", server.port, "/") | 129 client.post("127.0.0.1", server.port, "/") |
| 123 .then((request) { | 130 .then((request) { |
| 124 request.contentLength = 110; | 131 request.contentLength = 110; |
| 125 request.write("0123456789"); | 132 request.write("0123456789"); |
| 126 return request.close(); | 133 return request.close(); |
| 127 }) | 134 }) |
| 128 .catchError((_) {}); | 135 .catchError((_) { |
| 136 closed++; |
| 137 if (closed == connections) { |
| 138 server.close(); |
| 139 } |
| 140 }); |
| 129 } | 141 } |
| 130 new Timer.periodic(const Duration(milliseconds: 100), (t) { | |
| 131 if (errors == connections && server.connectionsInfo().total == 0) { | |
| 132 t.cancel(); | |
| 133 server.close(); | |
| 134 } | |
| 135 }); | |
| 136 }); | 142 }); |
| 137 } | 143 } |
| 138 | 144 |
| 139 | 145 |
| 140 void main() { | 146 void main() { |
| 141 testClientAndServerCloseNoListen(10); | 147 testClientAndServerCloseNoListen(10); |
| 142 testClientCloseServerListen(10); | 148 testClientCloseServerListen(10); |
| 143 testClientCloseSendingResponse(10); | 149 testClientCloseSendingResponse(10); |
| 144 testClientCloseWhileSendingRequest(10); | 150 testClientCloseWhileSendingRequest(10); |
| 145 } | 151 } |
| 146 | 152 |
| OLD | NEW |