Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: tests/standalone/io/raw_server_socket_cancel_test.dart

Issue 19675009: Close 'accepted' sockets when listening socket is closed. Fixes raw_server_socket_cancel_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove unused Socket.close, Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/eventhandler_win.cc ('k') | tests/standalone/standalone.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 void testCancelResubscribeServerSocket(int socketCount, int backlog) { 15 void testCancelResubscribeServerSocket(int socketCount, int backlog) {
16 var acceptCount = 0; 16 var acceptCount = 0;
17 var doneCount = 0; 17 var doneCount = 0;
18 var closeCount = 0;
19 var errorCount = 0; 18 var errorCount = 0;
20 var earlyErrorCount = 0; 19 var earlyErrorCount = 0;
21 20
22 ReceivePort port = new ReceivePort(); 21 ReceivePort port = new ReceivePort();
23 22
24 RawServerSocket.bind("127.0.0.1", 0, backlog: backlog).then((server) { 23 RawServerSocket.bind("127.0.0.1", 0, backlog: backlog).then((server) {
25 Expect.isTrue(server.port > 0); 24 Expect.isTrue(server.port > 0);
26 25
27 void checkDone() { 26 void checkDone() {
28 if (doneCount + earlyErrorCount == socketCount && 27 if (doneCount + errorCount + earlyErrorCount == socketCount) {
29 closeCount + errorCount + earlyErrorCount == socketCount) {
30 port.close(); 28 port.close();
31 } 29 }
32 } 30 }
33 31
34 var subscription; 32 var subscription;
35 subscription = server.listen((client) { 33 subscription = server.listen((client) {
36 client.writeEventsEnabled = false; 34 client.writeEventsEnabled = false;
37 client.listen((event) { 35 client.listen((event) {
38 switch(event) { 36 switch(event) {
39 case RawSocketEvent.READ: 37 case RawSocketEvent.READ:
40 client.read(); 38 client.read();
41 break; 39 break;
42 case RawSocketEvent.READ_CLOSED: 40 case RawSocketEvent.READ_CLOSED:
43 client.shutdown(SocketDirection.SEND); 41 client.shutdown(SocketDirection.SEND);
44 break; 42 break;
45 case RawSocketEvent.WRITE: 43 case RawSocketEvent.WRITE:
46 Expect.fail("No write event expected"); 44 Expect.fail("No write event expected");
47 break; 45 break;
48 } 46 }
49 }); 47 });
50 48
51 if (++acceptCount == socketCount / 2) { 49 if (++acceptCount == socketCount / 2) {
52 // Cancel subscription and then attempt to resubscribe. 50 // Cancel subscription and then attempt to resubscribe.
53 subscription.cancel(); 51 subscription.cancel();
54 Timer.run(() { 52 Timer.run(() {
55 subscription = server.listen((_) { 53 Expect.throws(() {
56 // Server socket is closed on cancel, so no more events. 54 server.listen((_) {
57 Expect.fail("Event after closed through cancel"); 55 // Server socket is closed on cancel, so no more events.
56 Expect.fail("Event after closed through cancel");
57 });
58 }); 58 });
59 }); 59 });
60 } 60 }
61 }); 61 });
62 62
63 // Connect a number of sockets. 63 // Connect a number of sockets.
64 for (int i = 0; i < socketCount; i++) { 64 for (int i = 0; i < socketCount; i++) {
65 RawSocket.connect("127.0.0.1", server.port).then((socket) { 65 RawSocket.connect("127.0.0.1", server.port).then((socket) {
66 bool done = false;
66 var subscription; 67 var subscription;
67 subscription = socket.listen((event) { 68 subscription = socket.listen((event) {
68 switch(event) { 69 switch(event) {
69 case RawSocketEvent.READ: 70 case RawSocketEvent.READ:
70 Expect.fail("No read event expected"); 71 Expect.fail("No read event expected");
71 break; 72 break;
72 case RawSocketEvent.READ_CLOSED: 73 case RawSocketEvent.READ_CLOSED:
73 closeCount++; 74 done = true;
75 doneCount++;
74 checkDone(); 76 checkDone();
75 break; 77 break;
76 case RawSocketEvent.WRITE: 78 case RawSocketEvent.WRITE:
77 // We don't care if this write succeeds, so we don't check 79 // We don't care if this write succeeds, so we don't check
78 // the return value (number of bytes written). 80 // the return value (number of bytes written).
79 socket.write([1,2,3]); 81 socket.write([1,2,3]);
80 socket.shutdown(SocketDirection.SEND); 82 socket.shutdown(SocketDirection.SEND);
81 break; 83 break;
82 } 84 }
83 }, 85 },
84 onDone: () { 86 onDone: () {
85 doneCount++; 87 if (!done) {
86 checkDone(); 88 doneCount++;
89 checkDone();
90 }
87 }, 91 },
88 onError: (e) { 92 onError: (e) {
89 // "Connection reset by peer" errors are handled here. 93 // "Connection reset by peer" errors are handled here.
90 errorCount++; 94 errorCount++;
91 checkDone(); 95 checkDone();
92 }); 96 }, cancelOnError: true);
93 }).catchError((e) { 97 }).catchError((e) {
94 // "Connection actively refused by host" errors are handled here. 98 // "Connection actively refused by host" errors are handled here.
95 earlyErrorCount++; 99 earlyErrorCount++;
96 checkDone(); 100 checkDone();
97 }); 101 });
98 } 102 }
99 }); 103 });
100 } 104 }
101 105
102 void main() { 106 void main() {
103 testCancelResubscribeServerSocket(10, 20); 107 testCancelResubscribeServerSocket(10, 20);
104 testCancelResubscribeServerSocket(20, 5); 108 testCancelResubscribeServerSocket(20, 5);
105 } 109 }
OLDNEW
« no previous file with comments | « runtime/bin/eventhandler_win.cc ('k') | tests/standalone/standalone.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698