Commit 0d5a4a73 authored by Dominik Charousset's avatar Dominik Charousset

Generalize I/O event handling in the test DSL

parent 5ea3e4ac
...@@ -576,28 +576,53 @@ public: ...@@ -576,28 +576,53 @@ public:
// -- DSL functions ---------------------------------------------------------- // -- DSL functions ----------------------------------------------------------
/// Advances the clock by a single tick duration.
size_t advance_time(caf::timespan interval) {
return sched.clock().advance_time(interval);
}
/// Allows the next actor to consume one message from its mailbox. Fails the /// Allows the next actor to consume one message from its mailbox. Fails the
/// test if no message was consumed. /// test if no message was consumed.
void consume_message() { virtual bool consume_message() {
if (!sched.try_run_once()) return sched.try_run_once();
CAF_FAIL("no message to consume");
} }
/// Allows each actors to consume all messages from its mailbox. Fails the /// Allows each actors to consume all messages from its mailbox. Fails the
/// test if no message was consumed. /// test if no message was consumed.
/// @returns The number of consumed messages. /// @returns The number of consumed messages.
size_t consume_messages() { size_t consume_messages() {
auto result = sched.run(); size_t result = 0;
if (result == 0) while (consume_message())
CAF_FAIL("no message to consume"); ++result;
return result;
}
/// Allows an simulated I/O devices to handle an event.
virtual bool handle_io_event() {
return false;
}
/// Allows each simulated I/O device to handle all events.
size_t handle_io_events() {
size_t result = 0;
while (handle_io_event())
++result;
return result; return result;
} }
/// Triggers the next pending timeout.
virtual bool trigger_timeout() {
return sched.trigger_timeout();
}
/// Triggers all pending timeouts.
size_t trigger_timeouts() {
size_t timeouts = 0;
while (trigger_timeout())
++timeouts;
return timeouts;
}
/// Advances the clock by `interval`.
size_t advance_time(caf::timespan interval) {
return sched.clock().advance_time(interval);
}
/// Consume messages and trigger timeouts until no activity remains. /// Consume messages and trigger timeouts until no activity remains.
/// @returns The total number of events, i.e., messages consumed and /// @returns The total number of events, i.e., messages consumed and
/// timeouts triggerd. /// timeouts triggerd.
...@@ -615,20 +640,31 @@ public: ...@@ -615,20 +640,31 @@ public:
// Bookkeeping. // Bookkeeping.
size_t events = 0; size_t events = 0;
// Loop until no activity remains. // Loop until no activity remains.
while (sched.has_job() || sched.has_pending_timeout()) { for (;;) {
while (sched.try_run_once()) { size_t progress = 0;
++events; while (consume_message()) {
++progress;
if (predicate()) { if (predicate()) {
CAF_LOG_DEBUG("stop due to predicate:" << CAF_ARG(events)); CAF_LOG_DEBUG("stop due to predicate:" << CAF_ARG(events));
return events; return events;
} }
} }
if (trigger_timeout()) while (handle_io_event()) {
++events; ++progress;
if (predicate()) {
CAF_LOG_DEBUG("stop due to predicate:" << CAF_ARG(events));
return events;
}
} }
if (trigger_timeout())
++progress;
if (progress == 0) {
CAF_LOG_DEBUG("no activity left:" << CAF_ARG(events)); CAF_LOG_DEBUG("no activity left:" << CAF_ARG(events));
return events; return events;
} }
events += progress;
}
}
/// Call `run()` when the next scheduled actor becomes ready. /// Call `run()` when the next scheduled actor becomes ready.
void run_after_next_ready_event() { void run_after_next_ready_event() {
...@@ -666,19 +702,6 @@ public: ...@@ -666,19 +702,6 @@ public:
return dynamic_cast<T&>(*ptr); return dynamic_cast<T&>(*ptr);
} }
/// Triggers the next pending timeout.
virtual bool trigger_timeout() {
return sched.trigger_timeout();
}
/// Triggers all pending timeouts.
size_t trigger_timeouts() {
size_t timeouts = 0;
while (trigger_timeout())
++timeouts;
return timeouts;
}
// -- member variables ------------------------------------------------------- // -- member variables -------------------------------------------------------
/// The user-generated system config. /// The user-generated system config.
......
...@@ -38,7 +38,9 @@ public: ...@@ -38,7 +38,9 @@ public:
// -- interface functions ---------------------------------------------------- // -- interface functions ----------------------------------------------------
virtual bool advance() = 0; virtual bool consume_message() = 0;
virtual bool handle_io_event() = 0;
virtual bool trigger_timeout() = 0; virtual bool trigger_timeout() = 0;
}; };
...@@ -110,9 +112,12 @@ public: ...@@ -110,9 +112,12 @@ public:
// -- overriding member functions -------------------------------------------- // -- overriding member functions --------------------------------------------
bool advance() override { bool consume_message() override {
return mpx.try_exec_runnable() || mpx.read_data() return this->sched.try_run_once() || mpx.try_exec_runnable();
|| mpx.try_accept_connection() || this->sched.try_run_once(); }
bool handle_io_event() override {
return mpx.read_data() || mpx.try_accept_connection();
} }
bool trigger_timeout() override { bool trigger_timeout() override {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment