-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathsemaphore_test.cpp
120 lines (100 loc) · 2.71 KB
/
semaphore_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
/*
* .============.
* // 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 "semaphore.hpp"
#include <tbox/base/scope_exit.hpp>
#include <tbox/event/timer_event.h>
using namespace std;
using namespace tbox;
using namespace tbox::event;
using namespace tbox::coroutine;
/**
* 生产者 -- 消费者测试
*/
TEST(Semaphore, TwoRoutines_ProduceAndConsumer)
{
Loop *sp_loop = event::Loop::New();
SetScopeExitAction([sp_loop]{ delete sp_loop;});
Scheduler sch(sp_loop);
Semaphore sem(sch, 0);
int times = 200;
int count = 0;
//! 生产者
auto routine1_entry = [&] (Scheduler &sch) {
for (int i = 0; i < times; ++i) {
sem.release();
sch.yield();
}
};
//! 消费者
auto routine2_entry = [&] (Scheduler &sch) {
while (!sch.isCanceled()) {
sem.acquire();
++count;
}
};
sch.create(routine1_entry);
sch.create(routine2_entry);
sp_loop->exitLoop(chrono::milliseconds(100));
sp_loop->runLoop();
EXPECT_EQ(times, count);
sch.cleanup();
}
/**
* 用定时器定时通过sem释放资源,协程循环接收数据
*
* 主要测试主协程的loop事件与子协程之间是否存在冲突
*/
TEST(Semaphore, TimerProduceAndConsumer)
{
Loop *sp_loop = event::Loop::New();
SetScopeExitAction([sp_loop]{ delete sp_loop;});
Scheduler sch(sp_loop);
Semaphore sem(sch, 0);
int times = 10;
int count = 0;
//! 定时器,定时生产
int index = 0;
auto timer = sp_loop->newTimerEvent();
timer->initialize(chrono::milliseconds(10), Event::Mode::kPersist);
SetScopeExitAction([timer]{ delete timer;});
timer->setCallback(
[&] {
if (index < times) {
sem.release();
++index;
} else {
sp_loop->exitLoop();
}
}
);
timer->enable();
//! 消费者
auto routine2_entry = [&] (Scheduler &sch) {
while (!sch.isCanceled()) {
sem.acquire();
++count;
}
};
sch.create(routine2_entry);
sp_loop->runLoop();
EXPECT_EQ(count, times);
sch.cleanup();
}