Skip to content

Commit ae5f3fc

Browse files
authored
Add core tests r85 (#5776)
1 parent d7d7abd commit ae5f3fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1479
-533
lines changed

core-tests/include/test_core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#define TEST_HTTP_DOMAIN "www.gov.cn"
3939
#define TEST_HTTP_EXPECT "Location: https://www.gov.cn/"
40+
#define TEST_HTTPS_EXPECT "中国政府网"
4041

4142
#define TEST_STR "hello world, hello swoole\n"
4243
#define TEST_STR2 "I am Rango\n"
@@ -59,7 +60,7 @@
5960
#define ASSERT_ERREQ(x) ASSERT_EQ(swoole_get_last_error(), x)
6061
#define EXPECT_ERREQ(x) EXPECT_EQ(swoole_get_last_error(), x)
6162

62-
#define TIMER_PARAMS Timer *timer, TimerNode *tnode
63+
#define TIMER_PARAMS swoole::Timer *timer, swoole::TimerNode *tnode
6364

6465
#ifdef SW_VERBOSE
6566
#define DEBUG() std::cout

core-tests/src/_lib/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ Server::Server(std::string _host, int _port, swoole::Server::Mode _mode, int _ty
4040
serv.private_data_2 = this;
4141

4242
if (!listen(host, port, (swSocketType) type)) {
43-
swoole_warning("listen(%s:%d) fail[error=%d].", host.c_str(), port, errno);
43+
swoole_sys_warning("listen(%s:%d) failed", host.c_str(), port);
4444
exit(0);
4545
}
4646

4747
if (serv.create() < 0) {
48-
swoole_warning("create server fail[error=%d].", errno);
48+
swoole_sys_warning("create server failed");
4949
exit(0);
5050
}
5151
}

core-tests/src/core/base.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,28 @@ TEST(base, fatal_error) {
342342
ASSERT_TRUE(rs->contains("(ERROR 9999)"));
343343
File::remove(TEST_LOG_FILE);
344344
}
345+
346+
TEST(base, spinlock) {
347+
test::counter_init();
348+
auto counter = test::counter_ptr();
349+
int n = 4096;
350+
351+
auto test_fn = [counter, n]() {
352+
SW_LOOP_N(n) {
353+
sw_spinlock((sw_atomic_t *) &counter[0]);
354+
counter[1]++;
355+
if (i % 100 == 0) {
356+
usleep(5);
357+
}
358+
sw_spinlock_release((sw_atomic_t *) &counter[0]);
359+
}
360+
};
361+
362+
std::thread t1(test_fn);
363+
std::thread t2(test_fn);
364+
365+
t1.join();
366+
t2.join();
367+
368+
ASSERT_EQ(counter[1], n * 2);
369+
}

core-tests/src/core/hash.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,44 @@
2020
#include "test_core.h"
2121
#include "swoole_hash.h"
2222

23+
static const int hash_count = 8192;
24+
static const int str_max_len = 1024;
2325
static const char *data = "hello world, PHP the best.";
2426

2527
TEST(hash, crc32) {
2628
ASSERT_EQ(swoole_crc32(data, strlen(data)), 2962796788);
2729
}
2830

2931
static void test_hash_func(uint64_t (*hash_fn)(const char *key, size_t len), int n) {
32+
std::vector<uint64_t> hashes;
33+
std::vector<std::string> data;
34+
hashes.resize(n);
35+
data.resize(n);
36+
3037
SW_LOOP_N(n) {
31-
size_t len = 1 + swoole_random_int() % 256;
32-
char buf[256];
38+
size_t len = 1 + swoole_random_int() % str_max_len;
39+
char buf[str_max_len];
3340
ASSERT_EQ(swoole_random_bytes(buf, len), len);
34-
ASSERT_GT(hash_fn(buf, len), 0);
41+
hashes[i] = hash_fn(buf, len);
42+
data[i] = std::string(buf, len);
43+
}
44+
45+
usleep(100);
46+
47+
SW_LOOP_N(n) {
48+
auto &s = data.at(i);
49+
ASSERT_EQ(hashes[i], hash_fn(s.c_str(), s.length()));
3550
}
3651
}
3752

3853
TEST(hash, php) {
39-
test_hash_func(swoole_hash_jenkins, 100);
54+
test_hash_func(swoole_hash_php, hash_count);
4055
}
4156

4257
TEST(hash, jenkins) {
43-
test_hash_func(swoole_hash_jenkins, 100);
58+
test_hash_func(swoole_hash_jenkins, hash_count);
4459
}
4560

4661
TEST(hash, austin) {
47-
test_hash_func(swoole_hash_austin, 100);
62+
test_hash_func(swoole_hash_austin, hash_count);
4863
}

core-tests/src/coroutine/async.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,40 @@ TEST(coroutine_async, error) {
5959
ASSERT_EQ(errno, ENOENT);
6060
});
6161
}
62+
63+
TEST(coroutine_async, cancel) {
64+
coroutine::run([](void *arg) {
65+
AsyncEvent ev = {};
66+
auto co = swoole::Coroutine::get_current();
67+
swoole_timer_after(50, [co](TIMER_PARAMS) { co->cancel(); });
68+
69+
bool retval = swoole::coroutine::async(
70+
[](AsyncEvent *event) {
71+
usleep(200000);
72+
event->retval = magic_code;
73+
},
74+
ev);
75+
76+
ASSERT_EQ(retval, false);
77+
ASSERT_EQ(ev.error, SW_ERROR_CO_CANCELED);
78+
DEBUG() << "done\n";
79+
});
80+
}
81+
82+
TEST(coroutine_async, timeout) {
83+
coroutine::run([](void *arg) {
84+
AsyncEvent ev = {};
85+
86+
bool retval = swoole::coroutine::async(
87+
[](AsyncEvent *event) {
88+
usleep(200000);
89+
event->retval = magic_code;
90+
},
91+
ev,
92+
0.1);
93+
94+
ASSERT_EQ(retval, false);
95+
ASSERT_EQ(ev.error, SW_ERROR_CO_TIMEDOUT);
96+
DEBUG() << "done\n";
97+
});
98+
}

