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
44 changes: 44 additions & 0 deletions LibTest/async/MultiStreamController/addErrorSync_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 addErrorSync( Object error, [ StackTrace? stackTrace ])
/// Adds and delivers an error event.
///
/// Adds an error like [addError] and attempts to deliver it immediately.
/// Delivery can be delayed if other previously added events are still pending
/// delivery, if the subscription is paused, or if the subscription isn't
/// listening yet.
///
/// @description Checks that this method sends an error event.
/// @author sgrekhov22@gmail.com

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

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

void listen(Stream<int> stream) {
int i = 1;
bool delivered = false;
stream.listen((v) {
Expect.equals(i, v);
i += 2;
}, onError: (e) {
delivered = true;
Expect.equals(2, e);
}, onDone: () {
Expect.isTrue(delivered);
asyncEnd();
});
}
46 changes: 46 additions & 0 deletions LibTest/async/MultiStreamController/addErrorSync_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 addErrorSync( Object error, [ StackTrace? stackTrace ])
/// Adds and delivers an error event.
///
/// Adds an error like [addError] and attempts to deliver it immediately.
/// Delivery can be delayed if other previously added events are still pending
/// delivery, if the subscription is paused, or if the subscription isn't
/// listening yet.
///
/// @description Checks that this method sends an error event and a stack trace.
/// @author sgrekhov22@gmail.com

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

StackTrace st = StackTrace.fromString("Stack trace");

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

void listen(Stream<int> stream) {
int i = 1;
bool delivered = false;
stream.listen((v) {
Expect.equals(i, v);
i += 2;
}, onError: (e, _st) {
delivered = true;
Expect.equals("Stack trace", _st.toString());
}, onDone: () {
Expect.isTrue(delivered);
asyncEnd();
});
}
47 changes: 47 additions & 0 deletions LibTest/async/MultiStreamController/addErrorSync_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 addErrorSync( Object error, [ StackTrace? stackTrace ])
/// Adds and delivers an error event.
///
/// Adds an error like [addError] and attempts to deliver it immediately.
/// Delivery can be delayed if other previously added events are still pending
/// delivery, if the subscription is paused, or if the subscription isn't
/// listening yet.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... where "the subsciprion isn't listening yet" includes the entire synchronous run of onListen.
So even without the earlier add(1), the addErrorSync would still be enqueued.

///
/// @description Checks that delivery can be delayed if subscription is paused.
/// @author sgrekhov22@gmail.com

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

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

void listen(Stream<int> stream) {
int i = 1;
bool delivered = false;
StreamSubscription ss = stream.listen((v) {
Expect.equals(i, v);
i += 2;
}, onError: (e) {
delivered = true;
Expect.equals(2, e);
}, onDone: () {
Expect.isTrue(delivered);
asyncEnd();
});
ss.pause(Future<void>.delayed(Duration(milliseconds: 100), () {
ss.resume();
}));
}
39 changes: 39 additions & 0 deletions LibTest/async/MultiStreamController/addError_A01_t01.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 addError( Object error, [ StackTrace? stackTrace ])
/// Sends or enqueues an error event.
///
/// @description Checks that this method sends an error event.
/// @author sgrekhov22@gmail.com

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

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

void listen(Stream<int> stream) {
int i = 1;
bool delivered = false;
stream.listen((v) {
Expect.equals(i, v);
i += 2;
}, onError: (e) {
delivered = true;
Expect.equals(2, e);
}, onDone: () {
Expect.isTrue(delivered);
asyncEnd();
});
}
41 changes: 41 additions & 0 deletions LibTest/async/MultiStreamController/addError_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 addError( Object error, [ StackTrace? stackTrace ])
/// Sends or enqueues an error event.
///
/// @description Checks that this method sends an error event and a stack trace.
/// @author sgrekhov22@gmail.com

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

StackTrace st = StackTrace.fromString("Stack trace");

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

