You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
1.3 KiB

  1. #include <gtest/gtest.h>
  2. #include <asyncpp.h>
  3. using namespace ::testing;
  4. using namespace ::asyncpp;
  5. struct delay
  6. {
  7. int const delay { 5 };
  8. int const threshold { 2 };
  9. int count { 0 };
  10. };
  11. namespace asyncpp
  12. {
  13. template<>
  14. struct stream_trait<delay, void>
  15. : public future_base<future<delay, void>>
  16. {
  17. using value_type = int;
  18. template<typename T_future>
  19. static inline auto poll(T_future& self)
  20. {
  21. using result_type = stream_result<value_type>;
  22. if (self.ref.count >= self.ref.delay)
  23. return result_type::done();
  24. ++self.ref.count;
  25. return self.ref.count <= self.ref.threshold
  26. ? result_type::not_ready()
  27. : result_type::ready(self.ref.count);
  28. }
  29. };
  30. }
  31. TEST(stream_tests, poll)
  32. {
  33. delay d { 5, 2, 0 };
  34. auto s = as_stream(d);
  35. auto r0 = s.poll();
  36. ASSERT_FALSE(r0);
  37. auto r1 = s.poll();
  38. ASSERT_FALSE(r1);
  39. auto r2 = s.poll();
  40. ASSERT_TRUE (r2);
  41. ASSERT_EQ (3, *r2);
  42. auto r3 = s.poll();
  43. ASSERT_TRUE (r3);
  44. ASSERT_EQ (4, *r3);
  45. auto r4 = s.poll();
  46. ASSERT_TRUE (r4);
  47. ASSERT_EQ (5, *r4);
  48. auto r = s.poll();
  49. ASSERT_FALSE(r);
  50. ASSERT_TRUE (r.is_done());
  51. }