-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtimer_event_source_test.cc
More file actions
75 lines (61 loc) · 1.69 KB
/
timer_event_source_test.cc
File metadata and controls
75 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <atomic>
#include <iostream>
#include <boost/test/unit_test.hpp>
#include "jml/arch/timers.h"
#include "jml/utils/testing/watchdog.h"
#include "soa/types/date.h"
#include "soa/service/message_loop.h"
#include "soa/service/timer_event_source.h"
using namespace std;
using namespace Datacratic;
BOOST_AUTO_TEST_CASE( test_addTimer )
{
ML::Watchdog wd(10);
std::atomic<int> ticks(0);
MessageLoop loop(1, 0, -1);
loop.start();
auto timer = make_shared<TimerEventSource>();
loop.addSource("timer", timer);
timer->waitConnectionState(AsyncEventSource::CONNECTED);
auto onTick = [&] (uint64_t) {
Date now = Date::now();
ticks++;
return ticks < 3;
};
timer->addTimer(0.2, onTick);
while (true) {
if (ticks == 3) {
Date now = Date::now();
break;
}
ML::sleep(1);
}
loop.shutdown();
}
BOOST_AUTO_TEST_CASE( test_cancelTimer )
{
ML::Watchdog wd(10);
std::atomic<int> ticks(0);
MessageLoop loop(1, 0, -1);
loop.start();
auto timer = make_shared<TimerEventSource>();
loop.addSource("timer", timer);
timer->waitConnectionState(AsyncEventSource::CONNECTED);
auto onTick = [&] (uint64_t) {
Date now = Date::now();
ticks++;
return true;
};
auto timerId = timer->addTimer(0.2, onTick);
BOOST_CHECK(timerId > 0);
ML::sleep(1);
BOOST_CHECK_EQUAL(timer->cancelTimer(timerId), true);
BOOST_CHECK_NE(ticks, 0);
int oldTicks = ticks;
ML::sleep(1);
BOOST_CHECK_EQUAL(ticks, oldTicks);
BOOST_CHECK_EQUAL(timer->cancelTimer(timerId), false);
loop.shutdown();
}