Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Unified Diff: tests/isolate/count_test.dart

Issue 27215002: Very simple version of Isolates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/isolate/count_stream_test.dart ('k') | tests/isolate/cross_isolate_message_stream_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/isolate/count_test.dart
diff --git a/tests/isolate/count_test.dart b/tests/isolate/count_test.dart
index 2010c8863bbfa45968090fd684daa7fc1088193b..c0cbde3eda001662dcabfd8e498713b0598c534e 100644
--- a/tests/isolate/count_test.dart
+++ b/tests/isolate/count_test.dart
@@ -6,47 +6,53 @@ library CountTest;
import '../../pkg/unittest/lib/unittest.dart';
import 'dart:isolate';
-void countMessages() {
+void countMessages(replyTo) {
int count = 0;
- port.receive((int message, SendPort replyTo) {
+ var port = new ReceivePort();
+ replyTo.send(["init", port.sendPort]);
+ port.listen((int message) {
if (message == -1) {
expect(count, 10);
- replyTo.send(-1, null);
+ replyTo.send(["done"]);
port.close();
return;
}
- expect(message, count);
count++;
- replyTo.send(message * 2, null);
+ expect(message, count);
+ replyTo.send(["count", message * 2]);
});
}
void main() {
test("count 10 consecutive messages", () {
- int count = 0;
- SendPort remote = spawnFunction(countMessages);
ReceivePort local = new ReceivePort();
- SendPort reply = local.toSendPort();
-
- local.receive(expectAsync2((int message, SendPort replyTo) {
- if (message == -1) {
- // [count] is '11' because when we sent '9' to [remote],
- // the other isolate will send another message '18', that this
- // isolate will receive. Then this isolate will send '10' to
- // [remote] and increment [count]. Note that this last '10'
- // message will not be received by the other isolate, since it
- // received '-1' before.
- expect(count, 11);
- local.close();
- return;
- }
-
- expect(message, (count - 1) * 2);
- remote.send(count++, reply);
- if (count == 10) {
- remote.send(-1, reply);
+ Isolate.spawn(countMessages, local.sendPort);
+ SendPort remote;
+ int count = 0;
+ var done = expectAsync0((){});
+ local.listen((msg) {
+ switch (msg[0]) {
+ case "init":
+ expect(remote, null);
+ remote = msg[1];
+ remote.send(++count);
+ break;
+ case "count":
+ expect(msg[1], count * 2);
+ if (count == 10) {
+ remote.send(-1);
+ } else {
+ remote.send(++count);
+ }
+ break;
+ case "done":
+ expect(count, 10);
+ local.close();
+ done();
+ break;
+ default:
+ fail("unreachable: ${msg[0]}");
}
- }, count: 11));
- remote.send(count++, reply);
+ });
});
}
« no previous file with comments | « tests/isolate/count_stream_test.dart ('k') | tests/isolate/cross_isolate_message_stream_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698