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 | |
23 void main() { | 22 void main() { |
24 testExit(); | 23 testExit(); |
25 testCancelExit(); | 24 testCancelExit(); |
26 testOverrideResponse(); | 25 testOverrideResponse(); |
27 } | 26 } |
28 | 27 |
29 void testExit() { | 28 void testExit() { |
30 bool mayComplete = false; | 29 bool mayComplete = false; |
31 asyncStart(); | 30 asyncStart(); |
32 var completer = new Completer(); // Completed by first reply from isolate. | 31 var completer = new Completer(); // Completed by first reply from isolate. |
33 RawReceivePort reply = new RawReceivePort(completer.complete); | 32 RawReceivePort reply = new RawReceivePort(completer.complete); |
34 RawReceivePort onExitPort; | 33 RawReceivePort onExitPort; |
35 onExitPort = new RawReceivePort((v) { | 34 onExitPort = new RawReceivePort((v) { |
36 if (v != "RESPONSE") throw "WRONG RESPONSE: $v"; | 35 if (v != "RESPONSE") throw "WRONG RESPONSE: $v"; |
37 reply.close(); | 36 reply.close(); |
38 onExitPort.close(); | 37 onExitPort.close(); |
39 if (!mayComplete) throw "COMPLETED EARLY"; | 38 if (!mayComplete) throw "COMPLETED EARLY"; |
40 asyncEnd(); | 39 asyncEnd(); |
41 }); | 40 }); |
42 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { | 41 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
43 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE"); | 42 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE"); |
44 return completer.future; | 43 return completer.future; |
45 }).then((echoPort) { | 44 }).then((echoPort) { |
46 int counter = 4; | 45 int counter = 4; |
47 reply.handler = (v) { | 46 reply.handler = (v) { |
48 if (v != counter) throw "WRONG REPLY"; | 47 if (v != counter) throw "WRONG REPLY"; |
49 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; | 48 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
50 counter--; | 49 counter--; |
51 mayComplete = (counter == 0); | 50 mayComplete = (counter == 0); |
52 echoPort.send(counter); | 51 echoPort.send(counter); |
53 }; | 52 }; |
54 echoPort.send(counter); | 53 echoPort.send(counter); |
55 }); | 54 }); |
56 } | 55 } |
57 | 56 |
58 | |
59 void testCancelExit() { | 57 void testCancelExit() { |
60 bool mayComplete = false; | 58 bool mayComplete = false; |
61 asyncStart(); | 59 asyncStart(); |
62 var completer = new Completer(); // Completed by first reply from isolate. | 60 var completer = new Completer(); // Completed by first reply from isolate. |
63 RawReceivePort reply = new RawReceivePort(completer.complete); | 61 RawReceivePort reply = new RawReceivePort(completer.complete); |
64 RawReceivePort onExitPort2 = new RawReceivePort((_) { | 62 RawReceivePort onExitPort2 = new RawReceivePort((_) { |
65 throw "RECEIVED EXIT MESSAGE"; | 63 throw "RECEIVED EXIT MESSAGE"; |
66 }); | 64 }); |
67 RawReceivePort onExitPort1; | 65 RawReceivePort onExitPort1; |
68 onExitPort1 = new RawReceivePort((_) { | 66 onExitPort1 = new RawReceivePort((_) { |
69 reply.close(); | 67 reply.close(); |
70 onExitPort1.close(); | 68 onExitPort1.close(); |
71 if (!mayComplete) throw "COMPLETED EARLY"; | 69 if (!mayComplete) throw "COMPLETED EARLY"; |
72 new Timer(const Duration(milliseconds: 0), () { | 70 new Timer(const Duration(milliseconds: 0), () { |
(...skipping 18 matching lines...) Expand all Loading... |
91 echoPort.send(counter); | 89 echoPort.send(counter); |
92 }; | 90 }; |
93 echoPort.send(counter); | 91 echoPort.send(counter); |
94 }); | 92 }); |
95 }); | 93 }); |
96 } | 94 } |
97 | 95 |
98 void testOverrideResponse() { | 96 void testOverrideResponse() { |
99 bool mayComplete = false; | 97 bool mayComplete = false; |
100 asyncStart(); | 98 asyncStart(); |
101 var completer = new Completer(); // Completed by first reply from isolate. | 99 var completer = new Completer(); // Completed by first reply from isolate. |
102 RawReceivePort reply = new RawReceivePort(completer.complete); | 100 RawReceivePort reply = new RawReceivePort(completer.complete); |
103 RawReceivePort onExitPort; | 101 RawReceivePort onExitPort; |
104 onExitPort = new RawReceivePort((v) { | 102 onExitPort = new RawReceivePort((v) { |
105 if (v != "RESPONSE2") throw "WRONG RESPONSE: $v"; | 103 if (v != "RESPONSE2") throw "WRONG RESPONSE: $v"; |
106 reply.close(); | 104 reply.close(); |
107 onExitPort.close(); | 105 onExitPort.close(); |
108 if (!mayComplete) throw "COMPLETED EARLY"; | 106 if (!mayComplete) throw "COMPLETED EARLY"; |
109 asyncEnd(); | 107 asyncEnd(); |
110 }); | 108 }); |
111 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { | 109 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
112 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE"); | 110 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE"); |
113 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE2"); | 111 isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE2"); |
114 return completer.future; | 112 return completer.future; |
115 }).then((echoPort) { | 113 }).then((echoPort) { |
116 int counter = 4; | 114 int counter = 4; |
117 reply.handler = (v) { | 115 reply.handler = (v) { |
118 if (v != counter) throw "WRONG REPLY"; | 116 if (v != counter) throw "WRONG REPLY"; |
119 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; | 117 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
120 counter--; | 118 counter--; |
121 mayComplete = (counter == 0); | 119 mayComplete = (counter == 0); |
122 echoPort.send(counter); | 120 echoPort.send(counter); |
123 }; | 121 }; |
124 echoPort.send(counter); | 122 echoPort.send(counter); |
125 }); | 123 }); |
126 } | 124 } |
OLD | NEW |