 Chromium Code Reviews
 Chromium Code Reviews Issue 36933002:
  All isolate tests running on vm  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 36933002:
  All isolate tests running on vm  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| 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 isolateA(SendPort init) { | 
| 14 port.receive((msg, replyTo) { | 14 ReceivePort port = new ReceivePort(); | 
| 15 expect(msg, "launch nested!"); | 15 port.first.then((message) { | 
| 16 SendPort p = spawnFunction(isolateB); | 16 expect(message[0], "launch nested!"); | 
| 17 p.send(replyTo, null); | 17 SendPort replyTo = message[1]; | 
| 18 port.close(); | 18 Isolate.spawn(isolateB, replyTo); | 
| 19 }); | 19 }); | 
| 20 init.send(port.sendPort); | |
| 
floitsch
2013/10/23 13:33:24
move send before port.first.
 
Lasse Reichstein Nielsen
2013/10/24 10:26:01
Done.
 | |
| 20 } | 21 } | 
| 21 | 22 | 
| 22 String msg0 = "0 there?"; | 23 String msg0 = "0 there?"; | 
| 23 String msg1 = "1 Yes."; | 24 String msg1 = "1 Yes."; | 
| 24 String msg2 = "2 great. Think the other one is already dead?"; | 25 String msg2 = "2 great. Think the other one is already dead?"; | 
| 25 String msg3 = "3 Give him some time."; | 26 String msg3 = "3 Give him some time."; | 
| 26 String msg4 = "4 now?"; | 27 String msg4 = "4 now?"; | 
| 27 String msg5 = "5 Now."; | 28 String msg5 = "5 Now."; | 
| 28 String msg6 = "6 Great. Bye"; | 29 String msg6 = "6 Great. Bye"; | 
| 29 | 30 | 
| 30 void _call(SendPort p, msg, void onreceive(m, replyTo)) { | 31 void _call(SendPort p, msg, void onreceive(m, replyTo)) { | 
| 31 final replyTo = new ReceivePort(); | 32 final replyTo = new ReceivePort(); | 
| 32 p.send(msg, replyTo.toSendPort()); | 33 p.send([msg, replyTo.sendPort]); | 
| 33 replyTo.receive((m, r) { | 34 print("-$msg"); | 
| 
floitsch
2013/10/23 13:33:24
debug print.
 
Lasse Reichstein Nielsen
2013/10/24 10:26:01
Done.
 | |
| 34 replyTo.close(); | 35 replyTo.first.then((m) { | 
| 35 onreceive(m, r); | 36 onreceive(m[0], m[1]); | 
| 36 }); | 37 }); | 
| 37 } | 38 } | 
| 38 | 39 | 
| 39 void isolateB() { | 40 void isolateB(SendPort mainPort) { | 
| 40 port.receive((mainPort, replyTo) { | 41 // Do a little ping-pong dance to give the intermediate isolate | 
| 41 port.close(); | 42 // time to die. | 
| 42 // Do a little ping-pong dance to give the intermediate isolate | 43 _call(mainPort, msg0, ((msg, replyTo) { | 
| 43 // time to die. | 44 expect(msg[0], "1"); | 
| 44 _call(mainPort, msg0, ((msg, replyTo) { | 45 _call(replyTo, msg2, ((msg, replyTo) { | 
| 45 expect(msg[0], "1"); | 46 expect(msg[0], "3"); | 
| 46 _call(replyTo, msg2, ((msg, replyTo) { | 47 _call(replyTo, msg4, ((msg, replyTo) { | 
| 47 expect(msg[0], "3"); | 48 expect(msg[0], "5"); | 
| 48 _call(replyTo, msg4, ((msg, replyTo) { | 49 replyTo.send([msg6, null]); | 
| 49 expect(msg[0], "5"); | |
| 50 replyTo.send(msg6, null); | |
| 51 })); | |
| 52 })); | 50 })); | 
| 53 })); | 51 })); | 
| 54 }); | 52 })); | 
| 55 } | 53 } | 
| 56 | 54 | 
| 57 main() { | 55 main() { | 
| 58 test("spawned isolate can spawn other isolates", () { | 56 test("spawned isolate can spawn other isolates", () { | 
| 59 SendPort port = spawnFunction(isolateA); | 57 ReceivePort init = new ReceivePort(); | 
| 60 _call(port, "launch nested!", expectAsync2((msg, replyTo) { | 58 Isolate.spawn(isolateA, init.sendPort); | 
| 61 expect(msg[0], "0"); | 59 init.first.then(expectAsync1((port) { | 
| 62 _call(replyTo, msg1, expectAsync2((msg, replyTo) { | 60 _call(port, "launch nested!", expectAsync2((msg, replyTo) { | 
| 63 expect(msg[0], "2"); | 61 expect(msg[0], "0"); | 
| 64 _call(replyTo, msg3, expectAsync2((msg, replyTo) { | 62 _call(replyTo, msg1, expectAsync2((msg, replyTo) { | 
| 65 expect(msg[0], "4"); | 63 expect(msg[0], "2"); | 
| 66 _call(replyTo, msg5, expectAsync2((msg, replyTo) { | 64 _call(replyTo, msg3, expectAsync2((msg, replyTo) { | 
| 67 expect(msg[0], "6"); | 65 expect(msg[0], "4"); | 
| 66 _call(replyTo, msg5, expectAsync2((msg, _) { | |
| 67 expect(msg[0], "6"); | |
| 68 })); | |
| 68 })); | 69 })); | 
| 69 })); | 70 })); | 
| 70 })); | 71 })); | 
| 71 })); | 72 })); | 
| 72 }); | 73 }); | 
| 73 } | 74 } | 
| OLD | NEW |