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"], | 29 Expect.listEquals(["alive", "control"], |
30 result.where((x) => x is String).toList(), | 30 result.where((x) => x is String).toList(), "control events"); |
31 "control events"); | |
32 Expect.listEquals([3, 2, 1, 0], | 31 Expect.listEquals([3, 2, 1, 0], |
33 result.where((x) => x is int).toList(), | 32 result.where((x) => x is int).toList(), "data events"); |
34 "data events"); | 33 Expect.isTrue( |
35 Expect.isTrue(result.indexOf("alive") < result.indexOf(2), | 34 result.indexOf("alive") < result.indexOf(2), "alive index < 2"); |
36 "alive index < 2"); | |
37 Expect.isTrue(result.indexOf("control") < result.indexOf(1), | 35 Expect.isTrue(result.indexOf("control") < result.indexOf(1), |
38 "control index < 1"); | 36 "control index < 1"); |
39 reply.close(); | 37 reply.close(); |
40 asyncEnd(); | 38 asyncEnd(); |
41 } | 39 } |
42 }; | 40 }; |
43 var pingPort = new RawReceivePort(); | 41 var pingPort = new RawReceivePort(); |
44 int pingCount = 0; | 42 int pingCount = 0; |
45 pingPort.handler = (response) { | 43 pingPort.handler = (response) { |
46 result.add(response); | 44 result.add(response); |
47 pingCount++; | 45 pingCount++; |
48 if (pingCount == 2) pingPort.close(); | 46 if (pingCount == 2) pingPort.close(); |
49 }; | 47 }; |
50 ping(message, priority) { | 48 ping(message, priority) { |
51 isolate.ping(pingPort.sendPort, response: message, priority: priority); | 49 isolate.ping(pingPort.sendPort, response: message, priority: priority); |
52 } | 50 } |
| 51 |
53 echoPort.send(3); | 52 echoPort.send(3); |
54 ping("alive", Isolate.IMMEDIATE); | 53 ping("alive", Isolate.IMMEDIATE); |
55 echoPort.send(2); | 54 echoPort.send(2); |
56 ping("control", Isolate.BEFORE_NEXT_EVENT); | 55 ping("control", Isolate.BEFORE_NEXT_EVENT); |
57 echoPort.send(1); | 56 echoPort.send(1); |
58 echoPort.send(0); | 57 echoPort.send(0); |
59 }); | 58 }); |
60 }); | 59 }); |
61 } | 60 } |
OLD | NEW |