-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathasync.cpp
99 lines (81 loc) · 3.5 KB
/
async.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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| @link https://www.swoole.com/ |
| @contact [email protected] |
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
| @Author Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "test_core.h"
#include "swoole_socket.h"
#include "swoole_async.h"
#include <atomic>
using namespace swoole;
static int callback_count;
TEST(async, dispatch) {
callback_count = 0;
std::atomic<int> handle_count(0);
AsyncEvent event = {};
event.object = &handle_count;
event.callback = [](AsyncEvent *event) { callback_count++; };
event.handler = [](AsyncEvent *event) { (*(std::atomic<int> *) event->object)++; };
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
for (int i = 0; i < 1000; ++i) {
auto ret = swoole::async::dispatch(&event);
EXPECT_EQ(ret->object, event.object);
}
swoole_event_wait();
ASSERT_EQ(handle_count, 1000);
ASSERT_EQ(callback_count, 1000);
}
TEST(async, schedule) {
callback_count = 0;
std::atomic<int> handle_count(0);
int N = 1000;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
AsyncEvent event{};
event.object = &handle_count;
event.callback = [](AsyncEvent *event) { callback_count++; };
event.handler = [](AsyncEvent *event) {
usleep(swoole_rand(50000, 100000));
(*(std::atomic<int> *) event->object)++;
};
SwooleG.aio_core_worker_num = 4;
SwooleG.aio_worker_num = 128;
SwooleG.aio_max_wait_time = 0.05;
SwooleG.aio_max_idle_time = 0.5;
int count = N;
swoole_timer_tick(2, [&count, &event, N](Timer *, TimerNode *timer) {
SW_LOOP_N(swoole_rand(5, 15)) {
auto ret = swoole::async::dispatch(&event);
EXPECT_EQ(ret->object, event.object);
count--;
if (count == 0) {
swoole_timer_del(timer);
ASSERT_GT(SwooleTG.async_threads->get_worker_num(), 16);
ASSERT_GT(SwooleTG.async_threads->get_queue_size(), 100);
ASSERT_GT(SwooleTG.async_threads->get_task_num(), 100);
break;
} else if (count == N - 1) {
ASSERT_EQ(SwooleTG.async_threads->get_worker_num(), 4);
ASSERT_LE(SwooleTG.async_threads->get_queue_size(), 1);
ASSERT_EQ(SwooleTG.async_threads->get_task_num(), 1);
} else if (count < N / 2) {
ASSERT_GT(SwooleTG.async_threads->get_worker_num(), 4);
}
}
});
swoole_event_wait();
ASSERT_EQ(handle_count, N);
ASSERT_EQ(callback_count, N);
}