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