core-tests/src/coroutine/socket.cpp

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ TEST(coroutine_socket, sendmsg_and_recvmsg) {
13711371
});
13721372
}
13731373

1374-
std::pair<std::shared_ptr<Socket>, std::shared_ptr<Socket>> create_socket_pair() {
1374+
std::pair<std::shared_ptr<Socket>, std::shared_ptr<Socket> > create_socket_pair() {
13751375
int pairs[2];
13761376
socketpair(AF_UNIX, SOCK_STREAM, 0, pairs);
13771377

@@ -1381,7 +1381,7 @@ std::pair<std::shared_ptr<Socket>, std::shared_ptr<Socket>> create_socket_pair()
13811381
sock0->get_socket()->set_buffer_size(65536);
13821382
sock1->get_socket()->set_buffer_size(65536);
13831383

1384-
std::pair<std::shared_ptr<Socket>, std::shared_ptr<Socket>> result(sock0, sock1);
1384+
std::pair<std::shared_ptr<Socket>, std::shared_ptr<Socket> > result(sock0, sock1);
13851385
return result;
13861386
}
13871387

@@ -1478,3 +1478,73 @@ TEST(coroutine_socket, option) {
14781478
optval *= 2;
14791479
ASSERT_TRUE(sock.set_option(SOL_SOCKET, SO_RCVBUF, optval));
14801480
}
1481+
1482+
static void test_ssl_verify() {
1483+
Socket sock(SW_SOCK_TCP);
1484+
sock.enable_ssl_encrypt();
1485+
sock.set_tls_host_name(TEST_HTTP_DOMAIN);
1486+
sock.set_ssl_verify_peer(true);
1487+
ASSERT_TRUE(sock.connect(TEST_HTTP_DOMAIN, 443));
1488+
ASSERT_TRUE(sock.ssl_verify(false));
1489+
1490+
auto req = swoole::test::http_get_request(TEST_HTTP_DOMAIN, "/");
1491+
ASSERT_EQ(sock.send(req.c_str(), req.length()), req.length());
1492+
ASSERT_TRUE(sock.check_liveness());
1493+
1494+
String buf(65536);
1495+
SW_LOOP {
1496+
auto n = sock.recv(buf.str + buf.length, buf.size - buf.length);
1497+
if (n <= 0) {
1498+
break;
1499+
}
1500+
buf.grow(n);
1501+
}
1502+
1503+
ASSERT_TRUE(buf.contains(TEST_HTTPS_EXPECT));
1504+
1505+
usleep(50000);
1506+
ASSERT_FALSE(sock.check_liveness());
1507+
}
1508+
1509+
TEST(coroutine_socket, ssl_verify) {
1510+
coroutine::run([](void *arg) { test_ssl_verify(); });
1511+
}
1512+
1513+
TEST(coroutine_socket, shutdown) {
1514+
coroutine::run([](void *arg) {
1515+
Socket sock(SW_SOCK_TCP);
1516+
ASSERT_TRUE(sock.connect(TEST_HTTP_DOMAIN, 80));
1517+
ASSERT_TRUE(sock.shutdown(SHUT_RD));
1518+
ASSERT_FALSE(sock.shutdown(SHUT_RD));
1519+
ASSERT_ERREQ(ENOTCONN);
1520+
1521+
ASSERT_TRUE(sock.shutdown(SHUT_WR));
1522+
ASSERT_FALSE(sock.shutdown(SHUT_WR));
1523+
ASSERT_ERREQ(ENOTCONN);
1524+
1525+
ASSERT_FALSE(sock.shutdown());
1526+
ASSERT_ERREQ(ENOTCONN);
1527+
});
1528+
}
1529+
1530+
TEST(coroutine_socket, recv_packet) {
1531+
coroutine::run([](void *arg) {
1532+
Socket sock(SW_SOCK_TCP);
1533+
ASSERT_TRUE(sock.connect(TEST_HTTP_DOMAIN, 80));
1534+
auto req = swoole::test::http_get_request(TEST_HTTP_DOMAIN, "/");
1535+
ASSERT_EQ(sock.send(req.c_str(), req.length()), req.length());
1536+
ASSERT_TRUE(sock.check_liveness());
1537+
auto n = sock.recv_packet();
1538+
ASSERT_GT(n, 0);
1539+
auto buf = sock.get_read_buffer();
1540+
ASSERT_TRUE(buf->contains(TEST_HTTP_EXPECT));
1541+
});
1542+
}
1543+
1544+
TEST(coroutine_socket, set_error) {
1545+
Socket sock(SW_SOCK_TCP);
1546+
sock.set_err(1000, std::string(TEST_STR));
1547+
1548+
ASSERT_EQ(sock.errCode, 1000);
1549+
ASSERT_STREQ(sock.errMsg, TEST_STR);
1550+
}

core-tests/src/coroutine/system.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,30 @@ TEST(coroutine_system, wait_event_readable) {
138138
ASSERT_GT(p.write(GREETING, strlen(GREETING)), 0);
139139
});
140140

