Files
sdk/tests/html/isolates_test.dart
T
Jacob Richman 2dcd56ef43 Format all tests.
There are far too many files here to review everyone carefully.
Spot checking most of the diffs look good as test code is generally written
with less care than application code so lots of ugly formatting get through.
If people notice files where the automated formatting bothers them feel free
to comment indicating file names and I'll move spaces within comments to make
the formatting cleaner and use comments to force block formatting as I have
done for other case where formatting looked bad.

BUG=
R=efortuna@google.com

Review-Url: https://codereview.chromium.org/2771453003 .
2017-04-17 14:53:02 -07:00

67 lines
1.7 KiB
Dart

library IsolatesTest;
import 'package:unittest/unittest.dart';
import 'package:unittest/html_config.dart';
import 'dart:async';
import 'dart:html';
import 'dart:convert';
import 'dart:isolate' as isolate;
String responseFor(message) => 'response for $message';
void isolateEntry(isolate.SendPort initialReplyTo) {
var port = new isolate.ReceivePort();
initialReplyTo.send(port.sendPort);
bool wasThrown = false;
try {
window.alert('Test');
} catch (e) {
wasThrown = true;
}
// If wasn't thrown, do not listen to messages to make test fail.
if (!wasThrown) {
return;
}
// Check that convert library was loaded to isolate.
JSON.encode([1, 2, 3]);
port.listen((message) {
var data = message[0];
var replyTo = message[1];
replyTo.send(responseFor(data));
});
}
Future sendReceive(isolate.SendPort port, msg) {
var response = new isolate.ReceivePort();
port.send([msg, response.sendPort]);
return response.first;
}
main() {
useHtmlConfiguration();
test('IsolateSpawn', () {
var port = new isolate.ReceivePort();
isolate.Isolate.spawn(isolateEntry, port.sendPort);
port.close();
});
test('NonDOMIsolates', () {
var callback = expectAsync(() {});
var response = new isolate.ReceivePort();
var remote = isolate.Isolate.spawn(isolateEntry, response.sendPort);
response.first.then((port) {
final msg1 = 'foo';
final msg2 = 'bar';
sendReceive(port, msg1).then((response) {
expect(response, equals(responseFor(msg1)));
sendReceive(port, msg2).then((response) {
expect(response, equals(responseFor(msg2)));
callback();
});
});
});
});
}