Skip to content

Commit 8090b43

Browse files
authored
Added more core tests, Increase test coverage to 83% (#5779)
1 parent 1af6641 commit 8090b43

File tree

16 files changed

+307
-19
lines changed

16 files changed

+307
-19
lines changed

.github/workflows/ext.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ env:
77

88
jobs:
99
build-ubuntu-latest:
10-
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[ext]')"
10+
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[ubuntu]')"
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v4

core-tests/src/core/base.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,36 @@ TEST(base, spinlock) {
367367

368368
ASSERT_EQ(counter[1], n * 2);
369369
}
370+
371+
TEST(base, futex) {
372+
sw_atomic_t value = 1;
373+
374+
std::thread t1([&value] {
375+
DEBUG() << "wait 1\n";
376+
ASSERT_EQ(sw_atomic_futex_wait(&value, -1), SW_OK); // no wait
377+
value = 0;
378+
379+
DEBUG() << "wait 2\n";
380+
381+
ASSERT_EQ(sw_atomic_futex_wait(&value, 0.05), SW_ERR); // timed out
382+
ASSERT_EQ(sw_atomic_futex_wait(&value, 0.5), SW_OK); // success
383+
384+
DEBUG() << "wait 3\n";
385+
386+
value = 0;
387+
ASSERT_EQ(sw_atomic_futex_wait(&value, -1), SW_OK); // no timeout
388+
});
389+
390+
std::thread t2([&value] {
391+
usleep(100000);
392+
DEBUG() << "wakeup 1\n";
393+
ASSERT_EQ(sw_atomic_futex_wakeup(&value, 1), 1);
394+
395+
DEBUG() << "wakeup 2\n";
396+
usleep(100000);
397+
ASSERT_EQ(sw_atomic_futex_wakeup(&value, 1), 1);
398+
});
399+
400+
t1.join();
401+
t2.join();
402+
}

core-tests/src/core/log.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,39 @@ TEST(log, level) {
1515
for (auto iter = processTypes.begin(); iter != processTypes.end(); iter++) {
1616
swoole_set_worker_type(*iter);
1717
sw_logger()->reset();
18+
19+
ASSERT_FALSE(sw_logger()->is_opened());
20+
21+
sw_logger()->set_level(999);
22+
ASSERT_EQ(sw_logger()->get_level(), SW_LOG_NONE);
23+
24+
sw_logger()->set_level(SW_LOG_DEBUG - 10);
25+
ASSERT_EQ(sw_logger()->get_level(), SW_LOG_DEBUG);
26+
1827
sw_logger()->set_level(SW_LOG_NOTICE);
1928
sw_logger()->open(file);
2029

30+
ASSERT_TRUE(sw_logger()->is_opened());
31+
32+
sw_logger()->put(SW_LOG_DEBUG, SW_STRL("hello no debug"));
33+
sw_logger()->put(SW_LOG_TRACE, SW_STRL("hello no trace"));
2134
sw_logger()->put(SW_LOG_INFO, SW_STRL("hello info"));
2235
sw_logger()->put(SW_LOG_NOTICE, SW_STRL("hello notice"));
2336
sw_logger()->put(SW_LOG_WARNING, SW_STRL("hello warning"));
2437

38+
sw_logger()->set_level(SW_LOG_DEBUG);
39+
sw_logger()->put(SW_LOG_DEBUG, SW_STRL("hello debug"));
40+
sw_logger()->put(SW_LOG_TRACE, SW_STRL("hello trace"));
41+
2542
auto content = file_get_contents(file);
2643

2744
sw_logger()->close();
2845
unlink(file);
2946

47+
ASSERT_FALSE(content->contains(SW_STRL("hello no debug")));
48+
ASSERT_FALSE(content->contains(SW_STRL("hello no trace")));
49+
ASSERT_TRUE(content->contains(SW_STRL("hello debug")));
50+
ASSERT_TRUE(content->contains(SW_STRL("hello trace")));
3051
ASSERT_FALSE(content->contains(SW_STRL("hello info")));
3152
ASSERT_TRUE(content->contains(SW_STRL("hello notice")));
3253
ASSERT_TRUE(content->contains(SW_STRL("hello warning")));

core-tests/src/memory/table.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ class table_t {
5858
throw exception_t("alloc failed", swoole_get_last_error());
5959
}
6060

61-
table->add_column("id", TableColumn::TYPE_INT, 0);
62-
table->add_column("name", TableColumn::TYPE_STRING, 32);
63-
table->add_column("score", TableColumn::TYPE_FLOAT, 0);
61+
EXPECT_TRUE(table->add_column("id", TableColumn::TYPE_INT, 0));
62+
EXPECT_TRUE(table->add_column("name", TableColumn::TYPE_STRING, 32));
63+
EXPECT_TRUE(table->add_column("score", TableColumn::TYPE_FLOAT, 0));
64+
65+
EXPECT_FALSE(table->add_column("bad_field", (TableColumn::Type) 8, 0));
6466

6567
if (!table->create()) {
6668
throw exception_t("create failed", swoole_get_last_error());
@@ -134,6 +136,8 @@ class table_t {
134136
TEST(table, create) {
135137
table_t table(1024);
136138

139+
ASSERT_GT(table.ptr()->get_memory_size(), table.ptr()->get_size() * table.ptr()->get_column_size());
140+
137141
table.set("php", {"php", 1, 1.245});
138142
table.set("java", {"java", 2, 3.1415926});
139143
table.set("c++", {"c++", 3, 4.888});

core-tests/src/network/dns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ TEST(dns, cancel) {
6464
test::coroutine::run([](void *arg) {
6565
auto co = Coroutine::get_current_safe();
6666
Coroutine::create([co](void *) {
67-
System::sleep(0.002);
67+
System::sleep(0.001);
6868
co->cancel();
6969
});
7070
auto list1 = swoole::coroutine::dns_lookup("www.baidu-not-found-for-cancel.com", AF_INET, 2);

core-tests/src/os/timer.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,42 @@ TEST(timer, error) {
151151
swoole_timer_delay(nullptr, 100);
152152
ASSERT_FALSE(swoole_timer_del(nullptr));
153153
SwooleTG.timer = tmp;
154+
155+
swoole_timer_free();
156+
}
157+
158+
TEST(timer, reinit) {
159+
int timer1_count = 0;
160+
int timer2_count = 0;
161+
162+
swoole_timer_after(
163+
20,
164+
[&](Timer *, TimerNode *) {
165+
timer1_count++;
166+
DEBUG() << "timer2_count" << timer2_count << "\n";
167+
},
168+
nullptr);
169+
170+
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
171+
172+
sw_timer()->reinit(sw_reactor());
173+
174+
uint64_t ms1 = swoole::time<std::chrono::milliseconds>();
175+
176+
swoole_timer_tick(
177+
100,
178+
[&](Timer *, TimerNode *tnode) {
179+
timer2_count++;
180+
DEBUG() << "timer2_count" << timer2_count << "\n";
181+
if (timer2_count == 5) {
182+
swoole_timer_del(tnode);
183+
}
184+
},
185+
nullptr);
186+
187+
swoole_event_wait();
188+
uint64_t ms2 = swoole::time<std::chrono::milliseconds>();
189+
ASSERT_LE(ms2 - ms1, 510);
190+
ASSERT_EQ(timer1_count, 1);
191+
ASSERT_EQ(timer2_count, 5);
154192
}

core-tests/src/server/server.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,3 +2895,181 @@ TEST(server, no_idle_task_worker) {
28952895

28962896
remove(TEST_LOG_FILE);
28972897
}
2898+
2899+
static void test_conn_overflow(Server::Mode mode, bool send_yield) {
2900+
Server serv(mode);
2901+
serv.worker_num = 1;
2902+
serv.send_yield = send_yield;
2903+
swoole_set_log_level(SW_LOG_WARNING);
2904+
2905+
test::counter_init();
2906+
auto counter = test::counter_ptr();
2907+
2908+
Mutex *lock = new Mutex(Mutex::PROCESS_SHARED);
2909+
lock->lock();
2910+
2911+
ListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
2912+
if (!port) {
2913+
swoole_warning("listen failed, [error=%d]", swoole_get_last_error());
2914+
exit(2);
2915+
}
2916+
2917+
ASSERT_EQ(serv.create(), SW_OK);
2918+
2919+
thread t1;
2920+
serv.onStart = [&lock, &t1](Server *serv) {
2921+
t1 = thread([=]() {
2922+
swoole_signal_block_all();
2923+
2924+
lock->lock();
2925+
2926+
ListenPort *port = serv->get_primary_port();
2927+
2928+
network::SyncClient c(SW_SOCK_TCP);
2929+
c.connect(TEST_HOST, port->port);
2930+
c.send(packet, strlen(packet));
2931+
char buf[1024];
2932+
c.recv(buf, sizeof(buf));
2933+
c.close();
2934+
2935+
kill(serv->gs->master_pid, SIGTERM);
2936+
});
2937+
};
2938+
2939+
serv.onWorkerStart = [&lock](Server *serv, Worker *worker) {
2940+
if (worker->id == 0) {
2941+
lock->unlock();
2942+
}
2943+
test::counter_incr(3);
2944+
DEBUG() << "onWorkerStart: id=" << worker->id << "\n";
2945+
};
2946+
2947+
serv.onReceive = [counter, send_yield](Server *serv, RecvData *req) -> int {
2948+
auto sid = req->session_id();
2949+
auto conn = serv->get_connection_by_session_id(sid);
2950+
conn->overflow = 1;
2951+
2952+
EXPECT_FALSE(serv->send(sid, SW_STRL(TEST_STR)));
2953+
EXPECT_ERREQ(send_yield ? SW_ERROR_OUTPUT_SEND_YIELD : SW_ERROR_OUTPUT_BUFFER_OVERFLOW);
2954+
2955+
counter[0] = 1;
2956+
2957+
swoole_timer_after(100, [serv, sid](TIMER_PARAMS) { serv->close(sid); });
2958+
2959+
return SW_OK;
2960+
};
2961+
2962+
ASSERT_EQ(serv.start(), 0);
2963+
2964+
t1.join();
2965+
delete lock;
2966+
ASSERT_EQ(counter[0], 1);
2967+
ASSERT_EQ(counter[3], 1);
2968+
}
2969+
2970+
TEST(server, overflow_1) {
2971+
test_conn_overflow(Server::MODE_BASE, false);
2972+
}
2973+
2974+
TEST(server, overflow_2) {
2975+
test_conn_overflow(Server::MODE_PROCESS, false);
2976+
}
2977+
2978+
TEST(server, overflow_3) {
2979+
test_conn_overflow(Server::MODE_BASE, true);
2980+
}
2981+
2982+
TEST(server, overflow_4) {
2983+
test_conn_overflow(Server::MODE_PROCESS, true);
2984+
}
2985+
2986+
TEST(server, send_timeout) {
2987+
Server serv(Server::MODE_BASE);
2988+
serv.worker_num = 1;
2989+
swoole_set_log_level(SW_LOG_WARNING);
2990+
2991+
test::counter_init();
2992+
auto counter = test::counter_ptr();
2993+
2994+
Mutex *lock = new Mutex(Mutex::PROCESS_SHARED);
2995+
lock->lock();
2996+
2997+
ListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
2998+
if (!port) {
2999+
swoole_warning("listen failed, [error=%d]", swoole_get_last_error());
3000+
exit(2);
3001+
}
3002+
3003+
port->max_idle_time = 1;
3004+
3005+
String wbuf(2 * 1024 * 1024);
3006+
wbuf.append_random_bytes(2 * 1024 * 1024, false);
3007+
3008+
ASSERT_EQ(serv.create(), SW_OK);
3009+
3010+
thread t1;
3011+
serv.onStart = [&lock, &t1, &wbuf](Server *serv) {
3012+
t1 = thread([=]() {
3013+
swoole_signal_block_all();
3014+
3015+
lock->lock();
3016+
3017+
ListenPort *port = serv->get_primary_port();
3018+
3019+
network::SyncClient c(SW_SOCK_TCP);
3020+
c.connect(TEST_HOST, port->port);
3021+
c.send(packet, strlen(packet));
3022+
3023+
String rbuf(3 * 1024 * 1024);
3024+
3025+
auto rn = c.recv(rbuf.str, 1024);
3026+
EXPECT_EQ(rn, 1024);
3027+
rbuf.length += 1024;
3028+
3029+
sleep(2);
3030+
3031+
while (true) {
3032+
rn = c.recv(rbuf.str + rbuf.length, rbuf.size - rbuf.length);
3033+
if (rn <= 0) {
3034+
break;
3035+
}
3036+
rbuf.length += rn;
3037+
}
3038+
3039+
EXPECT_MEMEQ(rbuf.str, wbuf.str, rbuf.length);
3040+
c.close();
3041+
3042+
kill(serv->gs->master_pid, SIGTERM);
3043+
});
3044+
};
3045+
3046+
serv.onWorkerStart = [&lock](Server *serv, Worker *worker) {
3047+
if (worker->id == 0) {
3048+
lock->unlock();
3049+
}
3050+
test::counter_incr(3);
3051+
DEBUG() << "onWorkerStart: id=" << worker->id << "\n";
3052+
};
3053+
3054+
serv.onReceive = [&wbuf](Server *serv, RecvData *req) -> int {
3055+
auto sid = req->session_id();
3056+
auto conn = serv->get_connection_by_session_id(sid);
3057+
3058+
swoole_timer_del(conn->socket->recv_timer);
3059+
conn->socket->recv_timer = nullptr;
3060+
conn->socket->set_buffer_size(65536);
3061+
3062+
EXPECT_TRUE(serv->send(sid, wbuf.str, wbuf.length));
3063+
3064+
test::counter_incr(0);
3065+
3066+
return SW_OK;
3067+
};
3068+
3069+
ASSERT_EQ(serv.start(), 0);
3070+
3071+
t1.join();
3072+
delete lock;
3073+
ASSERT_EQ(counter[0], 1);
3074+
ASSERT_EQ(counter[3], 1);
3075+
}

include/swoole_reactor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class Reactor {
363363
static int _writable_callback(Reactor *reactor, Event *ev);
364364
static ssize_t write_func(Reactor *reactor,
365365
network::Socket *socket,
366-
size_t _len,
366+
const size_t _len,
367367
const std::function<ssize_t()> &send_fn,
368368
const std::function<void(Buffer *buffer)> &append_fn);
369369

include/swoole_ssl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include <openssl/bio.h>
2828
#include <openssl/err.h>
2929
#include <openssl/crypto.h>
30+
#include <openssl/x509.h>
31+
#include <openssl/x509v3.h>
32+
#include <openssl/rand.h>
33+
#include <openssl/conf.h>
3034
#include <openssl/opensslv.h>
3135

3236
#if OPENSSL_VERSION_NUMBER >= 0x10100000L

include/swoole_table.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ class Table {
180180
return size;
181181
}
182182

183+
size_t get_column_size() const {
184+
return column_map->size();
185+
}
186+
183187
TableRow *get_by_index(uint32_t index) const {
184188
TableRow *row = rows[index];
185189
return row->active ? row : nullptr;

src/core/timer.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ bool Timer::init_with_reactor(Reactor *reactor) {
9797
}
9898

9999
void Timer::reinit(Reactor *reactor) {
100+
close(this);
100101
init_with_reactor(reactor);
101102
reactor->timeout_msec = next_msec_;
102103
}

src/network/socket.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@
2323
#include "swoole_string.h"
2424
#include "swoole_timer.h"
2525

26-
#ifdef SW_USE_OPENSSL
27-
#include <openssl/x509.h>
28-
#include <openssl/x509v3.h>
29-
#include <openssl/rand.h>
30-
#include <openssl/conf.h>
31-
#endif
32-
3326
namespace swoole {
3427
namespace network {
3528

0 commit comments

Comments
 (0)