|
|
@@ -0,0 +1,78 @@ |
|
|
|
#include <gtest/gtest.h> |
|
|
|
|
|
|
|
#include "../../helper/now_mock.h" |
|
|
|
|
|
|
|
#include <asyncpp.h> |
|
|
|
#include <asyncpp/timer.h> |
|
|
|
|
|
|
|
using namespace ::testing; |
|
|
|
using namespace ::asyncpp; |
|
|
|
|
|
|
|
TEST(timer_tests, current) |
|
|
|
{ |
|
|
|
{ |
|
|
|
timer::timer t; |
|
|
|
|
|
|
|
EXPECT_EQ(nullptr, timer::timer::current()); |
|
|
|
|
|
|
|
t.make_current(); |
|
|
|
|
|
|
|
EXPECT_EQ(&t, timer::timer::current()); |
|
|
|
|
|
|
|
t.clear_current(); |
|
|
|
|
|
|
|
EXPECT_EQ(nullptr, timer::timer::current()); |
|
|
|
|
|
|
|
t.make_current(); |
|
|
|
|
|
|
|
EXPECT_EQ(&t, timer::timer::current()); |
|
|
|
} |
|
|
|
|
|
|
|
EXPECT_EQ(nullptr, timer::timer::current()); |
|
|
|
} |
|
|
|
|
|
|
|
TEST(timer_tests, resource_registration) |
|
|
|
{ |
|
|
|
StrictMock<now_mock> m; |
|
|
|
EXPECT_CALL(m, now) |
|
|
|
.WillRepeatedly(Return(timer::time_point(std::chrono::seconds(0)))); |
|
|
|
|
|
|
|
using delay_future_type = decltype(as_future(timer::delay(std::chrono::seconds(10)))); |
|
|
|
|
|
|
|
timer::timer t; |
|
|
|
t.make_current(); |
|
|
|
|
|
|
|
auto f1 = std::make_unique<delay_future_type>(timer::delay(std::chrono::seconds(10))); |
|
|
|
auto f2 = std::make_unique<delay_future_type>(timer::delay(std::chrono::seconds(20))); |
|
|
|
auto f3 = std::make_unique<delay_future_type>(timer::delay(std::chrono::seconds(30))); |
|
|
|
|
|
|
|
EXPECT_EQ(0, t.resource_count()); |
|
|
|
|
|
|
|
f1->poll(); // simple add |
|
|
|
|
|
|
|
EXPECT_EQ(1, t.resource_count()); |
|
|
|
|
|
|
|
f2->poll(); // simple add |
|
|
|
|
|
|
|
EXPECT_EQ(2, t.resource_count()); |
|
|
|
|
|
|
|
f1.reset(); // remove first |
|
|
|
|
|
|
|
EXPECT_EQ(1, t.resource_count()); |
|
|
|
|
|
|
|
f3->poll(); // simple add |
|
|
|
|
|
|
|
EXPECT_EQ(2, t.resource_count()); |
|
|
|
|
|
|
|
f3->poll(); // calling twice should not add |
|
|
|
|
|
|
|
EXPECT_EQ(2, t.resource_count()); |
|
|
|
|
|
|
|
f3.reset(); // remove last |
|
|
|
|
|
|
|
EXPECT_EQ(1, t.resource_count()); |
|
|
|
|
|
|
|
f2.reset(); // remove remaining |
|
|
|
|
|
|
|
EXPECT_EQ(0, t.resource_count()); |
|
|
|
} |