-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathtest_coroutine.h
73 lines (62 loc) · 1.96 KB
/
test_coroutine.h
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
#pragma once
#include "test_core.h"
#include "swoole_coroutine.h"
#include "swoole_coroutine_channel.h"
#include "swoole_coroutine_system.h"
#include "swoole_coroutine_socket.h"
#include "swoole_coroutine_c_api.h"
namespace swoole {
namespace test {
class coroutine {
public:
coroutine(const CoroutineFunc &_fn, void *_arg, int *_complete_num)
: fn(_fn), arg(_arg), complete_num(_complete_num) {}
void start() {
fn(arg);
(*complete_num)++;
}
inline static void create(const CoroutineFunc &fn, void *arg, int *complete_num) {
auto test = new coroutine(fn, arg, complete_num);
long cid = swoole::Coroutine::create(
[](void *arg) {
((coroutine *) arg)->start();
delete (coroutine *) arg;
},
test);
ASSERT_GT(cid, 0);
}
inline static void run(std::initializer_list<std::pair<CoroutineFunc, void *>> args) {
int complete_num = 0;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
Coroutine::activate();
for (const auto &arg : args) {
create(arg.first, arg.second, &complete_num);
}
swoole_event_wait();
Coroutine::deactivate();
}
inline static void run(std::initializer_list<CoroutineFunc> fns) {
int complete_num = 0;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
Coroutine::activate();
for (const auto &fn : fns) {
create(fn, nullptr, &complete_num);
}
swoole_event_wait();
Coroutine::deactivate();
}
inline static void run(const CoroutineFunc &fn, void *arg = nullptr) {
int complete_num = 0;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
Coroutine::activate();
create(fn, arg, &complete_num);
swoole_event_wait();
Coroutine::deactivate();
}
private:
CoroutineFunc fn;
void *arg;
int *complete_num;
};
} // namespace test
} // namespace swoole