|
|
|
@@ -0,0 +1,68 @@ |
|
|
|
#include <gtest/gtest.h> |
|
|
|
|
|
|
|
#include <asyncpp.h> |
|
|
|
|
|
|
|
using namespace ::testing; |
|
|
|
using namespace ::asyncpp; |
|
|
|
|
|
|
|
struct delay |
|
|
|
{ |
|
|
|
int const delay { 5 }; |
|
|
|
int const threshold { 2 }; |
|
|
|
int count { 0 }; |
|
|
|
}; |
|
|
|
|
|
|
|
namespace asyncpp |
|
|
|
{ |
|
|
|
|
|
|
|
template<> |
|
|
|
struct stream_trait<delay, void> |
|
|
|
: public future_base<future<delay, void>> |
|
|
|
{ |
|
|
|
using value_type = int; |
|
|
|
|
|
|
|
template<typename T_future> |
|
|
|
static inline auto poll(T_future& self) |
|
|
|
{ |
|
|
|
using result_type = stream_result<value_type>; |
|
|
|
|
|
|
|
if (self.ref.count >= self.ref.delay) |
|
|
|
return result_type::done(); |
|
|
|
|
|
|
|
++self.ref.count; |
|
|
|
|
|
|
|
return self.ref.count <= self.ref.threshold |
|
|
|
? result_type::not_ready() |
|
|
|
: result_type::ready(self.ref.count); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
TEST(stream_tests, poll) |
|
|
|
{ |
|
|
|
delay d { 5, 2, 0 }; |
|
|
|
auto s = as_stream(d); |
|
|
|
|
|
|
|
auto r0 = s.poll(); |
|
|
|
ASSERT_FALSE(r0); |
|
|
|
|
|
|
|
auto r1 = s.poll(); |
|
|
|
ASSERT_FALSE(r1); |
|
|
|
|
|
|
|
auto r2 = s.poll(); |
|
|
|
ASSERT_TRUE (r2); |
|
|
|
ASSERT_EQ (3, *r2); |
|
|
|
|
|
|
|
auto r3 = s.poll(); |
|
|
|
ASSERT_TRUE (r3); |
|
|
|
ASSERT_EQ (4, *r3); |
|
|
|
|
|
|
|
auto r4 = s.poll(); |
|
|
|
ASSERT_TRUE (r4); |
|
|
|
ASSERT_EQ (5, *r4); |
|
|
|
|
|
|
|
auto r = s.poll(); |
|
|
|
ASSERT_FALSE(r); |
|
|
|
ASSERT_TRUE (r.is_done()); |
|
|
|
} |