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