Skip to content

Commit 8e6c3b9

Browse files
authored
Added more tests, Optimize tests, Refactor reload impl (#5764)
* Added tests --filter=[core] * Update core.yml --filter=[core] * Update core.yml --filter=[core] * Refactor reload impl --filter=[core]
1 parent c5d86a3 commit 8e6c3b9

File tree

17 files changed

+517
-429
lines changed

17 files changed

+517
-429
lines changed

.github/workflows/core.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ jobs:
6464
- name: run coverage
6565
shell: bash
6666
run: sudo apt-get install lcov &&
67-
sudo lcov --directory . --capture --output-file coverage.info &&
68-
sudo lcov --remove coverage.info "${{runner.workspace}}/swoole-src/include/*" '/usr/*' --output-file coverage.info &&
67+
sudo lcov --directory . --capture --branch-coverage --rc geninfo_unexecuted_blocks=1 --ignore-errors mismatch --output-file coverage.info &&
68+
sudo lcov --remove coverage.info '/usr/*' --output-file coverage.info &&
6969
sudo lcov --list coverage.info
7070

7171
- name: Upload coverage to Codecov

core-tests/code-stats.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh -e
2+
__CURRENT__=$(pwd)
3+
__DIR__=$(cd "$(dirname "$0")";pwd)
4+
5+
# enter the dir
6+
cd "${__DIR__}"
7+
cloc . --exclude-dir=js,Debug,CMakeFiles,build,CMakeFiles,deps

core-tests/include/test_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#define ASSERT_MEMEQ(x, y, n) ASSERT_EQ(memcmp((x), (y), n), 0)
5555
#define EXPECT_MEMEQ(x, y, n) EXPECT_EQ(memcmp((x), (y), n), 0)
5656

57+
#define TIMER_PARAMS Timer *timer, TimerNode *tnode
58+
5759
namespace swoole {
5860
struct HttpProxy;
5961
struct Socks5Proxy;

core-tests/src/coroutine/socket.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,19 @@ TEST(coroutine_socket, connect_refused) {
5151
TEST(coroutine_socket, connect_timeout) {
5252
coroutine::run([](void *arg) {
5353
Socket sock(SW_SOCK_TCP);
54+
5455
sock.set_timeout(0.5);
56+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_DNS), 0.5);
57+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_CONNECT), 0.5);
58+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_READ), 0.5);
59+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_WRITE), 0.5);
60+
61+
sock.set_timeout(1.5, Socket::TIMEOUT_RDWR);
62+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_DNS), 0.5);
63+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_CONNECT), 0.5);
64+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_READ), 1.5);
65+
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_WRITE), 1.5);
66+
5567
bool retval = sock.connect("192.0.0.1", 9801);
5668
ASSERT_EQ(retval, false);
5769
ASSERT_EQ(sock.errCode, ETIMEDOUT);
@@ -769,6 +781,27 @@ TEST(coroutine_socket, getsockname) {
769781
});
770782
}
771783

