Chromium Code Reviews| 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 | |
|
Lasse Reichstein Nielsen
2013/03/19 12:01:55
Add documentation for what is expected to happen i
floitsch
2013/03/19 13:18:59
Done.
| |
| 12 void crossIsolate1() { | |
| 13 bool first = true; | |
| 14 IsolateSink mainIsolate; | |
| 15 var subscription = stream.listen((msg) { | |
| 16 if (first) { | |
| 17 first = false; | |
| 18 mainIsolate = msg; | |
| 19 return; | |
| 20 } | |
| 21 IsolateSink otherIsolate = msg; | |
| 22 MessageBox box = new MessageBox(); | |
| 23 box.stream.single.then((msg) { | |
| 24 expect(msg[0], "fromMain"); | |
| 25 otherIsolate.add(["fromIsolate1", msg[1] + 58]); // 100; | |
| 26 otherIsolate.close(); | |
| 27 box.stream.close(); | |
| 28 }); | |
| 29 mainIsolate.add(['ready1', box.sink]); | |
| 30 stream.close(); | |
| 31 }); | |
| 32 } | |
| 33 | |
| 34 void crossIsolate2() { | |
| 35 var subscription; | |
| 36 subscription = stream.listen((msg) { | |
| 37 IsolateSink mainIsolate = msg; | |
| 38 MessageBox box = new MessageBox(); | |
| 39 box.stream.listen((msg) { | |
| 40 expect(msg[0], "fromIsolate1"); | |
| 41 mainIsolate.add(["fromIsolate2", msg[1] + 399]); // 499; | |
| 42 mainIsolate.close(); | |
| 43 box.stream.close(); | |
| 44 }); | |
| 45 mainIsolate.add(['ready2', box.sink]); | |
| 46 subscription.cancel(); | |
| 47 }); | |
| 48 } | |
| 49 | |
| 50 main() { | |
| 51 test("share sink, and send message cross isolates ", () { | |
| 52 IsolateSink sink1 = streamSpawnFunction(crossIsolate1); | |
| 53 IsolateSink sink2 = streamSpawnFunction(crossIsolate2); | |
| 54 // Create a new sink and send it to isolate2. | |
| 55 MessageBox box = new MessageBox(); | |
| 56 sink1.add(box.sink); | |
| 57 sink2.add(box.sink); | |
| 58 int msgNumber = 0; | |
| 59 | |
| 60 bool isReady1 = false; | |
| 61 bool isReady2 = false; | |
| 62 bool hasSentMessage = false; | |
| 63 | |
| 64 Function ready1 = expectAsync0(() => isReady1 = true); | |
| 65 Function ready2 = expectAsync0(() => isReady2 = true); | |
| 66 Function fromIsolate2 = expectAsync1((data) { | |
| 67 expect(data, 499); | |
| 68 }); | |
| 69 IsolateSink sink1b; | |
| 70 IsolateSink sink2b; | |
| 71 | |
| 72 box.stream.listen((msg) { | |
| 73 switch (msg[0]) { | |
| 74 case 'ready1': ready1(); sink1b = msg[1]; break; | |
| 75 case 'ready2': | |
| 76 ready2(); | |
| 77 sink2b = msg[1]; | |
| 78 sink1.add(sink2b); | |
| 79 break; | |
| 80 case 'fromIsolate2': fromIsolate2(msg[1]); break; | |
| 81 default: throw "bad message"; | |
| 82 } | |
| 83 if (isReady1 && isReady2 && !hasSentMessage) { | |
| 84 hasSentMessage = true; | |
| 85 sink1b.add(["fromMain", 42]); | |
| 86 sink1b.close(); | |
| 87 } | |
| 88 }); | |
| 89 }); | |
| 90 } | |
| OLD | NEW |