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