Skip to content

Commit 1a68380

Browse files
authored
Add core tests r76 (#5750)
* Optimize server core tests --filter=[core] * Optimize server core tests [2] --filter=[core] * Optimize core tests [3] --filter=[core] * skip coroutine_hook.flock test --filter=[core]
1 parent d7f98cc commit 1a68380

File tree

17 files changed

+420
-117
lines changed

17 files changed

+420
-117
lines changed

core-tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5)
33
project(core_tests)
44

55
#set(CMAKE_BUILD_TYPE Released)
6-
set(CMAKE_CXX_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD 14)
77
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
88

99
file(GLOB_RECURSE SOURCE_FILES FOLLOW_SYMLINKS src/*.cpp deps/llhttp/src/*.c)

core-tests/include/test_server.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,11 @@
55

66
#define SERVER_THIS ((swoole::test::Server *) serv->private_data_2)
77

8-
#define ON_WORKERSTART_PARAMS swServer *serv, int worker_id
8+
#define ON_START_PARAMS swServer *serv
9+
#define ON_WORKER_START_PARAMS swServer *serv, swoole::Worker *worker
910
#define ON_PACKET_PARAMS swServer *serv, swRecvData *req
1011
#define ON_RECEIVE_PARAMS swServer *serv, swRecvData *req
1112

12-
typedef void (*_onStart)(swServer *serv);
13-
typedef void (*_onShutdown)(swServer *serv);
14-
typedef void (*_onPipeMessage)(swServer *, swoole::EventData *data);
15-
typedef void (*_onWorkerStart)(swServer *serv, swoole::Worker *worker);
16-
typedef void (*_onWorkerStop)(swServer *serv, swoole::Worker *worker);
17-
typedef int (*_onReceive)(swServer *, swRecvData *);
18-
typedef int (*_onPacket)(swServer *, swRecvData *);
19-
typedef void (*_onClose)(swServer *serv, swDataHead *);
20-
typedef void (*_onConnect)(swServer *serv, swDataHead *);
21-
22-
using on_workerstart_lambda_type = void (*)(ON_WORKERSTART_PARAMS);
23-
using on_receive_lambda_type = void (*)(ON_RECEIVE_PARAMS);
24-
using on_packet_lambda_type = void (*)(ON_PACKET_PARAMS);
25-
2613
namespace swoole {
2714
namespace test {
2815
//--------------------------------------------------------------------------------------------------------
@@ -36,14 +23,23 @@ class Server {
3623
int mode;
3724
int type;
3825

26+
std::string tolower(const std::string &str);
27+
3928
public:
4029
DgramPacket *packet = nullptr;
4130

4231
Server(std::string _host, int _port, swoole::Server::Mode _mode, int _type);
4332
~Server();
44-
void on(std::string event, void *fn);
33+
34+
void on(const std::string &event, const std::function<void(swServer *, Worker *)> &fn);
35+
void on(const std::string &event, const std::function<void(swServer *)> &fn);
36+
void on(const std::string &event, const std::function<void(swServer *, EventData *)> &fn);
37+
void on(const std::string &event, const std::function<int(swServer *, EventData *)> &fn);
38+
void on(const std::string &event, const std::function<int(swServer *, RecvData *)> &fn);
39+
void on(const std::string &event, const std::function<void(swServer *, DataHead *)> &fn);
40+
4541
bool start();
46-
bool listen(std::string host, int port, enum swSocketType type);
42+
bool listen(const std::string &host, int port, enum swSocketType type);
4743
int send(int session_id, const void *data, uint32_t length);
4844
ssize_t sendto(const swoole::network::Address &address, const char *__buf, size_t __n, int server_socket = -1);
4945
int close(int session_id, int reset);

core-tests/src/_lib/server.cpp

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "test_server.h"
2222
#include "swoole_memory.h"
2323

24+
#include <algorithm>
25+
#include <cctype>
26+
2427
using namespace swoole::test;
2528
using swoole::network::Address;
2629

@@ -49,33 +52,69 @@ Server::Server(std::string _host, int _port, swoole::Server::Mode _mode, int _ty
4952

5053
Server::~Server() {}
5154

52-
void Server::on(std::string event, void *fn) {
53-
if (event == "Start") {
54-
serv.onStart = (_onStart) fn;
55-
} else if (event == "onShutdown") {
56-
serv.onShutdown = (_onShutdown) fn;
57-
} else if (event == "onPipeMessage") {
58-
serv.onPipeMessage = (_onPipeMessage) fn;
59-
} else if (event == "onWorkerStart") {
60-
serv.onWorkerStart = (_onWorkerStart) fn;
61-
} else if (event == "onWorkerStop") {
62-
serv.onWorkerStop = (_onWorkerStop) fn;
63-
} else if (event == "onReceive") {
64-
serv.onReceive = (_onReceive) fn;
65-
} else if (event == "onPacket") {
66-
serv.onPacket = (_onPacket) fn;
67-
} else if (event == "onClose") {
68-
serv.onClose = (_onClose) fn;
69-
} else {
70-
serv.onConnect = (_onConnect) fn;
55+
std::string Server::tolower(const std::string &str) {
56+
std::string str_copy = str;
57+
std::transform(str_copy.begin(), str_copy.end(), str_copy.begin(), [](unsigned char c) { return std::tolower(c); });
58+
return str_copy;
59+
}
60+
61+
void Server::on(const std::string &_event, const std::function<void(swServer *, swoole::Worker *)> &fn) {
62+
auto event = tolower(_event);
63+
if (event == "workerstart") {
64+
serv.onWorkerStart = fn;
65+
} else if (event == "workerstop") {
66+
serv.onWorkerStop = fn;
67+
}
68+
}
69+
70+
void Server::on(const std::string &_event, const std::function<void(swServer *)> &fn) {
71+
auto event = tolower(_event);
72+
if (event == "start") {
73+
serv.onStart = fn;
74+
} else if (event == "shutdown") {
75+
serv.onShutdown = fn;
76+
}
77+
}
78+
79+
void Server::on(const std::string &_event, const std::function<void(swServer *, EventData *)> &fn) {
80+
auto event = tolower(_event);
81+
if (event == "pipemessage") {
82+
serv.onPipeMessage = fn;
83+
}
84+
}
85+
86+
void Server::on(const std::string &_event, const std::function<int(swServer *, EventData *)> &fn) {
87+
auto event = tolower(_event);
88+
if (event == "task") {
89+
serv.onTask = fn;
90+
} else if (event == "finish") {
91+
serv.onFinish = fn;
92+
}
93+
}
94+
95+
void Server::on(const std::string &_event, const std::function<int(swServer *, RecvData *)> &fn) {
96+
auto event = tolower(_event);
97+
if (event == "packet") {
98+
serv.onPacket = fn;
99+
} else if (event == "receive") {
100+
serv.onReceive = fn;
101+
}
102+
}
103+
104+
void Server::on(const std::string &_event, const std::function<void(swServer *, DataHead *)> &fn) {
105+
auto event = tolower(_event);
106+
if (event == "connect") {
107+
serv.onConnect = fn;
108+
} else if (event == "close") {
109+
serv.onClose = fn;
71110
}
72111
}
73112

74113
bool Server::start() {
75114
return serv.start() == 0;
76115
}
77116

78-
bool Server::listen(std::string host, int port, enum swSocketType type) {
117+
bool Server::listen(const std::string &host, int port, enum swSocketType type) {
79118
ListenPort *ls = serv.add_port(type, (char *) host.c_str(), port);
80119
if (ls == nullptr) {
81120
return false;

core-tests/src/core/base.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,25 @@ TEST(base, only_dump) {
305305
swoole_dump_hex(data.c_str(), data.length());
306306
ASSERT_TRUE(true);
307307
}
308+
309+
TEST(base, redirect_stdout) {
310+
auto file = "/tmp/swoole.log";
311+
auto out_1 = "hello world, hello swoole!\n";
312+
auto out_2 = "write to /dev/null\n";
313+
auto status = test::spawn_exec_and_wait([&]() {
314+
swoole_redirect_stdout(file);
315+
printf(out_1);
316+
fflush(stdout);
317+
318+
swoole_redirect_stdout("/dev/null");
319+
printf(out_2);
320+
fflush(stdout);
321+
});
322+
ASSERT_EQ(status, 0);
323+
324+
auto rs = swoole::file_get_contents(file);
325+
ASSERT_NE(rs, nullptr);
326+
ASSERT_TRUE(rs->contains(out_1));
327+
ASSERT_FALSE(rs->contains(out_2));
328+
unlink(file);
329+
}

core-tests/src/core/log.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,19 @@ TEST(log, rotation) {
120120
TEST(log, redirect_1) {
121121
auto status = test::spawn_exec_and_wait([]() {
122122
sw_logger()->reset();
123-
ASSERT_FALSE(sw_logger()->redirect_stdout_and_stderr(1)); // no log file opened
124-
ASSERT_FALSE(sw_logger()->redirect_stdout_and_stderr(0)); // no redirected
123+
ASSERT_FALSE(sw_logger()->redirect_stdout_and_stderr(true)); // no log file opened
124+
ASSERT_FALSE(sw_logger()->redirect_stdout_and_stderr(false)); // no redirected
125125

126126
ASSERT_TRUE(sw_logger()->open(file));
127-
ASSERT_TRUE(sw_logger()->redirect_stdout_and_stderr(1));
128-
ASSERT_FALSE(sw_logger()->redirect_stdout_and_stderr(1)); // has been redirected
127+
ASSERT_TRUE(sw_logger()->redirect_stdout_and_stderr(true));
128+
ASSERT_FALSE(sw_logger()->redirect_stdout_and_stderr(true)); // has been redirected
129129

130130
printf("hello world\n");
131131
auto content = file_get_contents(file);
132132
ASSERT_NE(content.get(), nullptr);
133133

134134
sw_logger()->close();
135-
ASSERT_TRUE(sw_logger()->redirect_stdout_and_stderr(0));
135+
ASSERT_TRUE(sw_logger()->redirect_stdout_and_stderr(false));
136136
unlink(sw_logger()->get_real_file());
137137

138138
ASSERT_TRUE(content->contains(SW_STRL("hello world\n")));

core-tests/src/coroutine/hook.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ TEST(coroutine_hook, rename) {
224224
}
225225

226226
TEST(coroutine_hook, flock) {
227+
if (is_github_ci()) {
228+
return;
229+
}
227230
long start_time = swoole::time<std::chrono::milliseconds>();
228231
coroutine::run([&](void *arg) {
229232
swoole::Coroutine::create([&](void *arg) {

core-tests/src/coroutine/socket.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using swoole::Coroutine;
2525
using swoole::HttpProxy;
2626
using swoole::Protocol;
27+
using swoole::RecvData;
2728
using swoole::Socks5Proxy;
2829
using swoole::String;
2930
using swoole::coroutine::Socket;
@@ -89,12 +90,11 @@ TEST(coroutine_socket, recv_success) {
8990
int port = swoole::test::get_random_port();
9091

9192
Process proc([port](Process *proc) {
92-
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS) {
93-
SERVER_THIS->send(req->info.fd, req->data, req->info.len);
94-
};
95-
9693
Server serv(TEST_HOST, port, swoole::Server::MODE_BASE, SW_SOCK_TCP);
97-
serv.on("onReceive", (void *) receive_fn);
94+
serv.on("Receive", [](ON_RECEIVE_PARAMS) {
95+
SERVER_THIS->send(req->info.fd, req->data, req->info.len);
96+
return 0;
97+
});
9898
serv.start();
9999
});
100100

@@ -124,10 +124,11 @@ TEST(coroutine_socket, recv_fail) {
124124
int port = swoole::test::get_random_port();
125125

126126
Process proc([port](Process *proc) {
127-
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS) { SERVER_THIS->close(req->info.fd, 0); };
128-
129127
Server serv(TEST_HOST, port, swoole::Server::MODE_BASE, SW_SOCK_TCP);
130-
serv.on("onReceive", (void *) receive_fn);
128+
serv.on("Receive", [](ON_PACKET_PARAMS) -> int {
129+
serv->close(req->info.fd, 0);
130+
return 0;
131+
});
131132
serv.start();
132133
});
133134

0 commit comments

Comments
 (0)