|
| 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 | + |
| 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 | +} |
0 commit comments