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 '../../pkg/unittest/lib/unittest.dart'; |
11 | 11 |
12 void crossIsolate1() { | 12 /* |
13 port.receive((msg, replyTo) { | 13 * Everything starts in the main-isolate (in the main-method). |
14 SendPort otherIsolate = msg; | 14 * The main isolate spawns two isolates: isolate1 (with entry point |
15 ReceivePort receivePort = new ReceivePort(); | 15 * 'crossIsolate1') and isolate2 (with entry point 'crossIsolate2'). |
16 receivePort.receive((msg, replyTo) { | 16 * |
17 otherIsolate.send(msg + 58, null); // 100. | 17 * The main isolate creates two isolates, isolate1 and isolate2. |
18 receivePort.close(); | 18 * The second isolate is created with a send-port being listened on by |
19 }); | 19 * isolate1. A message is passed along this from isolate2 to isolate1. |
20 replyTo.send(['ready', receivePort.toSendPort()]); | 20 * Isolate1 then sends the result back to the main isolate for final checking. |
21 port.close(); | 21 */ |
| 22 |
| 23 void crossIsolate1(SendPort mainIsolate) { |
| 24 ReceivePort local = new ReceivePort(); |
| 25 mainIsolate.send(["ready1", local.sendPort]); |
| 26 local.first.then((msg) { |
| 27 // Message from crossIsolate2 |
| 28 expect(msg[0], "fromIsolate2"); |
| 29 mainIsolate.send(["fromIsolate1", msg[1] + 58]); // 100. |
| 30 local.close(); |
22 }); | 31 }); |
23 } | 32 } |
24 | 33 |
25 // crossIsolate2 is nearly the same as crossIsolate1, but contains a | 34 void crossIsolate2(SendPort toIsolate1) { |
26 // different constant. | 35 toIsolate1.send(["fromIsolate2", 42]); |
27 void crossIsolate2() { | |
28 port.receive((msg, replyTo) { | |
29 SendPort mainIsolate = msg; | |
30 ReceivePort receivePort = new ReceivePort(); | |
31 receivePort.receive((msg, replyTo) { | |
32 mainIsolate.send(msg + 399, null); // 499. | |
33 receivePort.close(); | |
34 }); | |
35 replyTo.send(['ready', receivePort.toSendPort()]); | |
36 port.close(); | |
37 }); | |
38 } | 36 } |
39 | 37 |
40 main() { | 38 main() { |
41 test("share port, and send message cross isolates ", () { | 39 test("send message cross isolates ", () { |
42 SendPort port1 = spawnFunction(crossIsolate1); | 40 ReceivePort fromIsolate1 = new ReceivePort(); |
43 SendPort port2 = spawnFunction(crossIsolate2); | 41 Isolate.spawn(crossIsolate1, fromIsolate1.sendPort); |
44 // Create a new receive port and send it to isolate2. | 42 var done = expectAsync0((){}); |
45 ReceivePort myPort = new ReceivePort(); | 43 fromIsolate1.listen((msg) { |
46 port2.call(myPort.toSendPort()).then(expectAsync1((msg) { | 44 print(msg[0]); |
47 expect(msg[0], "ready"); | 45 switch (msg[0]) { |
48 // Send port of isolate2 to isolate1. | 46 case "ready1": |
49 port1.call(msg[1]).then(expectAsync1((msg) { | 47 SendPort toIsolate1 = msg[1]; |
50 expect(msg[0], "ready"); | 48 Isolate.spawn(crossIsolate2, toIsolate1); |
51 myPort.receive(expectAsync2((msg, replyTo) { | 49 break; |
52 expect(msg, 499); | 50 case "fromIsolate1": |
53 myPort.close(); | 51 expect(msg[1], 100); |
54 })); | 52 fromIsolate1.close(); |
55 msg[1].send(42, null); | 53 break; |
56 })); | 54 default: |
57 })); | 55 fail("unreachable! Tag: ${msg[0]}"); |
| 56 } |
| 57 }, onDone: done); |
58 }); | 58 }); |
59 } | 59 } |
OLD | NEW |