Asyncstar - exception thrown before yield or await would not get caught

BUG=
R=floitsch@google.com

Review URL: https://codereview.chromium.org//947433002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@43948 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
sigurdm@google.com
2015-02-23 11:25:41 +00:00
parent d30966e628
commit ced919f9c4
2 changed files with 85 additions and 17 deletions
@@ -3724,16 +3724,18 @@ class AsyncStarStreamController {
addError(error, stackTrace) => controller.addError(error, stackTrace);
close() => controller.close();
AsyncStarStreamController(helperCallback) {
AsyncStarStreamController(body) {
controller = new StreamController(
onListen: () {
scheduleMicrotask(() {
JS('', '#(#, null)', helperCallback, async_error_codes.SUCCESS);
Function wrapped = _wrapJsFunctionForAsync(body,
async_error_codes.SUCCESS);
wrapped(null);
});
},
onResume: () {
if (!isAdding) {
asyncStarHelper(null, helperCallback, this);
asyncStarHelper(null, body, this);
}
}, onCancel: () {
stopRunning = true;
@@ -3741,8 +3743,8 @@ class AsyncStarStreamController {
}
}
makeAsyncStarController(helperCallback) {
return new AsyncStarStreamController(helperCallback);
makeAsyncStarController(body) {
return new AsyncStarStreamController(body);
}
class IterationMarker {
@@ -9,6 +9,7 @@ import "package:async_helper/async_helper.dart";
class Tracer {
final String expected;
final String name;
String _trace = "";
int counter = 0;
Tracer(this.expected, [this.name]);
@@ -17,12 +18,12 @@ class Tracer {
if (name != null) {
print("Tracing $name: $msg");
}
Expect.equals(expected[counter], msg);
_trace += msg;
counter++;
}
void done() {
Expect.equals(expected.length, counter, "Received too few traces");
Expect.equals(expected, _trace);
}
}
@@ -33,6 +34,55 @@ foo1(Tracer tracer) async* {
tracer.trace("b");
throw "Error";
} catch (e) {
Expect.equals("Error", e);
tracer.trace("c");
yield 1;
tracer.trace("d");
yield 2;
tracer.trace("e");
yield 3;
tracer.trace("f");
} finally {
tracer.trace("f");
}
tracer.trace("g");
}
foo2(Tracer tracer) async* {
try {
tracer.trace("a");
throw "Error";
} catch (error) {
Expect.equals("Error", error);
tracer.trace("b");
rethrow;
} finally {
tracer.trace("c");
}
}
foo3(Tracer tracer) async* {
try {
tracer.trace("a");
throw "Error";
} catch (error) {
Expect.equals("Error", error);
tracer.trace("b");
rethrow;
} finally {
tracer.trace("c");
yield 1;
}
}
foo4(Tracer tracer) async* {
try {
tracer.trace("a");
await new Future.value(3);
tracer.trace("b");
throw "Error";
} catch (e) {
Expect.equals("Error", e);
tracer.trace("c");
yield 1;
tracer.trace("d");
@@ -45,18 +95,34 @@ foo1(Tracer tracer) async* {
tracer.trace("g");
}
test() async {
Tracer tracer;
Completer foo1Done = new Completer();
tracer = new Tracer("abcdf");
runTest(test, expectedTrace, expectedError, shouldCancel) {
Tracer tracer = new Tracer(expectedTrace, expectedTrace);
Completer done = new Completer();
var subscription;
subscription = foo1(tracer).listen((event) async {
await subscription.cancel();
tracer.done();
foo1Done.complete(null);
subscription = test(tracer).listen((event) async {
tracer.trace("Y");
if (shouldCancel) {
await subscription.cancel();
tracer.trace("C");
done.complete(null);
}
}, onError: (error) {
Expect.equals(expectedError, error);
tracer.trace("X");
}, onDone: () {
tracer.done();
done.complete(null);
});
await foo1Done.future;
return done.future.then((_) => tracer.done());
}
test() async {
// TODO(sigurdm): These tests are too dependent on scheduling, and buffering
// behavior.
await runTest(foo1, "abcdYefC", null, true);
await runTest(foo2, "abcX", "Error", false);
await runTest(foo3, "abcYX", "Error", false);
await runTest(foo4, "abcdYeYfX", "Error2", false);
}