mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:30:32 +00:00
a2b556b937
Change-Id: Ia5c41dda6503e1fbb8cc6099835d07588425e2ba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128310 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
74 lines
1.8 KiB
Dart
74 lines
1.8 KiB
Dart
// Copyright (c) 2016, 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:async';
|
|
import "package:expect/expect.dart";
|
|
import "package:async_helper/async_helper.dart";
|
|
|
|
class A {
|
|
const A();
|
|
}
|
|
|
|
class B extends A {
|
|
const B();
|
|
}
|
|
|
|
/// Stream which emits an error if it's not canceled at the correct time.
|
|
///
|
|
/// Must be canceled after at most [maxEvents] events.
|
|
Stream makeStream(int maxEvents) {
|
|
var c;
|
|
int event = 0;
|
|
bool canceled = false;
|
|
c = new StreamController(onListen: () {
|
|
new Timer.periodic(const Duration(milliseconds: 10), (t) {
|
|
if (canceled) {
|
|
t.cancel();
|
|
return;
|
|
}
|
|
if (event == maxEvents) {
|
|
c.addError("NOT CANCELED IN TIME: $maxEvents");
|
|
c.close();
|
|
t.cancel();
|
|
} else {
|
|
c.add(event++);
|
|
}
|
|
});
|
|
}, onCancel: () {
|
|
canceled = true;
|
|
});
|
|
return c.stream;
|
|
}
|
|
|
|
main() {
|
|
asyncStart();
|
|
tests().then((_) {
|
|
asyncEnd();
|
|
});
|
|
}
|
|
|
|
tests() async {
|
|
await expectThrowsAsync(makeStream(4).take(5).toList(), "5/4");
|
|
await expectThrowsAsync(makeStream(0).take(1).toList(), "1/0");
|
|
|
|
Expect.listEquals([0, 1, 2, 3, 4], await makeStream(5).take(5).toList());
|
|
|
|
Expect.listEquals([0, 1, 2, 3], await makeStream(5).take(4).toList());
|
|
|
|
Expect.listEquals([0], await makeStream(5).take(1).toList());
|
|
|
|
Expect.listEquals([], await makeStream(5).take(0).toList());
|
|
|
|
Expect.listEquals([], await makeStream(0).take(0).toList());
|
|
|
|
// Regression for https://github.com/dart-lang/sdk/issues/30305
|
|
Expect.equals('result', await makeStream(5).take(0).drain('result'));
|
|
}
|
|
|
|
Future expectThrowsAsync(Future computation, String name) {
|
|
return computation.then((_) {
|
|
Expect.fail("$name: Did not throw");
|
|
}, onError: (e, s) {});
|
|
}
|