141+
// bad fd
142+
EXPECT_EQ(System::wait_event(9999, SW_EVENT_READ, 1), -1);
143+
EXPECT_EQ(errno, EBADF);
144+
EXPECT_ERREQ(EBADF);
145+
146+
// trigger event
141147
char buffer[128];
142148
auto pipe_sock = p.get_socket(false);
143-
System::wait_event(pipe_sock->get_fd(), SW_EVENT_READ, 1);
149+
// readable
150+
EXPECT_EQ(System::wait_event(pipe_sock->get_fd(), SW_EVENT_READ, 1), SW_EVENT_READ);
151+
// readable + writable
152+
EXPECT_EQ(System::wait_event(pipe_sock->get_fd(), SW_EVENT_READ | SW_EVENT_WRITE, 1),
153+
SW_EVENT_READ | SW_EVENT_WRITE);
154+
144155
ssize_t n = pipe_sock->read(buffer, sizeof(buffer));
145156
buffer[n] = 0;
146157
EXPECT_EQ(strlen(GREETING), n);
147158
EXPECT_STREQ(GREETING, buffer);
159+
160+
// timeout
161+
auto pipe_sock_2 = p.get_socket(true);
162+
EXPECT_EQ(System::wait_event(pipe_sock_2->get_fd(), SW_EVENT_READ, 0.1), -1);
163+
EXPECT_EQ(errno, SW_ERROR_CO_TIMEDOUT);
164+
EXPECT_ERREQ(SW_ERROR_CO_TIMEDOUT);
148165
});
149166
}
150167

