OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 // Dart test program for testing that isolates can spawn other isolates and | |
6 // that the nested isolates can communicate with the main once the spawner has | |
7 // disappeared. | |
8 | |
9 #library('NestedSpawn2Test'); | |
10 #import("dart:isolate"); | |
11 #import('../../../lib/unittest/unittest.dart'); | |
12 | |
13 class IsolateA extends Isolate { | |
14 IsolateA() : super.heavy(); | |
15 | |
16 void main() { | |
17 this.port.receive((msg, replyTo) { | |
18 Expect.equals("launch nested!", msg); | |
19 new IsolateB().spawn().then((SendPort p) { | |
20 p.send(replyTo, null); | |
21 this.port.close(); | |
22 }); | |
23 }); | |
24 } | |
25 } | |
26 | |
27 String msg0 = "0 there?"; | |
28 String msg1 = "1 Yes."; | |
29 String msg2 = "2 great. Think the other one is already dead?"; | |
30 String msg3 = "3 Give him some time."; | |
31 String msg4 = "4 now?"; | |
32 String msg5 = "5 Now."; | |
33 String msg6 = "6 Great. Bye"; | |
34 | |
35 void _call(SendPort p, msg, void onreceive(m, replyTo)) { | |
36 final replyTo = new ReceivePort(); | |
37 p.send(msg, replyTo.toSendPort()); | |
38 replyTo.receive((m, r) { | |
39 replyTo.close(); | |
40 onreceive(m, r); | |
41 }); | |
42 } | |
43 | |
44 class IsolateB extends Isolate { | |
45 IsolateB() : super.heavy(); | |
46 | |
47 void main() { | |
48 this.port.receive((mainPort, replyTo) { | |
49 this.port.close(); | |
50 // Do a little ping-pong dance to give the intermediate isolate time to | |
51 // die. | |
52 _call(mainPort, msg0, ((msg, replyTo) { | |
53 Expect.equals("1", msg[0]); | |
54 _call(replyTo, msg2, ((msg, replyTo) { | |
55 Expect.equals("3", msg[0]); | |
56 _call(replyTo, msg4, ((msg, replyTo) { | |
57 Expect.equals("5", msg[0]); | |
58 replyTo.send(msg6, null); | |
59 })); | |
60 })); | |
61 })); | |
62 }); | |
63 } | |
64 } | |
65 | |
66 main() { | |
67 test("spawned isolate can spawn other isolates", () { | |
68 new IsolateA().spawn().then(expectAsync1((SendPort port) { | |
69 _call(port, "launch nested!", expectAsync2((msg, replyTo) { | |
70 Expect.equals("0", msg[0]); | |
71 _call(replyTo, msg1, expectAsync2((msg, replyTo) { | |
72 Expect.equals("2", msg[0]); | |
73 _call(replyTo, msg3, expectAsync2((msg, replyTo) { | |
74 Expect.equals("4", msg[0]); | |
75 _call(replyTo, msg5, expectAsync2((msg, replyTo) { | |
76 Expect.equals("6", msg[0]); | |
77 })); | |
78 })); | |
79 })); | |
80 })); | |
81 })); | |
82 }); | |
83 } | |
OLD | NEW |