-
Notifications
You must be signed in to change notification settings - Fork 29
#2930. Add MultiStreamController tests. Part 1. #2936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
LibTest/async/MultiStreamController/addErrorSync_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
46
LibTest/async/MultiStreamController/addErrorSync_A01_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
47
LibTest/async/MultiStreamController/addErrorSync_A02_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| /// | ||
| /// @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(); | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| })); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
52
LibTest/async/MultiStreamController/addStream_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
59
LibTest/async/MultiStreamController/addStream_A02_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
59
LibTest/async/MultiStreamController/addStream_A02_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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), theaddErrorSyncwould still be enqueued.