core-tests/src/lock/lock.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ using swoole::RWLock;
2929
#ifdef HAVE_SPINLOCK
3030
using swoole::SpinLock;
3131
#endif
32-
using swoole::Mutex;
33-
using swoole::CoroutineLock;
3432
using swoole::Coroutine;
35-
using swoole::test::coroutine;
33+
using swoole::CoroutineLock;
34+
using swoole::Mutex;
3635
using swoole::coroutine::System;
36+
using swoole::test::coroutine;
3737

3838
static void test_func(swLock &lock) {
3939
int count = 0;
@@ -166,6 +166,33 @@ TEST(lock, coroutine_lock) {
166166
delete lock;
167167
}
168168

169+
TEST(lock, coroutine_lock_rd) {
170+
CoroutineLock *lock = new CoroutineLock(false);
171+
ASSERT_EQ(lock->lock_rd(), SW_ERROR_CO_OUT_OF_COROUTINE);
172+
auto callback = [lock]() {
173+
coroutine::run([lock](void *arg) {
174+
Coroutine::create([lock](void *) {
175+
ASSERT_EQ(lock->lock_rd(), 0);
176+
ASSERT_EQ(lock->lock_rd(), 0);
177+
System::sleep(1);
178+
ASSERT_EQ(lock->unlock(), 0);
179+
});
180+
181+
Coroutine::create([lock](void *) {
182+
ASSERT_EQ(lock->lock_rd(), 0);
183+
System::sleep(1);
184+
ASSERT_EQ(lock->unlock(), 0);
185+
});
186+
187+
Coroutine::create([lock](void *) { ASSERT_EQ(lock->trylock_rd(), EBUSY); });
188+
});
189+
};
190+
191+
std::thread t1(callback);
192+
t1.join();
193+
delete lock;
194+
}
195+
169196
#ifdef HAVE_RWLOCK
170197
TEST(lock, rwlock_shared) {
171198
RWLock lock(Mutex::PROCESS_SHARED);

core-tests/src/memory/lru_cache.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using namespace swoole;
55
using namespace std;
66

7-
LRUCache cache(2);
8-
97
int dtor_num = 0;
108
class lru_cache_test_class {
119
public:
@@ -17,6 +15,7 @@ class lru_cache_test_class {
1715
};
1816

1917
TEST(lru_cache, basic) {
18+
LRUCache<string> cache(2);
2019
shared_ptr<string> val = make_shared<string>("hello");
2120
shared_ptr<string> val1 = make_shared<string>("hello1");
2221

@@ -37,6 +36,7 @@ TEST(lru_cache, basic) {
3736
}
3837

3938
TEST(lru_cache, memory_free) {
39+
LRUCache<lru_cache_test_class> cache(2);
4040
shared_ptr<lru_cache_test_class> val = make_shared<lru_cache_test_class>();
4141
cache.set("test", val);
4242
ASSERT_EQ(cache.get("test").get(), val.get());
@@ -47,6 +47,7 @@ TEST(lru_cache, memory_free) {
4747
}
4848

4949
TEST(lru_cache, lru_kick) {
50+
LRUCache<lru_cache_test_class> cache(2);
5051
dtor_num = 0;
5152
shared_ptr<lru_cache_test_class> val = make_shared<lru_cache_test_class>();
5253
shared_ptr<lru_cache_test_class> val1 = make_shared<lru_cache_test_class>();
@@ -69,9 +70,9 @@ TEST(lru_cache, lru_kick) {
6970
ASSERT_EQ(dtor_num, 1);
7071
ASSERT_EQ(cache.get("test"), nullptr);
7172

72-
shared_ptr<string> val_str = make_shared<string>("hello");
73-
cache.set("test1", val_str); // update test1 and will del test2
74-
ASSERT_EQ(cache.get("test1").get(), val_str.get());
73+
shared_ptr<lru_cache_test_class> val4 = make_shared<lru_cache_test_class>();
74+
cache.set("test1", val4); // update test1 and will del test2
75+
ASSERT_EQ(cache.get("test1").get(), val4.get());
7576
ASSERT_EQ(dtor_num, 2);
7677

7778
cache.set("test3", val3);

0 commit comments

Comments
 (0)