-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathchannel_test.cpp
194 lines (160 loc) �� 4.91 KB
/
channel_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
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
/*
* .============.
* // 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 "channel.hpp"
#include <tbox/base/scope_exit.hpp>
#include <tbox/event/timer_event.h>
#include <algorithm>
using namespace std;
using namespace tbox;
using namespace tbox::event;
using namespace tbox::coroutine;
/**
* 生产者 -- 消费者测试
*/
TEST(Channel, TwoRoutines_ProduceAndConsumer)
{
Loop *sp_loop = event::Loop::New();
SetScopeExitAction([sp_loop]{ delete sp_loop;});
Scheduler sch(sp_loop);
Channel<int> ch(sch);
vector<int> send_vec;
vector<int> recv_vec;
int times = 200;
for (int i = 0; i < times; ++i)
send_vec.push_back(i);
random_shuffle(send_vec.begin(), send_vec.end());
//! 生产者,将 send_vec 中的数据逐一发送到 ch
auto routine1_entry = [&] (Scheduler &sch) {
for (int i : send_vec) {
ch << i;
sch.yield();
}
};
//! 消费者,从 ch 中读取数据,存入到 recv_vec 中
auto routine2_entry = [&] (Scheduler &sch) {
while (!sch.isCanceled()) {
int i = 0;
if (ch >> i) {
recv_vec.push_back(i);
}
}
};
sch.create(routine1_entry);
sch.create(routine2_entry);
sp_loop->exitLoop(chrono::milliseconds(100));
sp_loop->runLoop();
//! 检查发送的数据与接收到数据是否对应
EXPECT_EQ(send_vec, recv_vec);
sp_loop->cleanup();
}
/**
* 一个协程向ch发送数据,两个协程从ch取数据来处理
*
* 测试多个协程同时向同一个Channel取数据的功能是否正常
*/
TEST(Channel, ThreeRoutines_OneProduceAndTwoConsumer)
{
Loop *sp_loop = event::Loop::New();
SetScopeExitAction([sp_loop]{ delete sp_loop;});
Scheduler sch(sp_loop);
Channel<int> ch(sch);
vector<int> send_vec;
vector<int> recv_vec;
int times = 200;
for (int i = 0; i < times; ++i)
send_vec.push_back(i);
random_shuffle(send_vec.begin(), send_vec.end());
//! 生产者,将 send_vec 中的数据逐一发送到 ch
auto routine1_entry = [&] (Scheduler &sch) {
for (int i : send_vec) {
ch << i;
//cout << sch.getName() << " << " << i << endl;
sch.yield();
if (sch.isCanceled())
break;
}
};
//! 消费者,从 ch 中读取数据,存入到 recv_vec 中
auto routine2_entry = [&] (Scheduler &sch) {
while (!sch.isCanceled()) {
int i = 0;
if (ch >> i) {
recv_vec.push_back(i);
//cout << sch.getName() << " >> " << i << endl;
}
}
};
sch.create(routine1_entry, true, "r1");
sch.create(routine2_entry, true, "r2");
sch.create(routine2_entry, true, "r3");
sp_loop->exitLoop(chrono::milliseconds(100));
sp_loop->runLoop();
//! 检查发送的数据与接收到数据是否对应
EXPECT_EQ(send_vec, recv_vec);
}
/**
* 用定时器定时向ch发送数据,协程循环接收数据
*
* 主要测试主协程的loop事件与子协程之间是否存在冲突
*/
TEST(Channel, TimerProduceAndConsumer)
{
Loop *sp_loop = event::Loop::New();
SetScopeExitAction([sp_loop]{ delete sp_loop;});
Scheduler sch(sp_loop);
Channel<int> ch(sch);
vector<int> send_vec;
vector<int> recv_vec;
int times = 10;
for (int i = 0; i < times; ++i)
send_vec.push_back(i);
random_shuffle(send_vec.begin(), send_vec.end());
size_t index = 0;
auto timer = sp_loop->newTimerEvent();
SetScopeExitAction([timer]{ delete timer;});
timer->initialize(chrono::milliseconds(10), Event::Mode::kPersist);
timer->setCallback(
[&] {
if (index < send_vec.size()) {
ch << send_vec.at(index);
++index;
} else {
sp_loop->exitLoop();
}
}
);
timer->enable();
//! 消费者,从 ch 中读取数据,存入到 recv_vec 中
auto routine2_entry = [&] (Scheduler &sch) {
while (!sch.isCanceled()) {
int i = 0;
if (ch >> i) {
recv_vec.push_back(i);
}
}
};
sch.create(routine2_entry);
sp_loop->runLoop();
//! 检查发送的数据与接收到数据是否对应
EXPECT_EQ(send_vec, recv_vec);
sch.cleanup();
sp_loop->cleanup();
}