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