| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 } | 25 } |
| 26 | 26 |
| 27 String msg0 = "0 there?"; | 27 String msg0 = "0 there?"; |
| 28 String msg1 = "1 Yes."; | 28 String msg1 = "1 Yes."; |
| 29 String msg2 = "2 great. Think the other one is already dead?"; | 29 String msg2 = "2 great. Think the other one is already dead?"; |
| 30 String msg3 = "3 Give him some time."; | 30 String msg3 = "3 Give him some time."; |
| 31 String msg4 = "4 now?"; | 31 String msg4 = "4 now?"; |
| 32 String msg5 = "5 Now."; | 32 String msg5 = "5 Now."; |
| 33 String msg6 = "6 Great. Bye"; | 33 String msg6 = "6 Great. Bye"; |
| 34 | 34 |
| 35 void _call(SendPort p, msg, void onreceive(m, replyTo)) { |
| 36 final replyTo = new ReceivePort(); |
| 37 p.send(msg, replyTo.toSendPort()); |
| 38 replyTo.receive((m, r) { |
| 39 replyTo.close(); |
| 40 onreceive(m, r); |
| 41 }); |
| 42 } |
| 43 |
| 35 class IsolateB extends Isolate { | 44 class IsolateB extends Isolate { |
| 36 IsolateB() : super.heavy(); | 45 IsolateB() : super.heavy(); |
| 37 | 46 |
| 38 void main() { | 47 void main() { |
| 39 this.port.receive((mainPort, replyTo) { | 48 this.port.receive((mainPort, replyTo) { |
| 40 this.port.close(); | 49 this.port.close(); |
| 41 // Do a little ping-pong dance to give the intermediate isolate time to | 50 // Do a little ping-pong dance to give the intermediate isolate time to |
| 42 // die. | 51 // die. |
| 43 mainPort.call(msg0).receive((msg, replyTo) { | 52 _call(mainPort, msg0, ((msg, replyTo) { |
| 44 Expect.equals("1", msg[0]); | 53 Expect.equals("1", msg[0]); |
| 45 replyTo.call(msg2).receive((msg, replyTo) { | 54 _call(replyTo, msg2, ((msg, replyTo) { |
| 46 Expect.equals("3", msg[0]); | 55 Expect.equals("3", msg[0]); |
| 47 replyTo.call(msg4).receive((msg, replyTo) { | 56 _call(replyTo, msg4, ((msg, replyTo) { |
| 48 Expect.equals("5", msg[0]); | 57 Expect.equals("5", msg[0]); |
| 49 replyTo.send(msg6, null); | 58 replyTo.send(msg6, null); |
| 50 }); | 59 })); |
| 51 }); | 60 })); |
| 52 }); | 61 })); |
| 53 }); | 62 }); |
| 54 } | 63 } |
| 55 } | 64 } |
| 56 | 65 |
| 57 test(TestExpectation expect) { | 66 test(TestExpectation expect) { |
| 58 expect.completes(new IsolateA().spawn()).then((SendPort port) { | 67 expect.completes(new IsolateA().spawn()).then((SendPort port) { |
| 59 port.call("launch nested!").receive(expect.runs2((msg, replyTo) { | 68 _call(port, "launch nested!", expect.runs2((msg, replyTo) { |
| 60 Expect.equals("0", msg[0]); | 69 Expect.equals("0", msg[0]); |
| 61 replyTo.call(msg1).receive(expect.runs2((msg, replyTo) { | 70 _call(replyTo, msg1, expect.runs2((msg, replyTo) { |
| 62 Expect.equals("2", msg[0]); | 71 Expect.equals("2", msg[0]); |
| 63 replyTo.call(msg3).receive(expect.runs2((msg, replyTo) { | 72 _call(replyTo, msg3, expect.runs2((msg, replyTo) { |
| 64 Expect.equals("4", msg[0]); | 73 Expect.equals("4", msg[0]); |
| 65 replyTo.call(msg5).receive(expect.runs2((msg, replyTo) { | 74 _call(replyTo, msg5, expect.runs2((msg, replyTo) { |
| 66 Expect.equals("6", msg[0]); | 75 Expect.equals("6", msg[0]); |
| 67 expect.succeeded(); | 76 expect.succeeded(); |
| 68 })); | 77 })); |
| 69 })); | 78 })); |
| 70 })); | 79 })); |
| 71 })); | 80 })); |
| 72 }); | 81 }); |
| 73 } | 82 } |
| 74 | 83 |
| 75 main() { | 84 main() { |
| 76 runTests([test]); | 85 runTests([test]); |
| 77 } | 86 } |
| OLD | NEW |