784+
TEST(coroutine_socket, buffer) {
785+
Socket sock(SW_SOCK_TCP);
786+
auto rbuf = sock.get_read_buffer();
787+
auto wbuf = sock.get_write_buffer();
788+
789+
auto rbuf_pop = sock.pop_read_buffer();
790+
auto wbuf_pop = sock.pop_write_buffer();
791+
792+
ASSERT_EQ(rbuf, rbuf_pop);
793+
ASSERT_EQ(wbuf, wbuf_pop);
794+
795+
auto rbuf2 = sock.get_read_buffer();
796+
auto wbuf2 = sock.get_write_buffer();
797+
798+
ASSERT_NE(rbuf2, rbuf);
799+
ASSERT_NE(wbuf2, wbuf);
800+
801+
delete rbuf_pop;
802+
delete wbuf_pop;
803+
}
804+
772805
TEST(coroutine_socket, check_liveness) {
773806
coroutine::run([](void *arg) {
774807
Socket sock(SW_SOCK_TCP);
@@ -1298,3 +1331,9 @@ TEST(coroutine_socket, cancel) {
12981331
ASSERT_TRUE(results["read"]);
12991332
});
13001333
}
1334+
1335+
TEST(coroutine_socket, get_event_str) {
1336+
Socket sock;
1337+
ASSERT_STREQ(sock.get_event_str(SW_EVENT_READ), "reading");
1338+
ASSERT_STREQ(sock.get_event_str(SW_EVENT_WRITE), "writing");
1339+
}

core-tests/src/os/process_pool.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,47 @@ TEST(process_pool, shutdown) {
176176
sysv_signal(SIGTERM, SIG_DFL);
177177
}
178178

179+
TEST(process_pool, reload) {
180+
ProcessPool pool{};
181+
int *shm_value = (int *) sw_mem_pool()->alloc(sizeof(int));
182+
ASSERT_EQ(pool.create(2), SW_OK);
183+
184+
// init
185+
pool.set_max_packet_size(8192);
186+
pool.ptr = shm_value;
187+
pool.max_wait_time = 1;
188+
189+
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
190+
int *shm_value = (int *) pool->ptr;
191+
(*shm_value)++;
192+
193+
sysv_signal(SIGTERM, SIG_IGN);
194+
195+
while (true) {
196+
sleep(10000);
197+
}
198+
};
199+
200+
pool.onStart = [](ProcessPool *pool) { swoole_timer_after(100, [pool](TIMER_PARAMS) { pool->reload(); }); };
201+
202+
pool.onBeforeReload = [](ProcessPool *pool) { printf("onBeforeReload\n"); };
203+
204+
pool.onAfterReload = [](ProcessPool *pool) {
205+
printf("onAfterReload\n");
206+
swoole_timer_after(100, [pool](TIMER_PARAMS) { pool->shutdown(); });
207+
};
208+
209+
current_pool = &pool;
210+
sysv_signal(SIGTERM, [](int sig) { current_pool->running = false; });
211+
212+
ASSERT_EQ(pool.start(), SW_OK);
213+
ASSERT_EQ(pool.wait(), SW_OK);
214+
215+
pool.destroy();
216+
217+
ASSERT_EQ(*shm_value, 4);
218+
}
219+
179220
TEST(process_pool, async) {
180221
ProcessPool pool{};
181222
ASSERT_EQ(pool.create(1, 0, SW_IPC_UNIXSOCK), SW_OK);

ext-src/swoole_process_pool.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ static void process_pool_signal_handler(int sig) {
308308
case SIGUSR1:
309309
case SIGUSR2:
310310
current_pool->reload();
311-
current_pool->reload_init = false;
312311
break;
313312
case SIGIO:
314313
current_pool->read_message = true;

include/swoole_coroutine.h

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,7 @@ class Coroutine {
144144
static void set_on_resume(SwapCallback func);
145145
static void set_on_close(SwapCallback func);
146146
static void bailout(BailoutCallback func);
147-
148-
static inline bool run(const CoroutineFunc &fn, void *args = nullptr) {
149-
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
150-
long cid = create(fn, args);
151-
swoole_event_wait();
152-
return cid > 0;
153-
}
147+
static bool run(const CoroutineFunc &fn, void *args = nullptr);
154148

155149
static inline long create(const CoroutineFunc &fn, void *args = nullptr) {
156150
#ifdef SW_USE_THREAD_CONTEXT
@@ -228,17 +222,7 @@ class Coroutine {
228222
return sw_likely(co) ? co->get_execute_usec() : -1;
229223
}
230224

231-
static inline void calc_execute_usec(Coroutine *yield_coroutine, Coroutine *resume_coroutine) {
232-
long current_usec = time<seconds_type>(true);
233-
if (yield_coroutine) {
234-
yield_coroutine->execute_usec += current_usec - yield_coroutine->switch_usec;
235-
}
236-
237-
if (resume_coroutine) {
238-
resume_coroutine->switch_usec = current_usec;
239-
}
240-
}
241-
225+
static void calc_execute_usec(Coroutine *yield_coroutine, Coroutine *resume_coroutine);
242226
static void print_list();
243227

244228
protected:
@@ -263,41 +247,9 @@ class Coroutine {
263247
Coroutine *origin = nullptr;
264248
CancelFunc *cancel_fn_ = nullptr;
265249

266-
Coroutine(const CoroutineFunc &fn, void *private_data) : ctx(stack_size, fn, private_data) {
267-
cid = ++last_cid;
268-
coroutines[cid] = this;
269-
if (sw_unlikely(count() > peak_num)) {
270-
peak_num = count();
271-
}
272-
if (!activated) {
273-
activate();
274-
}
275-
}
276-
277-
Coroutine(long _cid, const CoroutineFunc &fn, void *private_data) : ctx(stack_size, fn, private_data) {
278-
cid = _cid;
279-
}
280-
281-
long run() {
282-
long cid = this->cid;
283-
origin = current;
284-
current = this;
285-
CALC_EXECUTE_USEC(origin, nullptr);
286-
state = STATE_RUNNING;
287-
ctx.swap_in();
288-
check_end();
289-
return cid;
290-
}
291-
292-
void check_end() {
293-
if (ctx.is_end()) {
294-
close();
295-
} else if (sw_unlikely(on_bailout)) {
296-
SW_ASSERT(current == nullptr);
297-
on_bailout();
298-
}
299-
}
300-
250+
Coroutine(const CoroutineFunc &fn, void *private_data);
251+
long run();
252+
void check_end();
301253
void close();
302254
};
303255
//-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)