-
Notifications
You must be signed in to change notification settings - Fork 37
Make test_scheduler more smart #278
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,58 +1,96 @@ | ||||||||||
| // ReactivePlusPlus library | ||||||||||
| // | ||||||||||
| // | ||||||||||
| // Copyright Aleksey Loginov 2022 - present. | ||||||||||
| // Distributed under the Boost Software License, Version 1.0. | ||||||||||
| // (See accompanying file LICENSE_1_0.txt or copy at | ||||||||||
| // https://www.boost.org/LICENSE_1_0.txt) | ||||||||||
| // | ||||||||||
| // | ||||||||||
| // Project home: https://github.com/victimsnino/ReactivePlusPlus | ||||||||||
|
|
||||||||||
| #pragma once | ||||||||||
|
|
||||||||||
| #include <rpp/schedulers.hpp> | ||||||||||
|
|
||||||||||
| static rpp::schedulers::time_point s_current_time{ std::chrono::seconds{0} }; | ||||||||||
| static rpp::schedulers::time_point s_current_time{std::chrono::seconds{0}}; | ||||||||||
|
|
||||||||||
| class test_scheduler final : public rpp::schedulers::details::scheduler_tag | ||||||||||
| { | ||||||||||
| public: | ||||||||||
| class worker_strategy | ||||||||||
| struct state | ||||||||||
| { | ||||||||||
| public: | ||||||||||
| worker_strategy(const rpp::subscription_base& sub, | ||||||||||
| std::shared_ptr<std::vector<rpp::schedulers::time_point>> schedulings) | ||||||||||
| : m_sub{ sub } | ||||||||||
| , m_schedulings{ schedulings } {} | ||||||||||
| state() = default; | ||||||||||
|
|
||||||||||
| void schedule(rpp::schedulers::time_point time_point, | ||||||||||
| rpp::schedulers::constraint::schedulable_fn auto&& fn) | ||||||||||
| { | ||||||||||
| schedulings.push_back(time_point); | ||||||||||
| queue.emplace(time_point, | ||||||||||
| static_cast<size_t>(rpp::schedulers::clock_type::now().time_since_epoch().count()), | ||||||||||
| std::forward<decltype(fn)>(fn)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void defer_at(rpp::schedulers::time_point time_point, rpp::schedulers::constraint::schedulable_fn auto&& fn) const | ||||||||||
| void drain() | ||||||||||
| { | ||||||||||
| while (m_sub.is_subscribed()) | ||||||||||
| while (!queue.empty() && sub.is_subscribed()) | ||||||||||
| { | ||||||||||
| m_schedulings->push_back(time_point); | ||||||||||
| auto time_point = queue.top().get_time_point(); | ||||||||||
| if (time_point > s_current_time) | ||||||||||
| return; | ||||||||||
|
|
||||||||||
| auto fn = queue.top().extract_function(); | ||||||||||
| queue.pop(); | ||||||||||
|
|
||||||||||
| executions.push_back(s_current_time); | ||||||||||
| if (auto duration = fn()) | ||||||||||
| time_point = std::max(now(), time_point + duration.value()); | ||||||||||
| else | ||||||||||
| return; | ||||||||||
| schedule(std::max(s_current_time, time_point + duration.value()), std::move(fn)); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| rpp::subscription_base sub{}; | ||||||||||
| std::vector<rpp::schedulers::time_point> schedulings{}; | ||||||||||
| std::vector<rpp::schedulers::time_point> executions{}; | ||||||||||
| std::priority_queue<rpp::schedulers::details::schedulable<std::function<rpp::schedulers::optional_duration()>>> queue{}; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| class worker_strategy | ||||||||||
| { | ||||||||||
| public: | ||||||||||
| worker_strategy(std::shared_ptr<state> state) | ||||||||||
| : m_state{state} { } | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| void defer_at(rpp::schedulers::time_point time_point, | ||||||||||
| rpp::schedulers::constraint::schedulable_fn auto&& fn) const | ||||||||||
| { | ||||||||||
| if (m_state->sub.is_subscribed()) | ||||||||||
| { | ||||||||||
| m_state->schedule(time_point, std::forward<decltype(fn)>(fn)); | ||||||||||
| m_state->drain(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| static rpp::schedulers::time_point now() { return s_current_time; } | ||||||||||
|
|
||||||||||
| private: | ||||||||||
| rpp::subscription_base m_sub; | ||||||||||
| std::shared_ptr<std::vector<rpp::schedulers::time_point>> m_schedulings; | ||||||||||
| std::shared_ptr<state> m_state; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| test_scheduler() {} | ||||||||||
|
|
||||||||||
| rpp::schedulers::worker<worker_strategy> create_worker(const rpp::subscription_base& sub = {}) const | ||||||||||
| { | ||||||||||
| return rpp::schedulers::worker<worker_strategy>{sub, m_schedulings}; | ||||||||||
| m_state->sub = sub; | ||||||||||
| return rpp::schedulers::worker<worker_strategy>{m_state}; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const auto& get_schedulings() const { return *m_schedulings; } | ||||||||||
| const auto& get_schedulings() const { return m_state->schedulings; } | ||||||||||
| const auto& get_executions() const { return m_state->executions; } | ||||||||||
|
|
||||||||||
| void time_advance(rpp::schedulers::duration dur) const | ||||||||||
| { | ||||||||||
| s_current_time += dur; | ||||||||||
| m_state->drain(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private: | ||||||||||
| std::shared_ptr<std::vector<rpp::schedulers::time_point>> m_schedulings = std::make_shared<std::vector<rpp::schedulers::time_point>>(); | ||||||||||
| }; | ||||||||||
| std::shared_ptr<state> m_state = std::make_shared<state>(); | ||||||||||
| }; | ||||||||||
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.
parameter
stateis passed by value and only copied once; consider moving it to avoid unnecessary copies