-
Notifications
You must be signed in to change notification settings - Fork 29
#2933. Add MultiStreamController tests. Part 4. #2941
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
7 commits
Select commit
Hold shift + click to select a range
90db101
#2933. Add MultiStreamController tests. Part 4.
sgrekhov af17cef
Update `onResume` tests
sgrekhov e385410
Implement review recommendations
sgrekhov dbca77a
Remove setting onSomeEvent tests to null
sgrekhov e471b9f
Implement review recommendations
sgrekhov 4346b80
Readd setting `onSomeEvent` tests
sgrekhov 7584935
Implement review recommendations
sgrekhov 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
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,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(); | ||
| }); | ||
| } | ||
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,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. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// @description Checks that this getter returns `true` if the stream is paused | ||
| /// and `false` otherwise. Test `isBroadcast: true` case. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import "dart:async"; | ||
| import "../../../Utils/expect.dart"; | ||
|
|
||
| main() { | ||
| asyncStart(); | ||
| var controllers = <MultiStreamController<int>>[]; | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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(); | ||
| }); | ||
| } | ||
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,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); | ||
| } |
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,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 | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// @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(); | ||
| }); | ||
| } | ||
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,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); | ||
| }); | ||
| } |
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 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(); | ||
| } | ||
| }); | ||
| } |
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,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; | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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(); | ||
| }); | ||
| } | ||
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,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(); | ||
| }); | ||
| } |
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 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(); | ||
| }); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.