Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions LibTest/async/MultiStreamController/isPaused_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024, 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.

/// @assertion bool get isPaused
/// Whether the subscription would need to buffer events.
///
/// This is the case if the controller's stream has a listener and it is paused,
/// or if it has not received a listener yet. In that case, the controller is
/// considered paused as well.
///
/// A broadcast stream controller is never considered paused. It always forwards
/// its events to all uncanceled subscriptions, if any, and let the
/// subscriptions handle their own pausing and buffering.
///
/// @description Checks that this getter returns `true` if the stream is paused
/// and `false` otherwise.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
var controllers = <MultiStreamController<int>>[];
var stream = Stream<int>.multi((controller) {
controllers.add(controller);
Expect.isFalse(controller.isPaused);
controller.add(1);
controller.add(2);
controller.add(3);
});
listen(stream);
listen(stream);
Future.delayed(Duration(milliseconds: 200), () {
controllers.forEach((c) {
Expect.isTrue(c.isPaused);
});
asyncEnd();
});
}

void listen(Stream<int> stream) {
late StreamSubscription ss;
ss = stream.listen((v) {
ss.pause();
});
}
48 changes: 48 additions & 0 deletions LibTest/async/MultiStreamController/isPaused_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024, 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.

/// @assertion bool get isPaused
/// Whether the subscription would need to buffer events.
///
/// This is the case if the controller's stream has a listener and it is paused,
/// or if it has not received a listener yet. In that case, the controller is
/// considered paused as well.
///
/// A broadcast stream controller is never considered paused. It always forwards
/// its events to all uncanceled subscriptions, if any, and let the
/// subscriptions handle their own pausing and buffering.
///
/// @description Checks that this getter returns `true` if the stream is paused
/// and `false` otherwise. Test `isBroadcast: true` case.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
var controllers = <MultiStreamController<int>>[];
var stream = Stream<int>.multi((controller) {
controllers.add(controller);
Expect.isFalse(controller.isPaused);
controller.add(1);
controller.add(2);
controller.add(3);
}, isBroadcast: true);
listen(stream);
listen(stream);
Future.delayed(Duration(milliseconds: 200), () {
controllers.forEach((c) {
Expect.isTrue(c.isPaused);
});
asyncEnd();
});
}

void listen(Stream<int> stream) {
late StreamSubscription ss;
ss = stream.listen((v) {
ss.pause();
});
}
50 changes: 50 additions & 0 deletions LibTest/async/MultiStreamController/isPaused_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2024, 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.

/// @assertion bool get isPaused
/// Whether the subscription would need to buffer events.
///
/// This is the case if the controller's stream has a listener and it is paused,
/// or if it has not received a listener yet. In that case, the controller is
/// considered paused as well.
///
/// A broadcast stream controller is never considered paused. It always forwards
/// its events to all uncanceled subscriptions, if any, and let the
/// subscriptions handle their own pausing and buffering.
///
/// @description Checks that this getter returns `true` if the stream is paused
/// and `false` otherwise.
/// @author sgrekhov22@gmail.com
/// @issue 56915

import "dart:async";
import "../../../Utils/expect.dart";

var theController;

main() {
asyncStart();
var stream = Stream<int>.multi((controller) {
theController = controller;
Expect.isFalse(controller.isPaused);
controller.add(1);
controller.add(2);
controller.add(3);
controller.close();
});
listen(stream);
}

void listen(Stream<int> stream) {
late StreamSubscription ss;
ss = stream.listen((v) {
if (v == 1) {
ss.pause(Future.delayed(Duration(milliseconds: 100)));
} else {
// Looks strange, but it is expected.
// See https://github.com/dart-lang/sdk/issues/56915 for more details.
Expect.isTrue(theController.isPaused);
}
}, onDone: asyncEnd);
}
34 changes: 34 additions & 0 deletions LibTest/async/MultiStreamController/onCancel_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2024, 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.

/// @assertion FutureOr<void> Function()? onCancel
/// The callback which is called when the stream is canceled.
///
/// May be set to `null`, in which case no callback will happen.
///
/// @description Checks that this callback is called when the stream is canceled
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
Expect.isNull(controller.onCancel);
controller.onCancel = asyncEnd;
controller.add(1);
controller.add(2);
controller.add(3);
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
late StreamSubscription ss;
ss = stream.listen((v) {
ss.cancel();
});
}
34 changes: 34 additions & 0 deletions LibTest/async/MultiStreamController/onCancel_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2024, 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.

/// @assertion FutureOr<void> Function()? onCancel
/// The callback which is called when the stream is canceled.
///
/// May be set to `null`, in which case no callback will happen.
///
/// @description Checks that this callback is called on "done" event.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
controller.onCancel = asyncEnd;
controller.add(1);
controller.add(2);
controller.add(3);
controller.close();
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
int i = 0;
stream.listen((v) {
Expect.equals(++i, v);
});
}
39 changes: 39 additions & 0 deletions LibTest/async/MultiStreamController/onCancel_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024, 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.

/// @assertion FutureOr<void> Function()? onCancel
/// The callback which is called when the stream is canceled.
///
/// May be set to `null`, in which case no callback will happen.
///
/// @description Checks that this callback is not called when the stream is
/// paused.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
var stream = Stream<int>.multi((controller) {
controller.onCancel = () {
Expect.fail("Unexpected onCancel");
};
controller.add(1);
controller.add(2);
controller.add(3);
});
listen(stream);
listen(stream);
Future.delayed(Duration(milliseconds: 200), asyncEnd);
}

void listen(Stream<int> stream) {
late StreamSubscription ss;
ss = stream.listen((v) {
if (v == 1) {
ss.pause();
}
});
}
35 changes: 35 additions & 0 deletions LibTest/async/MultiStreamController/onCancel_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2024, 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.

/// @assertion FutureOr<void> Function()? onCancel
/// The callback which is called when the stream is canceled.
///
/// May be set to `null`, in which case no callback will happen.
///
/// @description Checks that if this callback is set to `null`, then no callback
/// happens.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
controller.onCancel = null;
controller.add(1);
controller.add(2);
controller.add(3);
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
late StreamSubscription ss;
ss = stream.listen((v) {
ss.cancel();
asyncEnd();
});
}
42 changes: 42 additions & 0 deletions LibTest/async/MultiStreamController/onListen_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024, 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.

/// @assertion void Function()? onListen
///
/// The callback which is called when the stream is listened to.
///
/// May be set to null, in which case no callback will happen.
///
/// @description Checks that setting the `onListen` on the
/// [MultiStreamController] has no effect, the one subscription that the
/// controller applies to has already started listening.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
controller.onListen = () {
Expect.fail("Unexpected onListen");
};
controller.add(1);
controller.add(2);
controller.add(3);
controller.close();
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
int i = 0;
stream.listen((v) {
Expect.equals(++i, v);
}, onDone: () {
Expect.equals(3, i);
asyncEnd();
});
}
39 changes: 39 additions & 0 deletions LibTest/async/MultiStreamController/onListen_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024, 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.

/// @assertion void Function()? onListen
///
/// The callback which is called when the stream is listened to.
///
/// May be set to null, in which case no callback will happen.
///
/// @description Checks that when [MultiStreamController] is listened `onListen`
/// callback is set.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
Expect.isNotNull(controller.onListen);
controller.add(1);
controller.add(2);
controller.add(3);
controller.close();
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
int i = 0;
stream.listen((v) {
Expect.equals(++i, v);
}, onDone: () {
Expect.equals(3, i);
asyncEnd();
});
}
Loading