OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "dart:isolate"; |
| 6 import "dart:async"; |
| 7 import "package:async_helper/async_helper.dart"; |
| 8 |
| 9 void isomain(SendPort replyPort) { |
| 10 RawReceivePort port = new RawReceivePort(); |
| 11 port.handler = (v) { |
| 12 if (v == 0) { |
| 13 // Shut down when receiving the 0 message. |
| 14 port.close(); |
| 15 } else { |
| 16 replyPort.send(v); |
| 17 } |
| 18 }; |
| 19 replyPort.send(port.sendPort); |
| 20 } |
| 21 |
| 22 |
| 23 void main() { |
| 24 testExit(); |
| 25 testCancelExit(); |
| 26 } |
| 27 |
| 28 void testExit() { |
| 29 bool mayComplete = false; |
| 30 asyncStart(); |
| 31 var completer = new Completer(); // Completed by first reply from isolate. |
| 32 RawReceivePort reply = new RawReceivePort(completer.complete); |
| 33 RawReceivePort onExitPort; |
| 34 onExitPort = new RawReceivePort((_) { |
| 35 reply.close(); |
| 36 onExitPort.close(); |
| 37 if (!mayComplete) throw "COMPLETED EARLY"; |
| 38 asyncEnd(); |
| 39 }); |
| 40 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
| 41 isolate.addOnExitListener(onExitPort.sendPort); |
| 42 return completer.future; |
| 43 }).then((echoPort) { |
| 44 int counter = 4; |
| 45 reply.handler = (v) { |
| 46 if (v != counter) throw "WRONG REPLY"; |
| 47 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
| 48 counter--; |
| 49 mayComplete = (counter == 0); |
| 50 echoPort.send(counter); |
| 51 }; |
| 52 echoPort.send(counter); |
| 53 }); |
| 54 } |
| 55 |
| 56 |
| 57 void testCancelExit() { |
| 58 bool mayComplete = false; |
| 59 asyncStart(); |
| 60 var completer = new Completer(); // Completed by first reply from isolate. |
| 61 RawReceivePort reply = new RawReceivePort(completer.complete); |
| 62 RawReceivePort onExitPort2 = new RawReceivePort((_) { |
| 63 throw "RECEIVED EXIT MESSAGE"; |
| 64 }); |
| 65 RawReceivePort onExitPort1; |
| 66 onExitPort1 = new RawReceivePort((_) { |
| 67 reply.close(); |
| 68 onExitPort1.close(); |
| 69 if (!mayComplete) throw "COMPLETED EARLY"; |
| 70 new Timer(const Duration(milliseconds: 0), () { |
| 71 onExitPort2.close(); |
| 72 asyncEnd(); |
| 73 }); |
| 74 }); |
| 75 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
| 76 isolate.addOnExitListener(onExitPort2.sendPort); |
| 77 isolate.addOnExitListener(onExitPort1.sendPort); |
| 78 return completer.future.then((echoPort) { |
| 79 int counter = 4; |
| 80 reply.handler = (v) { |
| 81 if (v != counter) throw "WRONG REPLY"; |
| 82 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
| 83 counter--; |
| 84 mayComplete = (counter == 0); |
| 85 if (counter == 1) { |
| 86 // Remove listener 2, keep listener 1. |
| 87 isolate.removeOnExitListener(onExitPort2.sendPort); |
| 88 } |
| 89 echoPort.send(counter); |
| 90 }; |
| 91 echoPort.send(counter); |
| 92 }); |
| 93 }); |
| 94 } |
OLD | NEW |