Skip to content

Commit 16551e0

Browse files
author
sgrekhov
committed
dart-lang#1363. Tests for checking cancelling subscription for yield and yield* added.
1 parent cf11d72 commit 16551e0

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion An async* function returns a stream. Listening on that stream
6+
/// executes the function body linked to the stream subscription returned by
7+
/// that listen call.
8+
///
9+
/// A yield e; statement is specified such that it must successfully deliver the
10+
/// event to the subscription before continuing. If the subscription is
11+
/// canceled, delivering succeeds and does nothing, if the subscription is a
12+
/// paused, delivery succeeds when the event is accepted and buffered.
13+
/// Otherwise, deliver is successful after the subscription's event listener has
14+
/// been invoked with the event object.
15+
///
16+
/// After this has happened, the subscription is checked for being canceled or
17+
/// paused. If paused, the function is blocked at the yield statement until the
18+
/// subscription is resumed or canceled. In this case the yield is an
19+
/// asynchronous operation (it does not complete synchronously, but waits for an
20+
/// external event, the resume, before it continues). If canceled, including if
21+
/// the cancel happens during a pause, the yield statement acts like a return;
22+
/// statement.
23+
///
24+
/// @description Check that if the subscription is canceled, delivering succeeds
25+
/// and does nothing
26+
///
27+
/// @author [email protected]
28+
29+
import 'dart:async';
30+
import '../../../../Utils/expect.dart';
31+
32+
Stream<int> generator() async* {
33+
for (int i = 1; i <= 3; i++) {
34+
yield i;
35+
}
36+
}
37+
38+
test() async {
39+
List log = [];
40+
Stream<int> s = generator();
41+
late StreamSubscription<int> ss;
42+
ss = s.listen((int i) {
43+
log.add(i);
44+
ss.cancel();
45+
});
46+
await Future.delayed(Duration(seconds: 1));
47+
Expect.listEquals([1], log);
48+
asyncEnd();
49+
}
50+
51+
main() {
52+
asyncStart();
53+
test();
54+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion A yield* e; statement listens on the stream that e evaluates to
6+
/// and forwards all events to this function's subscription. If the subscription
7+
/// is paused, the pause is forwarded to the yielded stream. If the subscription
8+
/// is canceled, the cancel is forwarded to the yielded stream, then the yield*
9+
/// statement waits for any cancellation future, and finally it acts like a
10+
/// return; statement. If the yielded stream completes, the yield statement
11+
/// completes normally. A yield* is always an asynchronous operation.
12+
///
13+
/// @description Check that if the subscription is canceled, the cancel is
14+
/// forwarded to the yielded stream, then the yield* statement waits for any
15+
/// cancellation future, and finally it acts like a return; statement.
16+
///
17+
/// @author [email protected]
18+
19+
import 'dart:async';
20+
import '../../../../Utils/expect.dart';
21+
22+
Stream<int> generator([bool generate = false]) async* {
23+
if (generate) {
24+
yield* generator(true);
25+
} else {
26+
for (int i = 1; i <= 3; i++) {
27+
yield i;
28+
}
29+
}
30+
}
31+
32+
test() async {
33+
List log = [];
34+
Stream<int> s = generator();
35+
late StreamSubscription<int> ss;
36+
ss = s.listen((int i) {
37+
log.add(i);
38+
ss.cancel();
39+
});
40+
await Future.delayed(Duration(seconds: 1));
41+
Expect.listEquals([1], log);
42+
asyncEnd();
43+
}
44+
45+
main() {
46+
asyncStart();
47+
test();
48+
}

0 commit comments

Comments
 (0)