Bläddra i källkod

* Fixed bug: Timeout for streams is not reseted if the stream emits a value

master
bergmann 6 år sedan
förälder
incheckning
e3313d4cbd
3 ändrade filer med 47 tillägg och 10 borttagningar
  1. +5
    -1
      include/asyncpp/timing/timeout.h
  2. +16
    -5
      include/asyncpp/timing/timeout.inl
  3. +26
    -4
      test/asyncpp/timing/timeout_tests.cpp

+ 5
- 1
include/asyncpp/timing/timeout.h Visa fil

@@ -14,7 +14,11 @@ namespace timing {
struct timeout_exception struct timeout_exception
: public cppcore::exception : public cppcore::exception
{ {
using cppcore::exception::exception;
public:
/**
* @brief Constructor.
*/
inline timeout_exception();
}; };


template<typename T_inner> template<typename T_inner>


+ 16
- 5
include/asyncpp/timing/timeout.inl Visa fil

@@ -7,6 +7,12 @@
namespace asyncpp { namespace asyncpp {
namespace timing { namespace timing {


/* timeout_exception */

timeout_exception::timeout_exception()
: cppcore::exception::exception("timeout")
{ }

/* timer */ /* timer */


template<typename T_inner> template<typename T_inner>
@@ -71,12 +77,17 @@ namespace asyncpp
{ {
auto r = self->_inner.poll(); 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; return r;


+ 26
- 4
test/asyncpp/timing/timeout_tests.cpp Visa fil

@@ -144,10 +144,27 @@ TEST(timeout_tests, poll_stream_no_timeout)
auto r1 = f.poll(); auto r1 = f.poll();
ASSERT_FALSE(r1); ASSERT_FALSE(r1);


++t.i;
t.i = 1;

EXPECT_CALL(m, now())
.WillOnce(Return(time_point(std::chrono::seconds(4))));


auto r2 = f.poll(); auto r2 = f.poll();
ASSERT_TRUE(r2); 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) TEST(timeout_tests, poll_stream_timeout)
@@ -178,11 +195,15 @@ TEST(timeout_tests, poll_stream_timeout)
ASSERT_FALSE(r1); ASSERT_FALSE(r1);


EXPECT_CALL(m, now()) EXPECT_CALL(m, now())
.WillOnce(Return(time_point(std::chrono::seconds(5))))
.WillOnce(Return(time_point(std::chrono::seconds(5)))); .WillOnce(Return(time_point(std::chrono::seconds(5))));


EXPECT_THROW(f.poll(), timing::timeout_exception); 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(); auto r2 = f.poll();
ASSERT_TRUE(r2); ASSERT_TRUE(r2);
@@ -190,13 +211,14 @@ TEST(timeout_tests, poll_stream_timeout)
t.i = 0; t.i = 0;


EXPECT_CALL(m, now()) EXPECT_CALL(m, now())
.WillOnce(Return(time_point(std::chrono::seconds(9))));
.WillOnce(Return(time_point(std::chrono::seconds(14))));


auto r3 = f.poll(); auto r3 = f.poll();
ASSERT_FALSE(r3); ASSERT_FALSE(r3);


EXPECT_CALL(m, now()) 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); EXPECT_THROW(f.poll(), timing::timeout_exception);
} }

Laddar…
Avbryt
Spara