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