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