diff --git a/src/tests/rpp/test_debounce.cpp b/src/tests/rpp/test_debounce.cpp index 515afcb1b..8ff4c3033 100644 --- a/src/tests/rpp/test_debounce.cpp +++ b/src/tests/rpp/test_debounce.cpp @@ -22,7 +22,8 @@ SCENARIO("debounce emit only items where timeout reached", "[operators][debounce GIVEN("subject of items and subscriber subscribed on it via debounce") { auto mock = mock_observer{}; - auto subj = rpp::subjects::publish_subject{}; + std::optional> optional_subj{rpp::subjects::publish_subject{}}; + auto& subj = optional_subj.value(); subj.get_observable().debounce(debounce_delay, scheduler).subscribe(mock); WHEN("emit value") { @@ -96,6 +97,19 @@ SCENARIO("debounce emit only items where timeout reached", "[operators][debounce } } } + AND_WHEN("subject destoryed and then schedulable reaches schedulable") + { + optional_subj.reset(); + scheduler.time_advance(debounce_delay); + THEN("emission reached mock") + { + CHECK(scheduler.get_schedulings() == std::vector{start+debounce_delay}); + CHECK(scheduler.get_executions() == std::vector{start+debounce_delay}); + CHECK(mock.get_received_values() == std::vector{1}); + CHECK(mock.get_on_error_count() == 0); + CHECK(mock.get_on_completed_count() == 0); + } + } } } } diff --git a/src/tests/rpp/test_delay.cpp b/src/tests/rpp/test_delay.cpp index bdd78ea7c..779da8cf5 100644 --- a/src/tests/rpp/test_delay.cpp +++ b/src/tests/rpp/test_delay.cpp @@ -9,6 +9,7 @@ // Project home: https://github.com/victimsnino/ReactivePlusPlus #include "mock_observer.hpp" +#include #include #include @@ -148,5 +149,18 @@ SCENARIO("delay mirrors both source observable and trigger observable", "[delay] } } } + WHEN("subscribe on subject via delay via test_scheduler, sent value") + { + subj.get_observable() + .delay(std::chrono::seconds{30000}, test_scheduler{}) + .subscribe(mock); + + subj.get_subscriber().on_next(1); + + AND_THEN("no memory leak") + { + // checked via sanitizer + } + } } } diff --git a/src/tests/utils/test_scheduler.hpp b/src/tests/utils/test_scheduler.hpp index 6f9abb0c9..3b96bcd81 100644 --- a/src/tests/utils/test_scheduler.hpp +++ b/src/tests/utils/test_scheduler.hpp @@ -58,23 +58,26 @@ class test_scheduler final : public rpp::schedulers::details::scheduler_tag class worker_strategy { public: - worker_strategy(std::shared_ptr state) + worker_strategy(std::weak_ptr state) : m_state{state} { } void defer_at(rpp::schedulers::time_point time_point, rpp::schedulers::constraint::schedulable_fn auto&& fn) const { - if (m_state->sub.is_subscribed()) + if (auto locked = m_state.lock()) { - m_state->schedule(time_point, std::forward(fn)); - m_state->drain(); + if (locked->sub.is_subscribed()) + { + locked->schedule(time_point, std::forward(fn)); + locked->drain(); + } } } static rpp::schedulers::time_point now() { return s_current_time; } private: - std::shared_ptr m_state; + std::weak_ptr m_state; }; test_scheduler() {}