-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtimer_event_test.cpp
142 lines (117 loc) · 3.79 KB
/
timer_event_test.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include <gtest/gtest.h>
#include <unistd.h>
#include <fcntl.h>
#include "loop.h"
#include "timer_event.h"
namespace tbox {
namespace event {
using namespace std;
const int kAcceptableError = 10;
TEST(TimerEvent, Oneshot)
{
auto engines = Loop::Engines();
for (auto e : engines) {
cout << "engine: " << e << endl;
auto sp_loop = Loop::New(e);
auto timer_event = sp_loop->newTimerEvent();
EXPECT_TRUE(timer_event->initialize(chrono::milliseconds(10), Event::Mode::kOneshot));
EXPECT_TRUE(timer_event->enable());
int run_time = 0;
timer_event->setCallback([&]() { ++run_time; });
sp_loop->exitLoop(std::chrono::milliseconds(100));
sp_loop->runLoop();
EXPECT_EQ(run_time, 1);
EXPECT_FALSE(timer_event->isEnabled());
delete timer_event;
delete sp_loop;
}
}
TEST(TimerEvent, Persist)
{
auto engines = Loop::Engines();
for (auto e : engines) {
cout << "engine: " << e << endl;
auto sp_loop = Loop::New(e);
auto timer_event = sp_loop->newTimerEvent();
EXPECT_TRUE(timer_event->initialize(chrono::milliseconds(10), Event::Mode::kPersist));
EXPECT_TRUE(timer_event->enable());
int run_time = 0;
timer_event->setCallback([&]() { ++run_time; });
sp_loop->exitLoop(std::chrono::milliseconds(100));
sp_loop->runLoop();
EXPECT_EQ(run_time, 10);
EXPECT_TRUE(timer_event->isEnabled());
delete timer_event;
delete sp_loop;
}
}
TEST(TimerEvent, DisableSelfInCallback)
{
auto engines = Loop::Engines();
for (auto e : engines) {
cout << "engine: " << e << endl;
auto sp_loop = Loop::New(e);
auto timer_event = sp_loop->newTimerEvent();
EXPECT_TRUE(timer_event->initialize(chrono::milliseconds(10), Event::Mode::kPersist));
EXPECT_TRUE(timer_event->enable());
int run_time = 0;
timer_event->setCallback(
[&] () {
timer_event->disable();
++run_time;
}
);
sp_loop->exitLoop(std::chrono::milliseconds(100));
sp_loop->runLoop();
EXPECT_EQ(run_time, 1); //! 应该只执行一次
delete timer_event;
delete sp_loop;
}
}
TEST(TimerEvent, Precision)
{
auto engines = Loop::Engines();
for (auto e : engines) {
cout << "engine: " << e << endl;
auto sp_loop = Loop::New(e);
auto timer_event = sp_loop->newTimerEvent();
EXPECT_TRUE(timer_event->initialize(chrono::milliseconds(100), Event::Mode::kPersist));
EXPECT_TRUE(timer_event->enable());
int count = 0;
auto start_time = chrono::steady_clock::now();
timer_event->setCallback(
[&] {
++count;
auto d = chrono::steady_clock::now() - start_time;
EXPECT_GT(d, chrono::milliseconds(count * 100 - kAcceptableError));
EXPECT_LT(d, chrono::milliseconds(count * 100 + kAcceptableError));
if (count == 20)
sp_loop->exitLoop();
}
);
sp_loop->runLoop();
delete timer_event;
delete sp_loop;
}
}
}
}