OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source is governed by a | 2 // for details. All rights reserved. Use of this source 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 import "dart:isolate"; | 5 import "dart:isolate"; |
6 import "dart:async"; | 6 import "dart:async"; |
7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
8 | 8 |
9 void isomain(SendPort replyPort) { | 9 void isomain(SendPort replyPort) { |
10 RawReceivePort port = new RawReceivePort(); | 10 RawReceivePort port = new RawReceivePort(); |
11 port.handler = (v) { | 11 port.handler = (v) { |
12 if (v == 0) { | 12 if (v == 0) { |
13 // Shut down when receiving the 0 message. | 13 // Shut down when receiving the 0 message. |
14 port.close(); | 14 port.close(); |
15 } else { | 15 } else { |
16 replyPort.send(v); | 16 replyPort.send(v); |
17 } | 17 } |
18 }; | 18 }; |
19 replyPort.send(port.sendPort); | 19 replyPort.send(port.sendPort); |
20 } | 20 } |
21 | 21 |
22 | 22 |
23 void main() { | 23 void main() { |
24 testExit(); | 24 testExit(); |
25 testCancelExit(); | 25 testCancelExit(); |
| 26 testOverrideResponse(); |
26 } | 27 } |
27 | 28 |
28 void testExit() { | 29 void testExit() { |
29 bool mayComplete = false; | 30 bool mayComplete = false; |
30 asyncStart(); | 31 asyncStart(); |
31 var completer = new Completer(); // Completed by first reply from isolate. | 32 var completer = new Completer(); // Completed by first reply from isolate. |
32 RawReceivePort reply = new RawReceivePort(completer.complete); | 33 RawReceivePort reply = new RawReceivePort(completer.complete); |
33 RawReceivePort onExitPort; | 34 RawReceivePort onExitPort; |
34 onExitPort = new RawReceivePort((_) { | 35 onExitPort = new RawReceivePort((v) { |
| 36 if (v != "RESPONSE") throw "WRONG RESPONSE: $v"; |
35 reply.close(); | 37 reply.close(); |
36 onExitPort.close(); | 38 onExitPort.close(); |
37 if (!mayComplete) throw "COMPLETED EARLY"; | 39 if (!mayComplete) throw "COMPLETED EARLY"; |
38 asyncEnd(); | 40 asyncEnd(); |
39 }); | 41 }); |
40 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { | 42 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
41 isolate.addOnExitListener(onExitPort.sendPort); | 43 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE"); |
42 return completer.future; | 44 return completer.future; |
43 }).then((echoPort) { | 45 }).then((echoPort) { |
44 int counter = 4; | 46 int counter = 4; |
45 reply.handler = (v) { | 47 reply.handler = (v) { |
46 if (v != counter) throw "WRONG REPLY"; | 48 if (v != counter) throw "WRONG REPLY"; |
47 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; | 49 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
48 counter--; | 50 counter--; |
49 mayComplete = (counter == 0); | 51 mayComplete = (counter == 0); |
50 echoPort.send(counter); | 52 echoPort.send(counter); |
51 }; | 53 }; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 if (counter == 1) { | 87 if (counter == 1) { |
86 // Remove listener 2, keep listener 1. | 88 // Remove listener 2, keep listener 1. |
87 isolate.removeOnExitListener(onExitPort2.sendPort); | 89 isolate.removeOnExitListener(onExitPort2.sendPort); |
88 } | 90 } |
89 echoPort.send(counter); | 91 echoPort.send(counter); |
90 }; | 92 }; |
91 echoPort.send(counter); | 93 echoPort.send(counter); |
92 }); | 94 }); |
93 }); | 95 }); |
94 } | 96 } |
| 97 |
| 98 void testOverrideResponse() { |
| 99 bool mayComplete = false; |
| 100 asyncStart(); |
| 101 var completer = new Completer(); // Completed by first reply from isolate. |
| 102 RawReceivePort reply = new RawReceivePort(completer.complete); |
| 103 RawReceivePort onExitPort; |
| 104 onExitPort = new RawReceivePort((v) { |
| 105 if (v != "RESPONSE2") throw "WRONG RESPONSE: $v"; |
| 106 reply.close(); |
| 107 onExitPort.close(); |
| 108 if (!mayComplete) throw "COMPLETED EARLY"; |
| 109 asyncEnd(); |
| 110 }); |
| 111 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
| 112 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE"); |
| 113 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE2"); |
| 114 return completer.future; |
| 115 }).then((echoPort) { |
| 116 int counter = 4; |
| 117 reply.handler = (v) { |
| 118 if (v != counter) throw "WRONG REPLY"; |
| 119 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
| 120 counter--; |
| 121 mayComplete = (counter == 0); |
| 122 echoPort.send(counter); |
| 123 }; |
| 124 echoPort.send(counter); |
| 125 }); |
| 126 } |
OLD | NEW |