-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I049f6b1bf684ed28b6f14d380adfadf9f4e97fcb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217008 Reviewed-by: Nate Bosch <nbosch@google.com> Reviewed-by: Michael Thomsen <mit@google.com> Commit-Queue: Lasse Nielsen <lrn@google.com>
- Loading branch information
Showing
4 changed files
with
399 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) 2022, 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. | ||
|
||
import 'dart:isolate'; | ||
import 'dart:async'; | ||
import 'package:async_helper/async_helper.dart'; | ||
import 'package:expect/expect.dart'; | ||
|
||
void main() async { | ||
asyncStart(); | ||
// Sending result back. | ||
await testValue(); | ||
await testAsyncValue(); | ||
// Sending error from computation back. | ||
await testError(); | ||
await testAsyncError(); | ||
// Sending uncaught async error back. | ||
await testUncaughtError(); | ||
// Not sending anything back before isolate dies. | ||
await testIsolateHangs(); | ||
await testIsolateKilled(); | ||
await testIsolateExits(); | ||
asyncEnd(); | ||
} | ||
|
||
final StackTrace stack = StackTrace.fromString("Known Stacktrace"); | ||
final ArgumentError error = ArgumentError.value(42, "name"); | ||
|
||
var variable = 0; | ||
|
||
Future<void> testValue() async { | ||
var value = await Isolate.run<int>(() { | ||
variable = 1; // Changed in other isolate! | ||
Expect.equals(1, variable); | ||
return 42; | ||
}); | ||
Expect.equals(42, value); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testAsyncValue() async { | ||
var value = await Isolate.run<int>(() async { | ||
variable = 1; | ||
return 42; | ||
}); | ||
Expect.equals(42, value); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testError() async { | ||
var e = await asyncExpectThrows<ArgumentError>(Isolate.run<int>(() { | ||
variable = 1; | ||
Error.throwWithStackTrace(error, stack); | ||
})); | ||
Expect.equals(42, e.invalidValue); | ||
Expect.equals("name", e.name); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testAsyncError() async { | ||
var e = await asyncExpectThrows<ArgumentError>(Isolate.run<int>(() async { | ||
variable = 1; | ||
Error.throwWithStackTrace(error, stack); | ||
})); | ||
Expect.equals(42, e.invalidValue); | ||
Expect.equals("name", e.name); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testUncaughtError() async { | ||
var e = await asyncExpectThrows<RemoteError>(Isolate.run<int>(() async { | ||
variable = 1; | ||
unawaited(Future.error(error, stack)); // Uncaught error | ||
await Completer().future; // Never completes. | ||
return -1; | ||
})); | ||
|
||
Expect.type<RemoteError>(e); | ||
Expect.equals(error.toString(), e.toString()); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testIsolateHangs() async { | ||
var e = await asyncExpectThrows<RemoteError>(Isolate.run<int>(() async { | ||
variable = 1; | ||
await Completer<Never>().future; // Never completes. | ||
// Isolate should end while hanging here, because its event loop is empty. | ||
})); | ||
Expect.type<RemoteError>(e); | ||
Expect.equals("Computation ended without result", e.toString()); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testIsolateKilled() async { | ||
var e = await asyncExpectThrows<RemoteError>(Isolate.run<int>(() async { | ||
variable = 1; | ||
Isolate.current.kill(); // Send kill request. | ||
await Completer<Never>().future; // Never completes. | ||
// Isolate should get killed while hanging here. | ||
})); | ||
Expect.type<RemoteError>(e); | ||
Expect.equals("Computation ended without result", e.toString()); | ||
Expect.equals(0, variable); | ||
} | ||
|
||
Future<void> testIsolateExits() async { | ||
var e = await asyncExpectThrows<RemoteError>(Isolate.run<int>(() async { | ||
variable = 1; | ||
Isolate.exit(); // Dies here without sending anything back. | ||
})); | ||
Expect.type<RemoteError>(e); | ||
Expect.equals("Computation ended without result", e.toString()); | ||
Expect.equals(0, variable); | ||
} |
Oops, something went wrong.