| 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 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 import "dart:isolate"; | 5 import "dart:isolate"; |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
| 9 | 9 |
| 10 isomain1(replyPort) { | 10 isomain1(replyPort) { |
| 11 RawReceivePort port = new RawReceivePort(); | 11 RawReceivePort port = new RawReceivePort(); |
| 12 port.handler = (v) { | 12 port.handler = (v) { |
| 13 replyPort.send(v); | 13 replyPort.send(v); |
| 14 if (v == 0) port.close(); | 14 if (v == 0) port.close(); |
| 15 }; | 15 }; |
| 16 replyPort.send(port.sendPort); | 16 replyPort.send(port.sendPort); |
| 17 } | 17 } |
| 18 | 18 |
| 19 void main(){ | 19 void main(){ |
| 20 asyncStart(); | 20 asyncStart(); |
| 21 var completer = new Completer(); // Completed by first reply from isolate. | 21 var completer = new Completer(); // Completed by first reply from isolate. |
| 22 RawReceivePort reply = new RawReceivePort(completer.complete); | 22 RawReceivePort reply = new RawReceivePort(completer.complete); |
| 23 Isolate.spawn(isomain1, reply.sendPort).then((Isolate isolate) { | 23 Isolate.spawn(isomain1, reply.sendPort).then((Isolate isolate) { |
| 24 List result = []; | 24 List result = []; |
| 25 completer.future.then((echoPort) { | 25 completer.future.then((echoPort) { |
| 26 reply.handler = (v) { | 26 reply.handler = (v) { |
| 27 result.add(v); | 27 result.add(v); |
| 28 if (v == 0) { | 28 if (v == 0) { |
| 29 Expect.listEquals(["alive", "control", "event"], | 29 Expect.listEquals(["alive", "control"], |
| 30 result.where((x) => x is String).toList(), | 30 result.where((x) => x is String).toList(), |
| 31 "control events"); | 31 "control events"); |
| 32 Expect.listEquals([4, 3, 2, 1, 0], | 32 Expect.listEquals([3, 2, 1, 0], |
| 33 result.where((x) => x is int).toList(), | 33 result.where((x) => x is int).toList(), |
| 34 "data events"); | 34 "data events"); |
| 35 Expect.isTrue(result.indexOf("alive") < result.indexOf(3), | 35 Expect.isTrue(result.indexOf("alive") < result.indexOf(2), |
| 36 "alive index < 3"); | 36 "alive index < 2"); |
| 37 Expect.isTrue(result.indexOf("control") < result.indexOf(2), | 37 Expect.isTrue(result.indexOf("control") < result.indexOf(1), |
| 38 "control index < 2"); | 38 "control index < 1"); |
| 39 int eventIndex = result.indexOf("event"); | |
| 40 Expect.isTrue(eventIndex > result.indexOf(2), "event index > 2"); | |
| 41 Expect.isTrue(eventIndex < result.indexOf(1), "event index < 1"); | |
| 42 reply.close(); | 39 reply.close(); |
| 43 asyncEnd(); | 40 asyncEnd(); |
| 44 } | 41 } |
| 45 }; | 42 }; |
| 46 SendPort createPingPort(message) { | 43 var pingPort = new RawReceivePort(); |
| 47 var pingPort = new RawReceivePort(); | 44 int pingCount = 0; |
| 48 pingPort.handler = (_) { | 45 pingPort.handler = (response) { |
| 49 result.add(message); | 46 result.add(response); |
| 50 pingPort.close(); | 47 pingCount++; |
| 51 }; | 48 if (pingCount == 2) pingPort.close(); |
| 52 return pingPort.sendPort; | 49 }; |
| 50 ping(message, priority) { |
| 51 isolate.ping(pingPort.sendPort, response: message, priority: priority); |
| 53 } | 52 } |
| 54 echoPort.send(4); | |
| 55 isolate.ping(createPingPort("alive"), Isolate.IMMEDIATE); | |
| 56 echoPort.send(3); | 53 echoPort.send(3); |
| 57 isolate.ping(createPingPort("control"), Isolate.BEFORE_NEXT_EVENT); | 54 ping("alive", Isolate.IMMEDIATE); |
| 58 echoPort.send(2); | 55 echoPort.send(2); |
| 59 isolate.ping(createPingPort("event"), Isolate.AS_EVENT); | 56 ping("control", Isolate.BEFORE_NEXT_EVENT); |
| 60 echoPort.send(1); | 57 echoPort.send(1); |
| 61 echoPort.send(0); | 58 echoPort.send(0); |
| 62 }); | 59 }); |
| 63 }); | 60 }); |
| 64 } | 61 } |
| OLD | NEW |