Chromium Code Reviews| Index: tests/isolate/cross_isolate_message_stream_test.dart |
| diff --git a/tests/isolate/cross_isolate_message_stream_test.dart b/tests/isolate/cross_isolate_message_stream_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7535a085a3eb2edbb4f1355cc1b3a06a7d9296bf |
| --- /dev/null |
| +++ b/tests/isolate/cross_isolate_message_stream_test.dart |
| @@ -0,0 +1,90 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// Dart test program for testing that isolates can communicate to isolates |
| +// other than the main isolate. |
| + |
| +library CrossIsolateMessageTest; |
| +import 'dart:isolate'; |
| +import '../../pkg/unittest/lib/unittest.dart'; |
| + |
|
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.
|
| +void crossIsolate1() { |
| + bool first = true; |
| + IsolateSink mainIsolate; |
| + var subscription = stream.listen((msg) { |
| + if (first) { |
| + first = false; |
| + mainIsolate = msg; |
| + return; |
| + } |
| + IsolateSink otherIsolate = msg; |
| + MessageBox box = new MessageBox(); |
| + box.stream.single.then((msg) { |
| + expect(msg[0], "fromMain"); |
| + otherIsolate.add(["fromIsolate1", msg[1] + 58]); // 100; |
| + otherIsolate.close(); |
| + box.stream.close(); |
| + }); |
| + mainIsolate.add(['ready1', box.sink]); |
| + stream.close(); |
| + }); |
| +} |
| + |
| +void crossIsolate2() { |
| + var subscription; |
| + subscription = stream.listen((msg) { |
| + IsolateSink mainIsolate = msg; |
| + MessageBox box = new MessageBox(); |
| + box.stream.listen((msg) { |
| + expect(msg[0], "fromIsolate1"); |
| + mainIsolate.add(["fromIsolate2", msg[1] + 399]); // 499; |
| + mainIsolate.close(); |
| + box.stream.close(); |
| + }); |
| + mainIsolate.add(['ready2', box.sink]); |
| + subscription.cancel(); |
| + }); |
| +} |
| + |
| +main() { |
| + test("share sink, and send message cross isolates ", () { |
| + IsolateSink sink1 = streamSpawnFunction(crossIsolate1); |
| + IsolateSink sink2 = streamSpawnFunction(crossIsolate2); |
| + // Create a new sink and send it to isolate2. |
| + MessageBox box = new MessageBox(); |
| + sink1.add(box.sink); |
| + sink2.add(box.sink); |
| + int msgNumber = 0; |
| + |
| + bool isReady1 = false; |
| + bool isReady2 = false; |
| + bool hasSentMessage = false; |
| + |
| + Function ready1 = expectAsync0(() => isReady1 = true); |
| + Function ready2 = expectAsync0(() => isReady2 = true); |
| + Function fromIsolate2 = expectAsync1((data) { |
| + expect(data, 499); |
| + }); |
| + IsolateSink sink1b; |
| + IsolateSink sink2b; |
| + |
| + box.stream.listen((msg) { |
| + switch (msg[0]) { |
| + case 'ready1': ready1(); sink1b = msg[1]; break; |
| + case 'ready2': |
| + ready2(); |
| + sink2b = msg[1]; |
| + sink1.add(sink2b); |
| + break; |
| + case 'fromIsolate2': fromIsolate2(msg[1]); break; |
| + default: throw "bad message"; |
| + } |
| + if (isReady1 && isReady2 && !hasSentMessage) { |
| + hasSentMessage = true; |
| + sink1b.add(["fromMain", 42]); |
| + sink1b.close(); |
| + } |
| + }); |
| + }); |
| +} |