[ VM ] Update lib_2/isolate tests to no longer use package:unittest

Fixes https://github.com/dart-lang/sdk/issues/40799

Change-Id: Ia65221c2a14b9d8e923e394ebc1c35ea5dad4c6f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139953
Commit-Queue: Ben Konyi <bkonyi@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
This commit is contained in:
Ben Konyi 2020-03-19 00:57:26 +00:00 committed by commit-bot@chromium.org
parent eca06cd299
commit d1c8aae6d8
16 changed files with 326 additions and 477 deletions

View file

@ -8,8 +8,8 @@
library CountTest;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
void countMessages(replyTo) {
int count = 0;
@ -18,48 +18,46 @@ void countMessages(replyTo) {
port.listen((_message) {
int message = _message;
if (message == -1) {
expect(count, 10);
Expect.equals(count, 10);
replyTo.send(["done"]);
port.close();
return;
}
count++;
expect(message, count);
Expect.equals(message, count);
replyTo.send(["count", message * 2]);
});
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("count 10 consecutive messages", () {
ReceivePort local = new ReceivePort();
Isolate.spawn(countMessages, local.sendPort);
SendPort remote;
int count = 0;
var done = expectAsync(() {});
local.listen((msg) {
switch (msg[0]) {
case "init":
expect(remote, null);
remote = msg[1];
//testRemote(main, port);
ReceivePort local = new ReceivePort();
Isolate.spawn(countMessages, local.sendPort);
SendPort remote;
int count = 0;
asyncStart();
local.listen((msg) {
switch (msg[0]) {
case "init":
Expect.equals(remote, null);
remote = msg[1];
remote.send(++count);
break;
case "count":
Expect.equals(msg[1], count * 2);
if (count == 10) {
remote.send(-1);
} else {
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]}");
}
});
}
break;
case "done":
Expect.equals(count, 10);
local.close();
asyncEnd();
break;
default:
Expect.fail("unreachable: ${msg[0]}");
}
});
}

View file

@ -11,8 +11,8 @@
library CrossIsolateMessageTest;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
/*
* Everything starts in the main-isolate (in the main-method).
@ -30,7 +30,7 @@ void crossIsolate1(SendPort mainIsolate) {
mainIsolate.send(["ready1", local.sendPort]);
local.first.then((msg) {
// Message from crossIsolate2
expect(msg[0], "fromIsolate2");
Expect.equals(msg[0], "fromIsolate2");
mainIsolate.send(["fromIsolate1", msg[1] + 58]); // 100.
});
}
@ -40,24 +40,22 @@ void crossIsolate2(SendPort toIsolate1) {
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("send message cross isolates ", () {
ReceivePort fromIsolate1 = new ReceivePort();
Isolate.spawn(crossIsolate1, fromIsolate1.sendPort);
var done = expectAsync(() {});
fromIsolate1.listen((msg) {
switch (msg[0]) {
case "ready1":
SendPort toIsolate1 = msg[1];
Isolate.spawn(crossIsolate2, toIsolate1);
break;
case "fromIsolate1":
expect(msg[1], 100);
fromIsolate1.close();
break;
default:
fail("unreachable! Tag: ${msg[0]}");
}
}, onDone: done);
});
ReceivePort fromIsolate1 = new ReceivePort();
Isolate.spawn(crossIsolate1, fromIsolate1.sendPort);
asyncStart();
fromIsolate1.listen((msg) {
switch (msg[0]) {
case "ready1":
SendPort toIsolate1 = msg[1];
Isolate.spawn(crossIsolate2, toIsolate1);
break;
case "fromIsolate1":
Expect.equals(msg[1], 100);
fromIsolate1.close();
break;
default:
Expect.fail("unreachable! Tag: ${msg[0]}");
}
}, onDone: asyncEnd);
}

View file

@ -10,25 +10,22 @@ library MandelIsolateTest;
import 'dart:async';
import 'dart:isolate';
import 'dart:math';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
const TERMINATION_MESSAGE = -1;
const N = 100;
const ISOLATES = 20;
void main([args, port]) {
if (testRemote(main, port)) return;
// Test is really slow in debug builds of the VM.
var configuration = unittestConfiguration;
configuration.timeout = const Duration(seconds: 480);
test("Render Mandelbrot in parallel", () {
final state = new MandelbrotState();
state._validated.future.then(expectAsync((result) {
expect(result, isTrue);
}));
for (int i = 0; i < min(ISOLATES, N); i++) state.startClient(i);
final state = new MandelbrotState();
asyncStart();
state._validated.future.then((result) {
Expect.isTrue(result);
asyncEnd();
});
for (int i = 0; i < min(ISOLATES, N); i++) state.startClient(i);
}
class MandelbrotState {

View file

@ -11,8 +11,8 @@
library Message2Test;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
// ---------------------------------------------------------------------------
// Message passing test 2.
@ -20,14 +20,12 @@ import "remote_unittest_helper.dart";
class MessageTest {
static void mapEqualsDeep(Map expected, Map actual) {
expect(expected, isMap);
expect(actual, isMap);
expect(actual.length, expected.length);
Expect.equals(actual.length, expected.length);
testForEachMap(key, value) {
if (value is List) {
listEqualsDeep(value, actual[key]);
} else {
expect(actual[key], value);
Expect.equals(actual[key], value);
}
}
@ -37,11 +35,13 @@ class MessageTest {
static void listEqualsDeep(List expected, List actual) {
for (int i = 0; i < expected.length; i++) {
if (expected[i] is List) {
Expect.type<List>(actual[i]);
listEqualsDeep(expected[i], actual[i]);
} else if (expected[i] is Map) {
Expect.type<Map>(actual[i]);
mapEqualsDeep(expected[i], actual[i]);
} else {
expect(actual[i], expected[i]);
Expect.equals(actual[i], expected[i]);
}
}
}
@ -50,7 +50,8 @@ class MessageTest {
void pingPong(replyPort) {
ReceivePort port = new ReceivePort();
port.listen((message) {
if (message == null) {
if (message is SendPort) {
message.send('done');
port.close();
} else {
// Bounce the received object back so that the sender
@ -62,22 +63,25 @@ void pingPong(replyPort) {
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("map is equal after it is sent back and forth", () {
ReceivePort port = new ReceivePort();
Isolate.spawn(pingPong, port.sendPort);
port.first.then(expectAsync((remote) {
Map m = new Map();
m[1] = "eins";
m[2] = "deux";
m[3] = "tre";
m[4] = "four";
ReceivePort replyPort = new ReceivePort();
remote.send([m, replyPort.sendPort]);
replyPort.first.then(expectAsync((var received) {
ReceivePort port = new ReceivePort();
Isolate.spawn(pingPong, port.sendPort);
asyncStart();
port.first.then((remote) {
Map m = new Map();
m[1] = "eins";
m[2] = "deux";
m[3] = "tre";
m[4] = "four";
ReceivePort replyPort = new ReceivePort();
remote.send([m, replyPort.sendPort]);
replyPort.listen((var received) {
if (received == 'done') {
replyPort.close();
asyncEnd();
} else {
MessageTest.mapEqualsDeep(m, received);
remote.send(null);
}));
}));
remote.send(replyPort.sendPort);
}
});
});
}

View file

@ -10,10 +10,11 @@
library MessageTest;
import 'dart:isolate';
import 'dart:async';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'dart:isolate';
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
// ---------------------------------------------------------------------------
// Message passing test.
@ -42,14 +43,12 @@ class MessageTest {
];
static void VerifyMap(Map expected, Map actual) {
expect(expected, isMap);
expect(actual, isMap);
expect(actual.length, expected.length);
Expect.equals(actual.length, expected.length);
testForEachMap(key, value) {
if (value is List) {
VerifyList(value, actual[key]);
} else {
expect(actual[key], value);
Expect.equals(actual[key], value);
}
}
@ -61,18 +60,19 @@ class MessageTest {
if (expected[i] is List) {
VerifyList(expected[i], actual[i]);
} else if (expected[i] is Map) {
Expect.type<Map>(actual[i]);
VerifyMap(expected[i], actual[i]);
} else {
expect(actual[i], expected[i]);
Expect.equals(actual[i], expected[i]);
}
}
}
static void VerifyObject(int index, var actual) {
var expected = elms[index];
expect(expected, isList);
expect(actual, isList);
expect(actual.length, expected.length);
Expect.type<List>(expected);
Expect.type<List>(actual);
Expect.equals(actual.length, expected.length);
VerifyList(expected, actual);
}
}
@ -107,46 +107,47 @@ Future remoteCall(SendPort port, message) {
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("send objects and receive them back", () {
ReceivePort port = new ReceivePort();
Isolate.spawn(pingPong, port.sendPort);
port.first.then(expectAsync1((remote) {
// Send objects and receive them back.
for (int i = 0; i < MessageTest.elms.length; i++) {
var sentObject = MessageTest.elms[i];
remoteCall(remote, sentObject).then(expectAsync1((receivedObject) {
MessageTest.VerifyObject(i, receivedObject);
}));
}
// Send recursive objects and receive them back.
List local_list1 = ["Hello", "World", "Hello", 0xffffffffff];
List local_list2 = [null, local_list1, local_list1];
List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff];
List sendObject = new List(5);
sendObject[0] = local_list1;
sendObject[1] = sendObject;
sendObject[2] = local_list2;
sendObject[3] = sendObject;
sendObject[4] = local_list3;
remoteCall(remote, sendObject).then((var replyObject) {
expect(sendObject, isList);
expect(replyObject, isList);
expect(sendObject.length, equals(replyObject.length));
expect(replyObject[1], same(replyObject));
expect(replyObject[3], same(replyObject));
expect(replyObject[0], same(replyObject[2][1]));
expect(replyObject[0], same(replyObject[2][2]));
expect(replyObject[2], same(replyObject[4][0]));
expect(replyObject[0][0], same(replyObject[0][2]));
expect(replyObject[0][3], equals(replyObject[4][4]));
ReceivePort port = new ReceivePort();
Isolate.spawn(pingPong, port.sendPort);
asyncStart();
port.first.then((remote) {
// Send objects and receive them back.
for (int i = 0; i < MessageTest.elms.length; i++) {
var sentObject = MessageTest.elms[i];
asyncStart();
remoteCall(remote, sentObject).then((receivedObject) {
MessageTest.VerifyObject(i, receivedObject);
asyncEnd();
});
}
// Shutdown the MessageServer.
remoteCall(remote, -1).then(expectAsync1((message) {
expect(message, MessageTest.elms.length + 1);
}));
}));
// Send recursive objects and receive them back.
List local_list1 = ["Hello", "World", "Hello", 0xffffffffff];
List local_list2 = [null, local_list1, local_list1];
List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff];
List sendObject = new List(5);
sendObject[0] = local_list1;
sendObject[1] = sendObject;
sendObject[2] = local_list2;
sendObject[3] = sendObject;
sendObject[4] = local_list3;
remoteCall(remote, sendObject).then((var replyObject) {
Expect.type<List>(sendObject);
Expect.type<List>(replyObject);
Expect.equals(sendObject.length, replyObject.length);
Expect.identical(replyObject[1], replyObject);
Expect.identical(replyObject[3], replyObject);
Expect.identical(replyObject[0], replyObject[2][1]);
Expect.identical(replyObject[0], replyObject[2][2]);
Expect.identical(replyObject[2], replyObject[4][0]);
Expect.identical(replyObject[0][0], replyObject[0][2]);
Expect.equals(replyObject[0][3], replyObject[4][4]);
});
// Shutdown the MessageServer.
remoteCall(remote, -1).then((message) {
Expect.equals(message, MessageTest.elms.length + 1);
asyncEnd();
});
});
}

View file

@ -9,8 +9,8 @@ library MintMakerTest;
import 'dart:async';
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
class Mint {
Map<SendPort, Purse> _registry;
@ -141,8 +141,7 @@ class MintMakerWrapper {
static Future<MintMakerWrapper> create() {
ReceivePort reply = new ReceivePort();
return Isolate
.spawn(mintMakerWrapper, reply.sendPort)
return Isolate.spawn(mintMakerWrapper, reply.sendPort)
.then((_) => reply.first.then((port) => new MintMakerWrapper._(port)));
}
@ -158,32 +157,31 @@ class MintMakerWrapper {
}
_checkBalance(PurseWrapper wrapper, expected) {
wrapper.queryBalance(expectAsync1((balance) {
expect(balance, equals(expected));
}));
wrapper.queryBalance((balance) {
Expect.equals(balance, expected);
});
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("creating purse, deposit, and query balance", () {
MintMakerWrapper.create().then(expectAsync1((mintMaker) {
mintMaker.makeMint(expectAsync1((mint) {
mint.createPurse(100, expectAsync1((purse) {
asyncStart();
MintMakerWrapper.create().then((mintMaker) {
mintMaker.makeMint((mint) {
mint.createPurse(100, (purse) {
_checkBalance(purse, 100);
purse.sproutPurse((sprouted) {
_checkBalance(sprouted, 0);
_checkBalance(purse, 100);
purse.sproutPurse(expectAsync1((sprouted) {
_checkBalance(sprouted, 0);
_checkBalance(purse, 100);
sprouted.deposit(purse, 5);
_checkBalance(sprouted, 0 + 5);
_checkBalance(purse, 100 - 5);
sprouted.deposit(purse, 5);
_checkBalance(sprouted, 0 + 5);
_checkBalance(purse, 100 - 5);
sprouted.deposit(purse, 42);
_checkBalance(sprouted, 0 + 5 + 42);
_checkBalance(purse, 100 - 5 - 42);
}));
}));
}));
}));
sprouted.deposit(purse, 42);
_checkBalance(sprouted, 0 + 5 + 42);
_checkBalance(purse, 100 - 5 - 42);
asyncEnd();
});
});
});
});
}

View file

@ -12,14 +12,14 @@
library NestedSpawn2Test;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
void isolateA(SendPort init) {
ReceivePort port = new ReceivePort();
init.send(port.sendPort);
port.first.then((message) {
expect(message[0], "launch nested!");
Expect.equals(message[0], "launch nested!");
SendPort replyTo = message[1];
Isolate.spawn(isolateB, replyTo);
});
@ -45,11 +45,11 @@ void isolateB(SendPort mainPort) {
// Do a little ping-pong dance to give the intermediate isolate
// time to die.
_call(mainPort, msg0, ((msg, replyTo) {
expect(msg[0], "1");
Expect.equals(msg[0], "1");
_call(replyTo, msg2, ((msg, replyTo) {
expect(msg[0], "3");
Expect.equals(msg[0], "3");
_call(replyTo, msg4, ((msg, replyTo) {
expect(msg[0], "5");
Expect.equals(msg[0], "5");
replyTo.send([msg6, null]);
}));
}));
@ -57,23 +57,23 @@ void isolateB(SendPort mainPort) {
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("spawned isolate can spawn other isolates", () {
ReceivePort init = new ReceivePort();
Isolate.spawn(isolateA, init.sendPort);
return init.first.then(expectAsync((port) {
_call(port, "launch nested!", expectAsync((msg, replyTo) {
expect(msg[0], "0");
_call(replyTo, msg1, expectAsync((msg, replyTo) {
expect(msg[0], "2");
_call(replyTo, msg3, expectAsync((msg, replyTo) {
expect(msg[0], "4");
_call(replyTo, msg5, expectAsync((msg, _) {
expect(msg[0], "6");
}));
}));
}));
}));
}));
// spawned isolate can spawn other isolates
ReceivePort init = new ReceivePort();
Isolate.spawn(isolateA, init.sendPort);
asyncStart();
init.first.then((port) {
_call(port, "launch nested!", (msg, replyTo) {
Expect.equals(msg[0], "0");
_call(replyTo, msg1, (msg, replyTo) {
Expect.equals(msg[0], "2");
_call(replyTo, msg3, (msg, replyTo) {
Expect.equals(msg[0], "4");
_call(replyTo, msg5, (msg, _) {
Expect.equals(msg[0], "6");
asyncEnd();
});
});
});
});
});
}

View file

@ -10,8 +10,8 @@
library NestedSpawnTest;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
void isolateA(message) {
message.add("isolateA");
@ -24,14 +24,14 @@ void isolateB(message) {
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("spawned isolates can spawn nested isolates", () {
ReceivePort port = new ReceivePort();
Isolate.spawn(isolateA, [port.sendPort, "main"]);
return port.first.then((message) {
expect("main", message[1]);
expect("isolateA", message[2]);
expect("isolateB", message[3]);
});
// spawned isolates can spawn nested isolates
ReceivePort port = new ReceivePort();
Isolate.spawn(isolateA, [port.sendPort, "main"]);
asyncStart();
port.first.then((message) {
Expect.equals("main", message[1]);
Expect.equals("isolateA", message[2]);
Expect.equals("isolateB", message[3]);
asyncEnd();
});
}

View file

@ -6,9 +6,6 @@
// VMOptions=--no-enable-isolate-groups
// Test properties of ports.
// Note: unittest.dart depends on ports, in particular on the behaviour tested
// here. To keep things simple, we don't use the unittest library here.
library PortTest;
import "package:expect/expect.dart";

View file

@ -9,9 +9,10 @@
library raw_port_test;
import 'dart:async';
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import 'remote_unittest_helper.dart';
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
void remote(SendPort port) {
port.send("reply");
@ -22,52 +23,63 @@ void remote2(SendPort port) {
port.send("reply 2");
}
main([args, port]) {
if (testRemote(main, port)) return;
test("raw receive", () {
main([args, port]) async {
// Raw receive
asyncTest(() {
final completer = Completer<void>();
RawReceivePort port = new RawReceivePort();
Isolate.spawn(remote, port.sendPort);
port.handler = expectAsync((v) {
expect(v, "reply");
port.handler = (v) {
Expect.equals(v, "reply");
port.close();
});
completer.complete();
};
return completer.future;
});
test("raw receive hashCode", () {
// Raw receive hashCode
{
RawReceivePort port = new RawReceivePort();
expect(port.hashCode is int, true);
Expect.isTrue(port.hashCode is int);
port.close();
});
}
test("raw receive twice - change handler", () {
// Raw receive twice - change handler
asyncTest(() {
final completer = Completer<void>();
RawReceivePort port = new RawReceivePort();
Isolate.spawn(remote2, port.sendPort);
port.handler = expectAsync((v) {
expect(v, "reply 1");
port.handler = expectAsync((v) {
expect(v, "reply 2");
port.handler = (v) {
Expect.equals(v, "reply 1");
port.handler = (v) {
Expect.equals(v, "reply 2");
port.close();
});
});
completer.complete();
};
};
return completer.future;
});
test("from-raw-port", () {
// From raw port
asyncTest(() {
final completer = Completer<void>();
RawReceivePort rawPort = new RawReceivePort();
Isolate.spawn(remote, rawPort.sendPort);
rawPort.handler = expectAsync((v) {
expect(v, "reply");
rawPort.handler = (v) {
Expect.equals(v, "reply");
ReceivePort port = new ReceivePort.fromRawReceivePort(rawPort);
Isolate.spawn(remote, rawPort.sendPort);
Isolate.spawn(remote, port.sendPort);
int ctr = 2;
port.listen(
expectAsync((v) {
expect(v, "reply");
ctr--;
if (ctr == 0) port.close();
}, count: 2),
onDone: expectAsync(() {}));
});
(v) {
Expect.equals(v, "reply");
ctr--;
if (ctr == 0) port.close();
},
onDone: () => completer.complete(),
);
};
return completer.future;
});
}

View file

@ -1,141 +0,0 @@
// Copyright (c) 2013, 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.
// Helper functions and classes for running a set of unittests in a
// remote isolate.
// Used to test Isolate.spawn because dartium/drt does not allow it in the DOM
// isolate.
import "dart:isolate";
import "package:unittest/unittest.dart";
import "dart:mirrors";
/**
* Use this function at the beginning of the main method:
*
* void main([args, port]) {
* if (testRemote(main, port)) return;
* // the usual test.
* }
*
* Remember to import unittest using the URI `package:unittest/unittest.dart`.
* Otherwise it won't be sharing the `unittestConfiguration` with this library,
* and the override set below won't work.
*
* Returns `true` if the tests are being run remotely, and
* `false` if the tests should be run locally.
*/
bool testRemote(Function main, SendPort port) {
if (port != null) {
unittestConfiguration = new RemoteConfiguration(port);
return false;
}
var testResponses = new Map<String, List>();
ClosureMirror closure = reflect(main);
LibraryMirror library = closure.function.owner;
var receivePort = new ReceivePort();
void remoteAction(message) {
switch (message[0]) {
case "testStart":
String name = message[1];
testResponses[name] = null;
break;
case "testResult":
case "testResultChanged":
String name = message[1];
testResponses[name] = message;
break;
case "logMessage":
break; // Ignore.
case "summary":
throw message[1]; // Uncaught error.
case "done":
receivePort.close();
_simulateTests(testResponses);
break;
}
}
try {
Isolate.spawnUri(library.uri, null, receivePort.sendPort);
receivePort.listen(remoteAction);
return true;
} catch (e) {
// spawnUri is only supported by dart2js if web workers are available.
// If the spawnUri fails, run the tests locally instead, since we are
// not in a browser anyway.
//
// That is, we assume that either Isolate.spawn or Isolate.spawnUri must
// work, so if spawnUri doesn't work, we can run the tests directly.
receivePort.close();
return false;
}
}
class RemoteConfiguration implements Configuration {
final SendPort _port;
Duration timeout = const Duration(minutes: 2);
RemoteConfiguration(this._port);
bool get autoStart => true;
void onInit() {}
void onStart() {}
void onTestStart(TestCase testCase) {
_port.send(["testStart", testCase.description]);
}
void onTestResult(TestCase testCase) {
_port.send([
"testResult",
testCase.description,
testCase.result,
testCase.message
]);
}
void onTestResultChanged(TestCase testCase) {
_port.send([
"testResultChanged",
testCase.description,
testCase.result,
testCase.message
]);
}
void onLogMessage(TestCase testCase, String message) {
_port.send(["logMessage", testCase.description, message]);
}
void onDone(bool success) {
_port.send(["done", success]);
}
void onSummary(int passed, int failed, int errors, List<TestCase> results,
String uncaughtError) {
if (uncaughtError != null) {
_port.send(["summary", uncaughtError]);
}
}
}
void _simulateTests(Map<String, List> responses) {
// Start all unit tests in the same event.
responses.forEach((name, response) {
test(name, () {
var result = response[2];
var message = response[3];
if (result == FAIL) {
fail(message);
} else if (result == ERROR) {
throw message;
}
});
});
}

View file

@ -8,8 +8,8 @@
library RequestReplyTest;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
void entry(initPort) {
ReceivePort port = new ReceivePort();
@ -23,17 +23,16 @@ void entry(initPort) {
}
void main([args, port]) {
if (testRemote(main, port)) return;
test("send", () {
ReceivePort init = new ReceivePort();
Isolate.spawn(entry, init.sendPort);
init.first.then(expectAsync((port) {
ReceivePort reply = new ReceivePort();
port.send([99, reply.sendPort]);
reply.listen(expectAsync((message) {
expect(message, 99 + 87);
reply.close();
}));
}));
ReceivePort init = new ReceivePort();
Isolate.spawn(entry, init.sendPort);
asyncStart();
init.first.then((port) {
ReceivePort reply = new ReceivePort();
port.send([99, reply.sendPort]);
reply.listen((message) {
Expect.equals(message, 99 + 87);
reply.close();
asyncEnd();
});
});
}

View file

@ -13,8 +13,7 @@
library spawn_tests;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:expect/expect.dart';
class MyClass {
final myVar = 'there';
@ -29,26 +28,21 @@ isolateEntryPoint(args) {
reply.send('re: ${new MyClass().myFunc(msg)}');
}
void main([args, port]) {
if (testRemote(main, port)) {
return;
}
Future<void> main([args, port]) async {
// message - reply chain'
final exitPort = ReceivePort();
final replyPort = ReceivePort();
test('message - reply chain', () async {
final exitPort = ReceivePort();
final replyPort = ReceivePort();
Isolate.spawn(isolateEntryPoint, ['hi', replyPort.sendPort],
onExit: exitPort.sendPort);
final isolate = Isolate.spawn(isolateEntryPoint, ['hi', replyPort.sendPort],
onExit: exitPort.sendPort);
replyPort.listen((msg) {
replyPort.close();
expect(msg, equals('re: hi there'));
});
// Explicitly await spawned isolate exit to enforce main isolate not
// completing (and the stand-alone runtime exiting) before the spawned
// isolate is done.
await exitPort.first;
replyPort.listen((msg) {
replyPort.close();
Expect.equals(msg, 're: hi there');
});
// Explicitly await spawned isolate exit to enforce main isolate not
// completing (and the stand-alone runtime exiting) before the spawned
// isolate is done.
await exitPort.first;
}

View file

@ -9,8 +9,8 @@
library spawn_tests;
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
isolateEntryPoint(args) {
var msg = args[0];
@ -18,29 +18,24 @@ isolateEntryPoint(args) {
sendPort.send('re: $msg');
}
void main([args, port]) {
if (testRemote(main, port)) {
return;
}
Future<void> main([args, port]) async {
// message - reply chain
const String debugName = 'spawnedIsolate';
test('message - reply chain', () async {
const String debugName = 'spawnedIsolate';
ReceivePort port = new ReceivePort();
port.listen(expectAsync((msg) {
port.close();
expect(msg, equals('re: hi'));
}));
// Start new isolate; paused so it's alive till we read the debugName.
// If the isolate runs to completion, the isolate might get cleaned up
// and debugName might be null.
final isolate = await Isolate.spawn(
isolateEntryPoint, ['hi', port.sendPort],
paused: true, debugName: debugName);
expect(isolate.debugName, debugName);
isolate.resume(isolate.pauseCapability);
ReceivePort port = new ReceivePort();
asyncStart();
port.listen((msg) {
port.close();
Expect.equals(msg, 're: hi');
asyncEnd();
});
// Start new isolate; paused so it's alive till we read the debugName.
// If the isolate runs to completion, the isolate might get cleaned up
// and debugName might be null.
final isolate = await Isolate.spawn(isolateEntryPoint, ['hi', port.sendPort],
paused: true, debugName: debugName);
Expect.equals(isolate.debugName, debugName);
isolate.resume(isolate.pauseCapability);
}

View file

@ -10,10 +10,9 @@
library static_function_test;
import 'dart:isolate';
import 'dart:async';
import 'static_function_lib.dart' as lib;
import 'package:unittest/unittest.dart';
import 'remote_unittest_helper.dart';
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
void function(SendPort port) {
port.send("TOP");
@ -95,26 +94,28 @@ class _C {
}
void spawnTest(name, function, response) {
test(name, () {
ReceivePort r = new ReceivePort();
Isolate.spawn(function, r.sendPort);
r.listen(expectAsync((v) {
expect(v, response);
r.close();
}));
print(name);
ReceivePort r = new ReceivePort();
Isolate.spawn(function, r.sendPort);
asyncStart();
r.listen((v) {
Expect.equals(v, response);
r.close();
asyncEnd();
});
}
void functionFailTest(name, function) {
test("throws on $name", () {
Isolate.spawn(function, null).catchError(expectAsync((e) {
/* do nothing */
}));
print("throws on $name");
asyncStart();
Isolate.spawn(function, null).catchError((e) {
/* do nothing */
asyncEnd();
});
}
void main([args, port]) {
if (testRemote(main, port)) return;
asyncStart();
// Sanity check.
spawnTest("function", function, "TOP");
spawnTest("_function", _function, "_TOP");
@ -144,4 +145,5 @@ void main([args, port]) {
functionFailTest(
"named constructor closure", new C().namedConstructorBodyClosure);
functionFailTest("instance method", new C().instanceMethod);
asyncEnd();
}

View file

@ -10,8 +10,8 @@ library unresolved_ports;
import 'dart:async';
import 'dart:isolate';
import 'package:unittest/unittest.dart';
import "remote_unittest_helper.dart";
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
// This test does the following:
// - main spawns two isolates: 'tim' and 'beth'
@ -57,32 +57,27 @@ ReceivePort initIsolate(SendPort starter) {
}
baseTest({bool failForNegativeTest: false}) {
test('Message chain with unresolved ports', () {
ReceivePort port = new ReceivePort();
port.listen(expectAsync((msg) {
expect(
msg,
equals('main says: Beth, find out if Tim is coming.'
'\nBeth says: Tim are you coming? And Bob?'
'\nTim says: Can you tell "main" that we are all coming?'
'\nBob says: we are all coming!'));
expect(failForNegativeTest, isFalse);
port.close();
}));
// Message chain with unresolved ports
ReceivePort port = new ReceivePort();
asyncStart();
port.listen((msg) {
Expect.equals(
msg,
'main says: Beth, find out if Tim is coming.'
'\nBeth says: Tim are you coming? And Bob?'
'\nTim says: Can you tell "main" that we are all coming?'
'\nBob says: we are all coming!');
Expect.isFalse(failForNegativeTest);
port.close();
asyncEnd();
});
spawnFunction(timIsolate).then((tim) {
spawnFunction(bethIsolate).then((beth) {
beth.send([
'main says: Beth, find out if Tim is coming.',
tim,
port.sendPort
]);
});
spawnFunction(timIsolate).then((tim) {
spawnFunction(bethIsolate).then((beth) {
beth.send(
['main says: Beth, find out if Tim is coming.', tim, port.sendPort]);
});
});
}
void main([args, port]) {
if (testRemote(main, port)) return;
baseTest();
}
void main([args, port]) => baseTest();