| 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:isolate"; | 13 import "dart:isolate"; |
| 14 | 14 |
| 15 Future sendData(List<int> data, int port) { | 15 Future sendData(List<int> data, int port) { |
| 16 return Socket.connect("127.0.0.1", port).then((socket) { | 16 return Socket.connect("127.0.0.1", port).then((socket) { |
| 17 socket.listen((data) { | 17 socket.listen((data) { |
| 18 Expect.fail("No data response was expected"); | 18 Expect.fail("No data response was expected"); |
| 19 }); | 19 }); |
| 20 socket.add(data); | 20 socket.add(data); |
| 21 socket.done.then((_) { | 21 return socket.close() |
| 22 socket.destroy(); | 22 .then((_) { |
| 23 }); | 23 socket.destroy(); |
| 24 return socket.close(); | 24 }); |
| 25 }); | 25 }); |
| 26 } | 26 } |
| 27 | 27 |
| 28 class EarlyCloseTest { | 28 class EarlyCloseTest { |
| 29 EarlyCloseTest(this.data, | 29 EarlyCloseTest(this.data, |
| 30 [String this.exception, | 30 [String this.exception, |
| 31 bool this.expectRequest = false]); | 31 bool this.expectRequest = false]); |
| 32 | 32 |
| 33 Future execute() { | 33 Future execute() { |
| 34 return HttpServer.bind("127.0.0.1", 0).then((server) { | 34 return HttpServer.bind("127.0.0.1", 0).then((server) { |
| 35 Completer c = new Completer(); | 35 Completer c = new Completer(); |
| 36 | 36 |
| 37 bool calledOnRequest = false; | 37 bool calledOnRequest = false; |
| 38 bool calledOnError = false; | 38 bool calledOnError = false; |
| 39 bool calledOnDone = false; | 39 bool calledOnDone = false; |
| 40 ReceivePort port = new ReceivePort(); | 40 ReceivePort port = new ReceivePort(); |
| 41 var requestCompleter = new Completer(); |
| 41 server.listen( | 42 server.listen( |
| 42 (request) { | 43 (request) { |
| 43 Expect.isTrue(expectRequest); | 44 Expect.isTrue(expectRequest); |
| 44 Expect.isFalse(calledOnError); | 45 Expect.isFalse(calledOnError); |
| 45 Expect.isFalse(calledOnRequest, "onRequest called multiple times"); | 46 Expect.isFalse(calledOnRequest, "onRequest called multiple times"); |
| 46 calledOnRequest = true; | 47 calledOnRequest = true; |
| 47 request.listen( | 48 request.listen( |
| 48 (_) {}, | 49 (_) {}, |
| 50 onDone: () { |
| 51 requestCompleter.complete(); |
| 52 }, |
| 49 onError: (error) { | 53 onError: (error) { |
| 50 Expect.isFalse(calledOnError); | 54 Expect.isFalse(calledOnError); |
| 51 Expect.equals(exception, error.message); | 55 Expect.equals(exception, error.message); |
| 52 calledOnError = true; | 56 calledOnError = true; |
| 53 if (exception != null) port.close(); | 57 if (exception != null) port.close(); |
| 54 }); | 58 }); |
| 55 server.close(); | |
| 56 }, | 59 }, |
| 57 onDone: () { | 60 onDone: () { |
| 58 Expect.equals(expectRequest, calledOnRequest); | 61 Expect.equals(expectRequest, calledOnRequest); |
| 59 calledOnDone = true; | 62 calledOnDone = true; |
| 60 if (exception == null) port.close(); | 63 if (exception == null) port.close(); |
| 61 c.complete(null); | 64 c.complete(null); |
| 62 }); | 65 }); |
| 63 | 66 |
| 64 List<int> d; | 67 List<int> d; |
| 65 if (data is List<int>) d = data; | 68 if (data is List<int>) d = data; |
| 66 if (data is String) d = data.codeUnits; | 69 if (data is String) d = data.codeUnits; |
| 67 if (d == null) Expect.fail("Invalid data"); | 70 if (d == null) Expect.fail("Invalid data"); |
| 68 sendData(d, server.port) | 71 sendData(d, server.port) |
| 69 .then((_) { | 72 .then((_) { |
| 70 if (!expectRequest) server.close(); | 73 if (!expectRequest) requestCompleter.complete(); |
| 74 requestCompleter.future.then((_) => server.close()); |
| 71 }); | 75 }); |
| 72 | 76 |
| 73 return c.future; | 77 return c.future; |
| 74 }); | 78 }); |
| 75 } | 79 } |
| 76 | 80 |
| 77 final data; | 81 final data; |
| 78 final String exception; | 82 final String exception; |
| 79 final bool expectRequest; | 83 final bool expectRequest; |
| 80 } | 84 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 socket.done.catchError((_) {}); | 166 socket.done.catchError((_) {}); |
| 163 }); | 167 }); |
| 164 }); | 168 }); |
| 165 } | 169 } |
| 166 | 170 |
| 167 void main() { | 171 void main() { |
| 168 testEarlyClose1(); | 172 testEarlyClose1(); |
| 169 testEarlyClose2(); | 173 testEarlyClose2(); |
| 170 testEarlyClose3(); | 174 testEarlyClose3(); |
| 171 } | 175 } |
| OLD | NEW |