Skip to content
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

try to catch issue with subjects #697

Merged
merged 10 commits into from
Jan 11, 2025
Merged
Changes from 5 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
20 changes: 20 additions & 0 deletions src/tests/rpp/test_subjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ TEST_CASE("subject can be modified from on_next call")
}
}

TEST_CASE("subject handles addition from inside on_next properly")
{
rpp::subjects::publish_subject<int> subject{};

SUBCASE("subscribe inside on_next")
{
int value = {};
subject.get_observable().subscribe([&subject, &value](int v) {
for (int i = 0; i < 100; ++i)
subject.get_observable().subscribe([](int) {});
value = v;
Comment on lines +179 to +182
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve subscription management in test

The test creates 100 subscriptions without proper cleanup, which could lead to resource leaks.

Apply this diff to properly manage subscriptions:

-            for (int i = 0; i < 100; ++i)
-                subject.get_observable().subscribe([](int) {});
+            auto d = rpp::composite_disposable_wrapper::make();
+            for (int i = 0; i < 100; ++i)
+                subject.get_observable().subscribe(d, [](int) {});

Committable suggestion skipped: line range outside the PR's diff.

});

for (int i = 0; i < 100; ++i)
subject.get_observer().on_next(i);

REQUIRE(value == 99);
}
}

TEST_CASE("publish subject caches error/completed")
{
auto mock = mock_observer_strategy<int>{};
Expand Down
Loading