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 communicate to isolates | 5 // Dart test program for testing that isolates can communicate to isolates |
6 // other than the main isolate. | 6 // other than the main isolate. |
7 | 7 |
8 library CrossIsolateMessageTest; | 8 library CrossIsolateMessageTest; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import '../../pkg/unittest/lib/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| 11 import "remote_unittest_helper.dart"; |
11 | 12 |
12 /* | 13 /* |
13 * Everything starts in the main-isolate (in the main-method). | 14 * Everything starts in the main-isolate (in the main-method). |
14 * The main isolate spawns two isolates: isolate1 (with entry point | 15 * The main isolate spawns two isolates: isolate1 (with entry point |
15 * 'crossIsolate1') and isolate2 (with entry point 'crossIsolate2'). | 16 * 'crossIsolate1') and isolate2 (with entry point 'crossIsolate2'). |
16 * | 17 * |
17 * The main isolate creates two isolates, isolate1 and isolate2. | 18 * The main isolate creates two isolates, isolate1 and isolate2. |
18 * The second isolate is created with a send-port being listened on by | 19 * The second isolate is created with a send-port being listened on by |
19 * isolate1. A message is passed along this from isolate2 to isolate1. | 20 * isolate1. A message is passed along this from isolate2 to isolate1. |
20 * Isolate1 then sends the result back to the main isolate for final checking. | 21 * Isolate1 then sends the result back to the main isolate for final checking. |
21 */ | 22 */ |
22 | 23 |
23 void crossIsolate1(SendPort mainIsolate) { | 24 void crossIsolate1(SendPort mainIsolate) { |
24 ReceivePort local = new ReceivePort(); | 25 ReceivePort local = new ReceivePort(); |
25 mainIsolate.send(["ready1", local.sendPort]); | 26 mainIsolate.send(["ready1", local.sendPort]); |
26 local.first.then((msg) { | 27 local.first.then((msg) { |
27 // Message from crossIsolate2 | 28 // Message from crossIsolate2 |
28 expect(msg[0], "fromIsolate2"); | 29 expect(msg[0], "fromIsolate2"); |
29 mainIsolate.send(["fromIsolate1", msg[1] + 58]); // 100. | 30 mainIsolate.send(["fromIsolate1", msg[1] + 58]); // 100. |
30 }); | 31 }); |
31 } | 32 } |
32 | 33 |
33 void crossIsolate2(SendPort toIsolate1) { | 34 void crossIsolate2(SendPort toIsolate1) { |
34 toIsolate1.send(["fromIsolate2", 42]); | 35 toIsolate1.send(["fromIsolate2", 42]); |
35 } | 36 } |
36 | 37 |
37 main() { | 38 void main([args, port]) { |
| 39 if (testRemote(main, port)) return; |
38 test("send message cross isolates ", () { | 40 test("send message cross isolates ", () { |
39 ReceivePort fromIsolate1 = new ReceivePort(); | 41 ReceivePort fromIsolate1 = new ReceivePort(); |
40 Isolate.spawn(crossIsolate1, fromIsolate1.sendPort); | 42 Isolate.spawn(crossIsolate1, fromIsolate1.sendPort); |
41 var done = expectAsync0((){}); | 43 var done = expectAsync0((){}); |
42 fromIsolate1.listen((msg) { | 44 fromIsolate1.listen((msg) { |
43 switch (msg[0]) { | 45 switch (msg[0]) { |
44 case "ready1": | 46 case "ready1": |
45 SendPort toIsolate1 = msg[1]; | 47 SendPort toIsolate1 = msg[1]; |
46 Isolate.spawn(crossIsolate2, toIsolate1); | 48 Isolate.spawn(crossIsolate2, toIsolate1); |
47 break; | 49 break; |
48 case "fromIsolate1": | 50 case "fromIsolate1": |
49 expect(msg[1], 100); | 51 expect(msg[1], 100); |
50 fromIsolate1.close(); | 52 fromIsolate1.close(); |
51 break; | 53 break; |
52 default: | 54 default: |
53 fail("unreachable! Tag: ${msg[0]}"); | 55 fail("unreachable! Tag: ${msg[0]}"); |
54 } | 56 } |
55 }, onDone: done); | 57 }, onDone: done); |
56 }); | 58 }); |
57 } | 59 } |
OLD | NEW |