diff --git a/include/asyncpp/timing/timeout.h b/include/asyncpp/timing/timeout.h index 18f9a94..c4260d6 100644 --- a/include/asyncpp/timing/timeout.h +++ b/include/asyncpp/timing/timeout.h @@ -14,7 +14,11 @@ namespace timing { struct timeout_exception : public cppcore::exception { - using cppcore::exception::exception; + public: + /** + * @brief Constructor. + */ + inline timeout_exception(); }; template diff --git a/include/asyncpp/timing/timeout.inl b/include/asyncpp/timing/timeout.inl index 62ad510..9f99a47 100644 --- a/include/asyncpp/timing/timeout.inl +++ b/include/asyncpp/timing/timeout.inl @@ -7,6 +7,12 @@ namespace asyncpp { namespace timing { + /* timeout_exception */ + + timeout_exception::timeout_exception() + : cppcore::exception::exception("timeout") + { } + /* timer */ template @@ -71,12 +77,17 @@ namespace asyncpp { auto r = self->_inner.poll(); - if ( r.is_not_ready() - && self->_delay.poll()) + if (r.is_not_ready()) { - auto new_deadline = self->_delay->deadline() + self->_timeout; - self->_delay->reset(new_deadline); - throw timing::timeout_exception(); + if (self->_delay.poll()) + { + self->_delay->reset(self->_timeout); + throw timing::timeout_exception(); + } + } + else + { + self->_delay->reset(self->_timeout); } return r; diff --git a/test/asyncpp/timing/timeout_tests.cpp b/test/asyncpp/timing/timeout_tests.cpp index aa7342b..1bae92c 100644 --- a/test/asyncpp/timing/timeout_tests.cpp +++ b/test/asyncpp/timing/timeout_tests.cpp @@ -144,10 +144,27 @@ TEST(timeout_tests, poll_stream_no_timeout) auto r1 = f.poll(); ASSERT_FALSE(r1); - ++t.i; + t.i = 1; + + EXPECT_CALL(m, now()) + .WillOnce(Return(time_point(std::chrono::seconds(4)))); auto r2 = f.poll(); ASSERT_TRUE(r2); + + EXPECT_CALL(m, now()) + .WillOnce(Return(time_point(std::chrono::seconds(8)))); + + auto r3 = f.poll(); + ASSERT_TRUE(r3); + + t.i = 0; + + EXPECT_CALL(m, now()) + .WillOnce(Return(time_point(std::chrono::seconds(12)))); + + auto r4 = f.poll(); + ASSERT_FALSE(r4); } TEST(timeout_tests, poll_stream_timeout) @@ -178,11 +195,15 @@ TEST(timeout_tests, poll_stream_timeout) ASSERT_FALSE(r1); EXPECT_CALL(m, now()) + .WillOnce(Return(time_point(std::chrono::seconds(5)))) .WillOnce(Return(time_point(std::chrono::seconds(5)))); EXPECT_THROW(f.poll(), timing::timeout_exception); - ++t.i; + t.i = 1; + + EXPECT_CALL(m, now()) + .WillOnce(Return(time_point(std::chrono::seconds(10)))); auto r2 = f.poll(); ASSERT_TRUE(r2); @@ -190,13 +211,14 @@ TEST(timeout_tests, poll_stream_timeout) t.i = 0; EXPECT_CALL(m, now()) - .WillOnce(Return(time_point(std::chrono::seconds(9)))); + .WillOnce(Return(time_point(std::chrono::seconds(14)))); auto r3 = f.poll(); ASSERT_FALSE(r3); EXPECT_CALL(m, now()) - .WillOnce(Return(time_point(std::chrono::seconds(10)))); + .WillOnce(Return(time_point(std::chrono::seconds(15)))) + .WillOnce(Return(time_point(std::chrono::seconds(15)))); EXPECT_THROW(f.poll(), timing::timeout_exception); }