| 
 | 1 | +#include <catch2/catch_all.hpp>  | 
 | 2 | +#include <zmq.hpp>  | 
 | 3 | + | 
 | 4 | +#include <type_traits>  | 
 | 5 | +#include <thread>  | 
 | 6 | +#include <chrono>  | 
 | 7 | + | 
 | 8 | +#if defined(ZMQ_CPP11) && defined(ZMQ_HAVE_TIMERS)  | 
 | 9 | + | 
 | 10 | +static_assert(std::is_default_constructible<zmq::timers>::value);  | 
 | 11 | +static_assert(!std::is_copy_constructible<zmq::timers>::value);  | 
 | 12 | +static_assert(!std::is_copy_assignable<zmq::timers>::value);  | 
 | 13 | + | 
 | 14 | +TEST_CASE("timers constructor", "[timers]")  | 
 | 15 | +{  | 
 | 16 | +    zmq::timers timers;  | 
 | 17 | +    CHECK(!timers.timeout().has_value());  | 
 | 18 | +}  | 
 | 19 | + | 
 | 20 | +TEST_CASE("timers add/execute", "[timers]")  | 
 | 21 | +{  | 
 | 22 | +    using namespace std::chrono_literals;  | 
 | 23 | +    zmq::timers timers;  | 
 | 24 | +    bool handler_ran = false;  | 
 | 25 | +    timers.add(4ms, [](auto, void *arg) { *(bool *) arg = true; }, &handler_ran);  | 
 | 26 | +    CHECK(timers.timeout().has_value());  | 
 | 27 | +    CHECK(!handler_ran);  | 
 | 28 | +    std::this_thread::sleep_for(10ms);  | 
 | 29 | +    timers.execute();  | 
 | 30 | +    CHECK(handler_ran);  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +TEST_CASE("timers add/cancel", "[timers]")  | 
 | 34 | +{  | 
 | 35 | +    using namespace std::chrono_literals;  | 
 | 36 | +    zmq::timers timers;  | 
 | 37 | +    bool handler_ran = false;  | 
 | 38 | +    auto id =  | 
 | 39 | +      timers.add(4ms, [](auto, void *arg) { *(bool *) arg = true; }, &handler_ran);  | 
 | 40 | +    CHECK(timers.timeout().has_value());  | 
 | 41 | +    CHECK(!handler_ran);  | 
 | 42 | +    timers.cancel(id);  | 
 | 43 | +    CHECK(!timers.timeout().has_value());  | 
 | 44 | +    CHECK(!handler_ran);  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +TEST_CASE("timers set_interval", "[timers]")  | 
 | 48 | +{  | 
 | 49 | +    using namespace std::chrono_literals;  | 
 | 50 | +    zmq::timers timers;  | 
 | 51 | +    bool handler_ran = false;  | 
 | 52 | +    // Interval of 4 hours should never run like this  | 
 | 53 | +    auto id =  | 
 | 54 | +      timers.add(4h, [](auto, void *arg) { *(bool *) arg = true; }, &handler_ran);  | 
 | 55 | +    CHECK(timers.timeout().has_value());  | 
 | 56 | +    CHECK(!handler_ran);  | 
 | 57 | +    // Change the interval to 4ms and wait for it to timeout  | 
 | 58 | +    timers.set_interval(id, 4ms);  | 
 | 59 | +    std::this_thread::sleep_for(10ms);  | 
 | 60 | +    timers.execute();  | 
 | 61 | +    CHECK(handler_ran);  | 
 | 62 | +}  | 
 | 63 | + | 
 | 64 | +TEST_CASE("timers reset", "[timers]")  | 
 | 65 | +{  | 
 | 66 | +    using namespace std::chrono_literals;  | 
 | 67 | +    zmq::timers timers;  | 
 | 68 | +    bool handler_ran = false;  | 
 | 69 | +    auto id =  | 
 | 70 | +      timers.add(4ms, [](auto, void *arg) { *(bool *) arg = true; }, &handler_ran);  | 
 | 71 | +    CHECK(timers.timeout().has_value());  | 
 | 72 | +    std::this_thread::sleep_for(10ms);  | 
 | 73 | +    // Available to be executed but we reset it  | 
 | 74 | +    timers.reset(id);  | 
 | 75 | +    CHECK(timers.timeout().has_value());  | 
 | 76 | +    CHECK(!handler_ran);  | 
 | 77 | + | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +#endif // defined(ZMQ_CPP11) && defined(ZMQ_HAVE_TIMERS)  | 
0 commit comments