Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, 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 // This test checks that a shutdown(SocketDirection.SEND) of a socket, | |
| 6 // when the other end is already closed, does not discard unread data | |
| 7 // that remains in the connection. | |
| 8 | |
| 9 import "dart:io"; | |
| 10 import "dart:async"; | |
| 11 import "package:expect/expect.dart"; | |
| 12 | |
| 13 RawServerSocket server; | |
| 14 RawSocket client; | |
| 15 Duration delay = new Duration(seconds: 1); | |
| 16 | |
| 17 void serverListen(RawSocket serverSide) { | |
| 18 var data = new List.generate(200, (i) => i % 20 + 65); | |
| 19 var offset = 0; | |
| 20 void serveData(RawSocketEvent event) { | |
| 21 if (event == RawSocketEvent.WRITE) { | |
| 22 while (offset < data.length) { | |
| 23 var written = serverSide.write(data, offset); | |
| 24 offset += written; | |
| 25 if (written == 0) { | |
| 26 serverSide.writeEventsEnabled = true; | |
| 27 return; | |
| 28 } | |
| 29 } | |
| 30 serverSide.close(); | |
| 31 server.close(); | |
| 32 } | |
| 33 } | |
| 34 serverSide.listen(serveData); | |
| 35 } | |
| 36 | |
| 37 | |
| 38 void clientListen(RawSocketEvent event) { | |
| 39 if (event == RawSocketEvent.READ) { | |
| 40 client.readEventsEnabled = false; | |
| 41 new Future.delayed(delay, () { | |
| 42 var data = client.read(100); | |
| 43 if (data == null) { | |
| 44 // If there is no data ready to read, wait until there is data | |
| 45 // that can be read, before running the rest of the test. | |
| 46 client.readEventsEnabled = true; | |
| 47 return; | |
| 48 } | |
| 49 client.shutdown(SocketDirection.SEND); | |
| 50 data = client.read(100); | |
| 51 Expect.isNotNull(data); | |
| 52 client.close(); | |
| 53 }); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 | |
| 58 test() async { | |
| 59 server = await RawServerSocket.bind("localhost", 0); | |
|
Søren Gjesse
2015/03/04 13:28:15
'localhost' -> InternetAddress.IP_V4_LOOPBACK
Bill Hesse
2015/03/04 14:36:42
Done.
| |
| 60 server.listen(serverListen); | |
| 61 client = await RawSocket.connect("localhost", server.port); | |
| 62 client.listen(clientListen); | |
| 63 } | |
| 64 | |
| 65 | |
| 66 void main() { | |
| 67 test(); | |
| 68 } | |
| OLD | NEW |