-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathaction_test.cpp
140 lines (116 loc) · 3.65 KB
/
action_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
/*
* .============.
* // 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 <tbox/event/loop.h>
#include <tbox/base/scope_exit.hpp>
#include "action.h"
namespace tbox {
namespace flow {
TEST(Action, StartFinish) {
auto loop = event::Loop::New();
SetScopeExitAction([loop] { delete loop; });
class TestAction : public Action {
public:
explicit TestAction(event::Loop &loop) : Action(loop, "Test") { }
virtual bool isReady() const override { return true; }
virtual void onStart() override { finish(true, 100); }
};
TestAction action(*loop);
bool is_callback = false;
action.setFinishCallback(
[&, loop] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_TRUE(is_succ);
EXPECT_EQ(r.code, 100);
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].type, "Test");
is_callback = true;
}
);
action.start();
loop->exitLoop(std::chrono::milliseconds(10));
loop->runLoop();
EXPECT_TRUE(is_callback);
}
TEST(Action, StartBlock) {
auto loop = event::Loop::New();
SetScopeExitAction([loop] { delete loop; });
class TestAction : public Action {
public:
explicit TestAction(event::Loop &loop) : Action(loop, "Test") { }
virtual bool isReady() const override { return true; }
virtual void onStart() override {
Action::onStart();
block(Action::Reason(100, "block_on_start"));
}
};
bool is_block = false;
TestAction action(*loop);
action.setBlockCallback(
[&](const Action::Reason &r, const Action::Trace &t) {
EXPECT_EQ(r.code, 100);
EXPECT_EQ(r.message, "block_on_start");
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].type, "Test");
is_block = true;
}
);
action.start();
loop->exitLoop(std::chrono::milliseconds(10));
loop->runLoop();
EXPECT_TRUE(is_block);
EXPECT_EQ(action.state(), Action::State::kPause);
action.stop();
EXPECT_EQ(action.state(), Action::State::kStoped);
}
TEST(Action, Timeout) {
auto loop = event::Loop::New();
SetScopeExitAction([loop] { delete loop; });
class TestAction : public Action {
public:
explicit TestAction(event::Loop &loop) : Action(loop, "Test") { }
virtual bool isReady() const override { return true; }
};
TestAction action(*loop);
action.setTimeout(std::chrono::milliseconds(50));
bool is_callback = false;
std::chrono::steady_clock::time_point ts_start;
std::chrono::steady_clock::time_point ts_timeout;
action.setFinishCallback(
[&, loop] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_FALSE(is_succ);
EXPECT_EQ(r.code, ACTION_REASON_ACTION_TIMEOUT);
EXPECT_EQ(r.message, "ActionTimeout");
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].type, "Test");
is_callback = true;
ts_timeout = std::chrono::steady_clock::now();
loop->exitLoop();
}
);
action.start();
ts_start = std::chrono::steady_clock::now();
loop->runLoop();
EXPECT_TRUE(is_callback);
auto cout_50 = std::chrono::duration_cast<std::chrono::milliseconds>(ts_timeout - ts_start).count();
EXPECT_LE(cout_50, 51);
EXPECT_GE(cout_50, 49);
}
}
}