2012-08-03 07:24:29 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-10-05 06:22:36 +00:00
|
|
|
// 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.
|
|
|
|
|
2012-11-01 23:09:47 +00:00
|
|
|
library CountTest;
|
|
|
|
import 'dart:isolate';
|
2013-11-25 08:19:53 +00:00
|
|
|
import 'package:unittest/unittest.dart';
|
|
|
|
import "remote_unittest_helper.dart";
|
2011-10-05 06:22:36 +00:00
|
|
|
|
2013-10-25 19:23:00 +00:00
|
|
|
void countMessages(replyTo) {
|
2012-08-03 07:24:29 +00:00
|
|
|
int count = 0;
|
2013-10-25 19:23:00 +00:00
|
|
|
var port = new ReceivePort();
|
|
|
|
replyTo.send(["init", port.sendPort]);
|
|
|
|
port.listen((int message) {
|
2012-08-03 07:24:29 +00:00
|
|
|
if (message == -1) {
|
2012-11-01 23:09:47 +00:00
|
|
|
expect(count, 10);
|
2013-10-25 19:23:00 +00:00
|
|
|
replyTo.send(["done"]);
|
2012-08-03 07:24:29 +00:00
|
|
|
port.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
count++;
|
2013-10-25 19:23:00 +00:00
|
|
|
expect(message, count);
|
|
|
|
replyTo.send(["count", message * 2]);
|
2012-08-03 07:24:29 +00:00
|
|
|
});
|
2011-10-05 06:22:36 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 08:19:53 +00:00
|
|
|
void main([args, port]) {
|
|
|
|
if (testRemote(main, port)) return;
|
2012-04-25 22:31:04 +00:00
|
|
|
test("count 10 consecutive messages", () {
|
2012-08-03 07:24:29 +00:00
|
|
|
ReceivePort local = new ReceivePort();
|
2013-10-25 19:23:00 +00:00
|
|
|
Isolate.spawn(countMessages, local.sendPort);
|
|
|
|
SendPort remote;
|
|
|
|
int count = 0;
|
2014-03-31 18:33:18 +00:00
|
|
|
var done = expectAsync((){});
|
2013-10-25 19:23:00 +00:00
|
|
|
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]}");
|
2012-08-03 07:24:29 +00:00
|
|
|
}
|
2013-10-25 19:23:00 +00:00
|
|
|
});
|
2011-10-05 06:22:36 +00:00
|
|
|
});
|
|
|
|
}
|