dart-sdk/tests/lib/html/isolates_test.dart
Srujan Gaddam a925f4770f [dart:html] Remove 2.7 annotation from lib/html
Strips out the temporary annotation so all the tests without
NNBD related changes can run in strong mode.

Change-Id: I88f18916f0c5a45a80eddb73941db19984f0bfcf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140652
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
2020-04-06 20:41:14 +00:00

69 lines
1.8 KiB
Dart

// Copyright (c) 2020, 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.
library IsolatesTest;
import 'package:async_helper/async_minitest.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() {
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();
});
});
});
});
}