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. |
22 }); | 30 }); |
23 } | 31 } |
24 | 32 |
25 // crossIsolate2 is nearly the same as crossIsolate1, but contains a | 33 void crossIsolate2(SendPort toIsolate1) { |
26 // different constant. | 34 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 } | 35 } |
39 | 36 |
40 main() { | 37 main() { |
41 test("share port, and send message cross isolates ", () { | 38 test("send message cross isolates ", () { |
42 SendPort port1 = spawnFunction(crossIsolate1); | 39 ReceivePort fromIsolate1 = new ReceivePort(); |
43 SendPort port2 = spawnFunction(crossIsolate2); | 40 Isolate.spawn(crossIsolate1, fromIsolate1.sendPort); |
44 // Create a new receive port and send it to isolate2. | 41 var done = expectAsync0((){}); |
45 ReceivePort myPort = new ReceivePort(); | 42 fromIsolate1.listen((msg) { |
46 port2.call(myPort.toSendPort()).then(expectAsync1((msg) { | 43 switch (msg[0]) { |
47 expect(msg[0], "ready"); | 44 case "ready1": |
48 // Send port of isolate2 to isolate1. | 45 SendPort toIsolate1 = msg[1]; |
49 port1.call(msg[1]).then(expectAsync1((msg) { | 46 Isolate.spawn(crossIsolate2, toIsolate1); |
50 expect(msg[0], "ready"); | 47 break; |
51 myPort.receive(expectAsync2((msg, replyTo) { | 48 case "fromIsolate1": |
52 expect(msg, 499); | 49 expect(msg[1], 100); |
53 myPort.close(); | 50 fromIsolate1.close(); |
54 })); | 51 break; |
55 msg[1].send(42, null); | 52 default: |
56 })); | 53 fail("unreachable! Tag: ${msg[0]}"); |
57 })); | 54 } |
| 55 }, onDone: done); |
58 }); | 56 }); |
59 } | 57 } |
OLD | NEW |