-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDbThreadMonitorTest.cpp
More file actions
229 lines (189 loc) · 6.5 KB
/
Copy pathDbThreadMonitorTest.cpp
File metadata and controls
229 lines (189 loc) · 6.5 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <gtest/gtest.h>
#include "DbThreadMonitor.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/null_sink.h"
#include <atomic>
#include <chrono>
#include <thread>
#include <vector>
using namespace std::chrono_literals;
// DbThreadMonitor 는 LOG("net") spdlog 로거를 사용한다. 테스트 하네스에는 해당
// 로거가 등록되어 있지 않으므로(InitLog 미호출), 출력이 없는 null 로거를 한 번 등록한다.
namespace
{
void EnsureNetLogger()
{
if (!spdlog::get("net"))
{
auto logger = std::make_shared<spdlog::logger>(
"net", std::make_shared<spdlog::sinks::null_sink_mt>());
logger->set_level(spdlog::level::debug);
spdlog::register_logger(logger);
}
}
}
class DbThreadMonitorTest : public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
EnsureNetLogger();
}
void SetUp() override
{
// 싱글턴 상태를 테스트마다 초기화하여 결정적으로 만든다.
DbThreadMonitor::Instance().Reset();
mon_ = &DbThreadMonitor::Instance();
}
DbThreadMonitor* mon_ = nullptr;
};
// BeginEnqueue 는 작업을 "큐 대기" 상태로 등록한다(아직 실행 시작 전).
TEST_F(DbThreadMonitorTest, BeginEnqueueTracksQueuedTask)
{
const uint64_t token = mon_->BeginEnqueue("PlayerLoad", 1001);
EXPECT_NE(token, 0u);
EXPECT_EQ(mon_->InflightCount(), 1u);
EXPECT_EQ(mon_->QueuedCount(), 1u);
EXPECT_EQ(mon_->RunningCount(), 0u);
EXPECT_EQ(mon_->CompletedCount(), 0u);
}
// OnStart 호출 시 "실행 중" 으로 전환된다.
TEST_F(DbThreadMonitorTest, OnStartMarksRunning)
{
const uint64_t token = mon_->BeginEnqueue("PlayerSave", 2002);
mon_->OnStart(token);
EXPECT_EQ(mon_->InflightCount(), 1u);
EXPECT_EQ(mon_->RunningCount(), 1u);
EXPECT_EQ(mon_->QueuedCount(), 0u);
}
// OnFinish 는 작업을 제거하고 완료 수 / 스레드 통계를 갱신한다.
TEST_F(DbThreadMonitorTest, OnFinishRemovesAndAccumulates)
{
const uint64_t token = mon_->BeginEnqueue("PlayerLoad", 3003);
mon_->OnStart(token);
mon_->OnFinish(token);
EXPECT_EQ(mon_->InflightCount(), 0u);
EXPECT_EQ(mon_->CompletedCount(), 1u);
auto stats = mon_->SnapshotThreadStats();
ASSERT_EQ(stats.size(), 1u);
EXPECT_EQ(stats.begin()->second.taskCount, 1u);
}
// DbTaskScope(RAII) 는 생성 시 시작, 소멸 시 종료를 자동 기록한다.
TEST_F(DbThreadMonitorTest, DbTaskScopeRecordsStartAndFinish)
{
const uint64_t token = mon_->BeginEnqueue("PlayerSave", 4004);
{
DbTaskScope scope(token);
EXPECT_EQ(mon_->RunningCount(), 1u);
}
EXPECT_EQ(mon_->InflightCount(), 0u);
EXPECT_EQ(mon_->CompletedCount(), 1u);
}
// 실행 시간이 슬로우 임계값을 넘으면 SlowCount 가 증가한다.
TEST_F(DbThreadMonitorTest, SlowTaskIncrementsSlowCount)
{
mon_->SetSlowThreshold(1ms);
const uint64_t token = mon_->BeginEnqueue("SlowQuery", 5005);
mon_->OnStart(token);
std::this_thread::sleep_for(10ms);
mon_->OnFinish(token);
EXPECT_EQ(mon_->CompletedCount(), 1u);
EXPECT_EQ(mon_->SlowCount(), 1u);
}
// 빠른 작업은 슬로우로 집계되지 않는다.
TEST_F(DbThreadMonitorTest, FastTaskIsNotSlow)
{
mon_->SetSlowThreshold(10s); // 사실상 도달 불가
const uint64_t token = mon_->BeginEnqueue("FastQuery", 6006);
mon_->OnStart(token);
mon_->OnFinish(token);
EXPECT_EQ(mon_->CompletedCount(), 1u);
EXPECT_EQ(mon_->SlowCount(), 0u);
}
// 실행이 stuck 임계값을 넘으면 CollectStuckTasks 가 해당 작업을 보고한다.
TEST_F(DbThreadMonitorTest, CollectStuckTasksDetectsLongRunning)
{
mon_->SetStuckThreshold(1ms);
const uint64_t token = mon_->BeginEnqueue("DeadlockSuspect", 7007);
mon_->OnStart(token);
std::this_thread::sleep_for(10ms);
auto stuck = mon_->CollectStuckTasks();
ASSERT_EQ(stuck.size(), 1u);
EXPECT_EQ(stuck[0].id, token);
EXPECT_EQ(stuck[0].name, "DeadlockSuspect");
EXPECT_EQ(stuck[0].playerId, 7007);
EXPECT_GT(stuck[0].runningMs, 0.0);
mon_->OnFinish(token);
EXPECT_TRUE(mon_->CollectStuckTasks().empty());
}
// 큐에서 대기만 하는(시작 전) 작업은 stuck 으로 보고되지 않는다.
TEST_F(DbThreadMonitorTest, QueuedTaskIsNotReportedStuck)
{
mon_->SetStuckThreshold(1ms);
mon_->BeginEnqueue("QueuedOnly", 8008); // OnStart 호출하지 않음
std::this_thread::sleep_for(10ms);
EXPECT_TRUE(mon_->CollectStuckTasks().empty());
}
// 알 수 없는 토큰에 대한 OnStart/OnFinish 는 무시되며 상태를 바꾸지 않는다.
TEST_F(DbThreadMonitorTest, UnknownTokenIsIgnored)
{
mon_->OnStart(999999);
mon_->OnFinish(999999);
EXPECT_EQ(mon_->InflightCount(), 0u);
EXPECT_EQ(mon_->CompletedCount(), 0u);
}
// 같은 스레드에서 여러 작업을 처리하면 스레드 통계가 누적된다.
TEST_F(DbThreadMonitorTest, MultipleTasksAccumulateOnSameThread)
{
const int N = 5;
for (int i = 0; i < N; ++i)
{
const uint64_t token = mon_->BeginEnqueue("PlayerLoad", 100 + i);
DbTaskScope scope(token);
}
EXPECT_EQ(mon_->CompletedCount(), static_cast<uint64_t>(N));
EXPECT_EQ(mon_->InflightCount(), 0u);
auto stats = mon_->SnapshotThreadStats();
ASSERT_EQ(stats.size(), 1u); // 모두 같은(테스트) 스레드
EXPECT_EQ(stats.begin()->second.taskCount, static_cast<uint64_t>(N));
}
// Reset 은 모든 누적 상태와 카운터를 초기화한다.
TEST_F(DbThreadMonitorTest, ResetClearsState)
{
const uint64_t token = mon_->BeginEnqueue("PlayerLoad", 1);
mon_->OnStart(token);
mon_->OnFinish(token);
ASSERT_EQ(mon_->CompletedCount(), 1u);
mon_->Reset();
EXPECT_EQ(mon_->InflightCount(), 0u);
EXPECT_EQ(mon_->CompletedCount(), 0u);
EXPECT_EQ(mon_->SlowCount(), 0u);
EXPECT_TRUE(mon_->SnapshotThreadStats().empty());
}
// 여러 스레드에서 동시에 enqueue/실행해도 카운트가 정확하고 누수가 없어야 한다(스레드 안전성).
TEST_F(DbThreadMonitorTest, ConcurrentTasksAreThreadSafe)
{
const int kThreads = 8;
const int kPerThread = 200;
std::vector<std::thread> workers;
workers.reserve(kThreads);
for (int t = 0; t < kThreads; ++t)
{
workers.emplace_back([this, t]() {
for (int i = 0; i < kPerThread; ++i)
{
const uint64_t token = mon_->BeginEnqueue("ConcurrentLoad", t * 1000 + i);
DbTaskScope scope(token);
}
});
}
for (auto& w : workers)
w.join();
EXPECT_EQ(mon_->CompletedCount(), static_cast<uint64_t>(kThreads * kPerThread));
EXPECT_EQ(mon_->InflightCount(), 0u);
// 스레드별 처리 건수의 합은 전체 완료 수와 같아야 한다.
uint64_t totalFromStats = 0;
for (const auto& kv : mon_->SnapshotThreadStats())
totalFromStats += kv.second.taskCount;
EXPECT_EQ(totalFromStats, static_cast<uint64_t>(kThreads * kPerThread));
}