| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Dart test program for testing that isolates can communicate to isolates |
| 6 // other than the main isolate. |
| 7 |
| 8 library CrossIsolateMessageTest; |
| 9 import 'dart:isolate'; |
| 10 import '../../pkg/unittest/lib/unittest.dart'; |
| 11 |
| 12 /* |
| 13 * Everything starts in the main-isolate (in the main-method). |
| 14 * The main isolate spawns two isolates: isolate1 (with entry point |
| 15 * 'crossIsolate1') and isolate2 (with entry point 'crossIsolate2'). |
| 16 * |
| 17 * The main-isolate creates a new message-box and sends its sink to both |
| 18 * isolates. Whenever isolate1 or isolate2 send something to the main-isolate |
| 19 * they will use this sink. |
| 20 * Isolate2 stores the sink and replies with a new sink (sink2b) it created. |
| 21 * Isolate1 stores the sink and waits for another message. |
| 22 * Main receives isolate2's sink2b and sends it to isolate1. |
| 23 * Isolate1 stores this sink as "otherIsolate" and send a new sink (sink1b) to |
| 24 * the main isolate. |
| 25 * Main receives sink1b and sents a message "fromMain, 42" to sink1b. |
| 26 * isolate1 receives this message, modifies it (adding 58 to 42) and forwards |
| 27 * it to isolate2 (otherIsolate). |
| 28 * isolate2 receives the message, modifies it (adding 399), and sends it to |
| 29 * the main isolate. |
| 30 * The main-isolate receives it, verifies that the result is 499 and ends the |
| 31 * test. |
| 32 */ |
| 33 |
| 34 void crossIsolate1() { |
| 35 bool first = true; |
| 36 IsolateSink mainIsolate; |
| 37 var subscription = stream.listen((msg) { |
| 38 if (first) { |
| 39 first = false; |
| 40 mainIsolate = msg; |
| 41 return; |
| 42 } |
| 43 IsolateSink otherIsolate = msg; |
| 44 MessageBox box = new MessageBox(); |
| 45 box.stream.single.then((msg) { |
| 46 expect(msg[0], "fromMain"); |
| 47 otherIsolate.add(["fromIsolate1", msg[1] + 58]); // 100; |
| 48 otherIsolate.close(); |
| 49 box.stream.close(); |
| 50 }); |
| 51 mainIsolate.add(['ready1', box.sink]); |
| 52 stream.close(); |
| 53 }); |
| 54 } |
| 55 |
| 56 void crossIsolate2() { |
| 57 var subscription; |
| 58 subscription = stream.listen((msg) { |
| 59 IsolateSink mainIsolate = msg; |
| 60 MessageBox box = new MessageBox(); |
| 61 box.stream.listen((msg) { |
| 62 expect(msg[0], "fromIsolate1"); |
| 63 mainIsolate.add(["fromIsolate2", msg[1] + 399]); // 499; |
| 64 mainIsolate.close(); |
| 65 box.stream.close(); |
| 66 }); |
| 67 mainIsolate.add(['ready2', box.sink]); |
| 68 subscription.cancel(); |
| 69 }); |
| 70 } |
| 71 |
| 72 main() { |
| 73 test("share sink, and send message cross isolates ", () { |
| 74 IsolateSink sink1 = streamSpawnFunction(crossIsolate1); |
| 75 IsolateSink sink2 = streamSpawnFunction(crossIsolate2); |
| 76 // Create a new sink and send it to isolate2. |
| 77 MessageBox box = new MessageBox(); |
| 78 sink1.add(box.sink); |
| 79 sink2.add(box.sink); |
| 80 int msgNumber = 0; |
| 81 |
| 82 bool isReady1 = false; |
| 83 bool isReady2 = false; |
| 84 bool hasSentMessage = false; |
| 85 |
| 86 Function ready1 = expectAsync0(() => isReady1 = true); |
| 87 Function ready2 = expectAsync0(() => isReady2 = true); |
| 88 Function fromIsolate2 = expectAsync1((data) { |
| 89 expect(data, 499); |
| 90 }); |
| 91 IsolateSink sink1b; |
| 92 IsolateSink sink2b; |
| 93 |
| 94 box.stream.listen((msg) { |
| 95 switch (msg[0]) { |
| 96 case 'ready1': ready1(); sink1b = msg[1]; break; |
| 97 case 'ready2': |
| 98 ready2(); |
| 99 sink2b = msg[1]; |
| 100 sink1.add(sink2b); |
| 101 break; |
| 102 case 'fromIsolate2': fromIsolate2(msg[1]); break; |
| 103 default: throw "bad message"; |
| 104 } |
| 105 if (isReady1 && isReady2 && !hasSentMessage) { |
| 106 hasSentMessage = true; |
| 107 sink1b.add(["fromMain", 42]); |
| 108 sink1b.close(); |
| 109 } |
| 110 }); |
| 111 }); |
| 112 } |
| OLD | NEW |