void listen(Stream<int> stream) {
int i = 1;
bool delivered = false;
stream.listen((v) {
Expect.equals(i, v);
i += 2;
}, onError: (e, _st) {
delivered = true;
Expect.equals("Stack trace", _st.toString());
}, onDone: () {
Expect.isTrue(delivered);
asyncEnd();
});
}
52 changes: 52 additions & 0 deletions LibTest/async/MultiStreamController/addStream_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 Future addStream(
/// Stream<T> source, {
/// bool? cancelOnError,
/// })
/// Receives events from source and puts them into this controller's stream.
///
/// Returns a future which completes when the source stream is done.
///
/// Events must not be added directly to this controller using `add`,
/// `addError`, `close` or `addStream`, until the returned future is complete.
///
/// Data and error events are forwarded to this controller's stream. A done
/// event on the source will end the addStream operation and complete the
/// returned future.
///
/// If `cancelOnError` is `true`, only the first error on `source` is forwarded
/// to the controller's stream, and the `addStream` ends after this. If
/// `cancelOnError` is `false`, all errors are forwarded and only a done event
/// will end the `addStream`. If `cancelOnError` is omitted or null, it defaults
/// to `false`.
///
/// @description Checks that this method receives events from source and puts
/// them into this controller's stream.
/// @author sgrekhov22@gmail.com

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

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
controller.addStream(Stream.fromIterable([1, 2, 3])).then((_) {
controller.close();
});
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
int i = 1;
stream.listen((v) {
Expect.equals(i++, v);
}, onDone: () {
Expect.equals(4, i);
asyncEnd();
});
}
59 changes: 59 additions & 0 deletions LibTest/async/MultiStreamController/addStream_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 Future addStream(
/// Stream<T> source, {
/// bool? cancelOnError,
/// })
/// Receives events from source and puts them into this controller's stream.
///
/// Returns a future which completes when the source stream is done.
///
/// Events must not be added directly to this controller using `add`,
/// `addError`, `close` or `addStream`, until the returned future is complete.
///
/// Data and error events are forwarded to this controller's stream. A done
/// event on the source will end the addStream operation and complete the
/// returned future.
///
/// If `cancelOnError` is `true`, only the first error on `source` is forwarded
/// to the controller's stream, and the `addStream` ends after this. If
/// `cancelOnError` is `false`, all errors are forwarded and only a done event
/// will end the `addStream`. If `cancelOnError` is omitted or null, it defaults
/// to `false`.
///
/// @description Checks that it is a run-time error if events are added to the
/// controller via `add` until the returned future is complete.
/// @author sgrekhov22@gmail.com

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

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
controller
.addStream(
Stream.fromFuture(Future.delayed(Duration(seconds: 1), () => 42)))
.then((_) {
controller.close();
});
Expect.throws(() {
controller.add(1);
});
});
listen(stream);
listen(stream);
}

void listen(Stream<int> stream) {
int callCount = 0;
stream.listen((v) {
callCount++;
Expect.equals(42, v);
}, onDone: () {
Expect.equals(1, callCount);
asyncEnd();
});
}
59 changes: 59 additions & 0 deletions LibTest/async/MultiStreamController/addStream_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 Future addStream(
/// Stream<T> source, {
/// bool? cancelOnError,
/// })
/// Receives events from source and puts them into this controller's stream.
///
/// Returns a future which completes when the source stream is done.
///
/// Events must not be added directly to this controller using `add`,
/// `addError`, `close` or `addStream`, until the returned future is complete.
///
/// Data and error events are forwarded to this controller's stream. A done
/// event on the source will end the addStream operation and complete the
/// returned future.
///
/// If `cancelOnError` is `true`, only the first error on `source` is forwarded
/// to the controller's stream, and the `addStream` ends after this. If
/// `cancelOnError` is `false`, all errors are forwarded and only a done event
/// will end the `addStream`. If `cancelOnError` is omitted or null, it defaults
/// to `false`.
///
/// @description Checks that it is a run-time error if events are added to the
/// controller via `addSync` until the returned future is complete.
/// @author sgrekhov22@gmail.com

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

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
controller
.addStream(
Stream.fromFuture(Future.delayed(Duration(seconds: 1), () => 42)))
.then((_) {
controller.close();
});
Expect.throws(() {
controller.addSync(1);
});
});
listen(stream);
listen(stream);
}

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