ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.03k stars 390 forks source link

question: writing tests with virtual_time #537

Open pck opened 4 years ago

pck commented 4 years ago

Hello, thank you for all your work on this library.

I'm not sure how to write virtual_time-tests for objects that use RxCpp internally and rely on a run_loop in production.

My current approach is:

auto time = std::make_shared<rxcpp::schedulers::detail::test_type>()
    ->create_test_type_worker_interface();
rxcpp::schedulers::detail::action_queue::ensure(time);
... create things, test initial state ...
time->advance_by(1000);
... test changed state ...

This doesn't work out of the box because the virtual time isn't used to schedule new work. Is the following change reasonable? (Maybe with an "#ifdef RXCPP_TESTING" guard to opt-in?)

--- Rx/v2/src/rxcpp/schedulers/rx-currentthread.hpp
+++ Rx/v2/src/rxcpp/schedulers/rx-currentthread.hpp
@@ -176,6 +176,8 @@ private:
         }

         virtual clock_type::time_point now() const {
+            if (queue_type::owned())
+                return queue_type::get_worker_interface()->now();
             return clock_type::now();
         }

Can you suggest a better approach?