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