Skip to content

Commit 07289e3

Browse files
authored
Optimize code, refactor the underlying layer, and increase test coverage to 80% (#5769)
* Optimize code, refactor the underlying layer, and increase test coverage to 78% * code format * Fix tests --filter=[unit][core][thread] --verbose * Refactor socket address impl --filter=[unit][core][thread] --verbose * Fix tests[1] --filter=[unit][core][thread] --verbose * Fix tests[2] --filter=[core] --verbose * Fix tests[3] --filter=[core] --verbose * Fix tests[4] --filter=[core] --verbose * Fix tests[5] --filter=[core] --verbose * Fix tests[6] --filter=[core] --verbose * Fix tests[7] --filter=[core] --verbose * Fix tests[8] --filter=[core][unit][thread] --verbose * Fix tests[9] --filter=[core][unit][thread] --verbose * Fix tests[9] --filter=[core][unit][thread] --verbose * fix tests * Fix tests [10] * Fix tests [10] --filter=[unit] * Added ssl lock tests * Added SW_SOCK_RAW6, Fix SW_SOCK_RAW * Fix tests [11] --filter=[unit][core] * fix php tests * Fix tests [12] --filter=[unit][core] * Refactor, Added more core tests [13] * Refactor CoSocket::sendto(), Fix tests --filter=[core] * Fix tests --filter=[core] * Fix tests [12] --filter=[core] * Fix tests [13] --filter=[core] * Fix tests [14] --filter=[core] * fix tests * fix core tests
1 parent 4f46e62 commit 07289e3

File tree

114 files changed

+4443
-2145
lines changed

Some content is hidden

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

114 files changed

+4443
-2145
lines changed

.github/workflows/core.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ jobs:
5656
if [[ "$GITHUB_COMMIT_MESSAGE" == *"--strace"* ]]; then
5757
export SWOOLE_ENABLE_STRACE=1
5858
fi
59+
if [[ "$GITHUB_COMMIT_MESSAGE" == *"--verbose"* ]]; then
60+
export SWOOLE_ENABLE_VERBOSE=1
61+
fi
5962
echo "SWOOLE_VALGRIND=${SWOOLE_VALGRIND}"
6063
echo "SWOOLE_ENABLE_ASAN=${SWOOLE_ENABLE_ASAN}"
6164
echo "SWOOLE_ENABLE_STRACE=${SWOOLE_ENABLE_STRACE}"
65+
echo "SWOOLE_ENABLE_VERBOSE=${SWOOLE_ENABLE_VERBOSE}"
6266
cd core-tests && ./run.sh
6367
6468
- name: run coverage

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,6 @@ cmake-build-debug/
118118
/tests/include/lib/vendor/
119119
/tests/include/lib/composer.lock
120120
/scripts/data
121+
/.cmake/
122+
/Testing/
123+
build.ninja

CMakeLists.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
PROJECT(libswoole)
2-
cmake_minimum_required(VERSION 3.5)
1+
cmake_minimum_required(VERSION 3.10)
2+
project(libswoole)
33

4-
ENABLE_LANGUAGE(ASM)
4+
enable_language(ASM)
55
set(SWOOLE_VERSION 6.0.3-dev)
66

77
set(CMAKE_CXX_STANDARD 11)
@@ -115,8 +115,22 @@ if (DEFINED enable_asan)
115115
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
116116
endif()
117117

118-
execute_process(COMMAND php-config --includes OUTPUT_VARIABLE PHP_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE)
119-
execute_process(COMMAND php-config --extension-dir OUTPUT_VARIABLE PHP_EXTENSION_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
118+
if (DEFINED php_dir)
119+
set(PHP_CONFIG "${php_dir}/bin/php-config")
120+
else ()
121+
set(PHP_CONFIG "php-config")
122+
endif()
123+
124+
execute_process(COMMAND ${PHP_CONFIG} --includes OUTPUT_VARIABLE PHP_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE PHP_CONFIG_RESULT)
125+
if (NOT PHP_CONFIG_RESULT EQUAL 0)
126+
message(FATAL_ERROR "Failed to execute php-config: ${PHP_CONFIG_RESULT}")
127+
endif()
128+
129+
execute_process(COMMAND ${PHP_CONFIG} --extension-dir OUTPUT_VARIABLE PHP_EXTENSION_DIR OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE PHP_CONFIG_RESULT)
130+
if (NOT PHP_CONFIG_RESULT EQUAL 0)
131+
message(FATAL_ERROR "Failed to execute php-config: ${PHP_CONFIG_RESULT}")
132+
endif()
133+
120134
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PHP_INCLUDES}")
121135
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PHP_INCLUDES}")
122136

core-tests/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
.cmake/
3+
Testing/
4+
build.ninja

core-tests/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ else()
5252
endif()
5353
endif()
5454

55-
if (DEFINED brotli_dir)
56-
list(APPEND core_tests_includes ${brotli_dir}/include)
57-
list(APPEND core_tests_link_directories ${brotli_dir}/lib)
58-
endif()
59-
6055
if (DEFINED enable_asan)
6156
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
6257
endif()
@@ -65,6 +60,10 @@ if (DEFINED enable_thread)
6560
add_definitions(-DSW_THREAD)
6661
endif()
6762

63+
if (DEFINED verbose)
64+
add_definitions(-DSW_VERBOSE)
65+
endif()
66+
6867
# should execute before the add_executable command
6968
link_directories(${core_tests_link_directories})
7069

core-tests/include/httplib_server.h

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -517,30 +517,31 @@ inline bool Server::write_content_with_provider(
517517

518518
inline bool Server::read_content(Stream &strm, Request &req, Response &res) {
519519
MultipartFormDataMap::iterator cur;
520-
if (read_content_core(strm,
521-
req,
522-
res,
523-
// Regular
524-
[&](const char *buf, size_t n) {
525-
if (req.body.size() + n > req.body.max_size()) {
526-
return false;
527-
}
528-
req.body.append(buf, n);
529-
return true;
530-
},
531-
// Multipart
532-
[&](const MultipartFormData &file) {
533-
cur = req.files.emplace(file.name, file);
534-
return true;
535-
},
536-
[&](const char *buf, size_t n) {
537-
auto &content = cur->second.content;
538-
if (content.size() + n > content.max_size()) {
539-
return false;
540-
}
541-
content.append(buf, n);
542-
return true;
543-
})) {
520+
if (read_content_core(
521+
strm,
522+
req,
523+
res,
524+
// Regular
525+
[&](const char *buf, size_t n) {
526+
if (req.body.size() + n > req.body.max_size()) {
527+
return false;
528+
}
529+
req.body.append(buf, n);
530+
return true;
531+
},
532+
// Multipart
533+
[&](const MultipartFormData &file) {
534+
cur = req.files.emplace(file.name, file);
535+
return true;
536+
},
537+
[&](const char *buf, size_t n) {
538+
auto &content = cur->second.content;
539+
if (content.size() + n > content.max_size()) {
540+
return false;
541+
}
542+
content.append(buf, n);
543+
return true;
544+
})) {
544545
const auto &content_type = req.get_header_value("Content-Type");
545546
if (!content_type.find("application/x-www-form-urlencoded")) {
546547
detail::parse_query_text(req.body, req.params);
@@ -1018,7 +1019,7 @@ class CoSocketStream : public detail::SocketStream {
10181019
void get_remote_ip_and_port(std::string &ip, int &port) const {
10191020
Address sa;
10201021
sock_->getpeername(&sa);
1021-
ip = std::string(sa.get_ip());
1022+
ip = std::string(sa.get_addr());
10221023
port = sa.get_port();
10231024
}
10241025

core-tests/include/redis_client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class RedisClient {
6363
std::string Get(const std::string &key);
6464
bool Set(const std::string &key, const std::string &value);
6565
long Ttl(const std::string &key);
66+
bool Select(int db);
67+
std::string Role();
6668
};
6769

6870
} // namespace swoole

core-tests/include/test_core.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,34 @@
5353

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)
56+
#define ASSERT_ERREQ(x) ASSERT_EQ(swoole_get_last_error(), x)
57+
#define EXPECT_ERREQ(x) EXPECT_EQ(swoole_get_last_error(), x)
5658

5759
#define TIMER_PARAMS Timer *timer, TimerNode *tnode
5860

61+
#ifdef SW_VERBOSE
62+
#define DEBUG() std::cout
63+
#else
64+
#define DEBUG() swoole::test::null_stream
65+
#endif
66+
5967
namespace swoole {
6068
struct HttpProxy;
6169
struct Socks5Proxy;
6270
namespace test {
63-
71+
class NullStream {
72+
public:
73+
template <typename T>
74+
NullStream &operator<<(const T &) {
75+
return *this;
76+
}
77+
78+
NullStream &operator<<(std::ostream &(*) (std::ostream &) ) {
79+
return *this;
80+
}
81+
};
82+
83+
extern NullStream null_stream;
6484
const std::string &get_root_path();
6585
std::string get_ssl_dir();
6686
std::string get_jpg_file();
@@ -69,11 +89,14 @@ int exec_js_script(const std::string &file, const std::string &args);
6989
std::string http_get_request(const std::string &domain, const std::string &path);
7090
int get_random_port();
7191

72-
Socks5Proxy *create_socks5_proxy();
73-
HttpProxy *create_http_proxy();
74-
7592
pid_t spawn_exec(const std::function<void(void)> &fn);
7693
int spawn_exec_and_wait(const std::function<void(void)> &fn);
7794

95+
int dump_cert_info(const char *data, size_t len);
96+
97+
static inline int dump_cert_info(const String *str) {
98+
return dump_cert_info(str->str, str->length);
99+
}
100+
78101
} // namespace test
79102
}; // namespace swoole

core-tests/run.sh

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
#!/bin/bash
2-
__DIR__=$(cd "$(dirname "$0")";pwd)
3-
__SWOOLE_DIR__=$(cd "$(dirname "${__DIR__}")";pwd)
2+
__DIR__=$(cd "$(dirname "$0")" || exit;pwd)
3+
__SWOOLE_DIR__=$(cd "$(dirname "${__DIR__}")" || exit;pwd)
4+
CMAKE_ARGS="-D swoole_dir=${__SWOOLE_DIR__} -D enable_thread=1"
45

56
if [ "${SWOOLE_ENABLE_ASAN}" = 1 ]; then
6-
cmake . -D swoole_dir="${__SWOOLE_DIR__}" -D enable_thread=1 -D enable_asan=1
7-
else
8-
cmake . -D swoole_dir="${__SWOOLE_DIR__}" -D enable_thread=1
7+
CMAKE_ARGS="${CMAKE_ARGS} -D enable_asan=1"
98
fi
10-
make -j8
9+
10+
if [ "${SWOOLE_ENABLE_VERBOSE}" = 1 ]; then
11+
CMAKE_ARGS="${CMAKE_ARGS} -D verbose=1"
12+
fi
13+
14+
cmake . ${CMAKE_ARGS} || exit 1
15+
make VERBOSE=1 -j8 || exit 1
1116
ipcs -q
1217

13-
cd ${__DIR__}/js
18+
cd "${__DIR__}"/js || exit 1
1419
npm install
15-
cd ${__DIR__}
20+
cd "${__DIR__}" || exit 1
1621

17-
tasks=$(./bin/core_tests --gtest_list_tests | awk '/\./')
22+
tasks=$(./bin/core_tests --gtest_list_tests | awk '/\./') || exit 255
1823
for task in $tasks; do
1924

2025
if [ "${SWOOLE_VALGRIND}" = 1 ]; then
@@ -28,10 +33,10 @@ for task in $tasks; do
2833

2934
echo "run tests for $task"
3035

31-
if [ $task = "log." ]; then
32-
$execute_command --gtest_filter=$task*
36+
if [ "$task" = "log." ]; then
37+
$execute_command --gtest_filter="$task"*
3338
else
34-
sudo -E $execute_command --gtest_filter=$task*
39+
sudo -E "$execute_command" --gtest_filter="$task"*
3540
fi
3641

3742
if [ $? -ne 0 ] && [ "${GITHUB_ACTIONS}" = true ]; then

core-tests/src/_lib/redis.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ long RedisClient::Ttl(const std::string &key) {
4747
}
4848
}
4949

50+
bool RedisClient::Select(int db) {
51+
auto _db = std::to_string(db);
52+
const char *argv[] = {"SELECT", _db.c_str()};
53+
size_t argvlen[] = {strlen(argv[0]), _db.length()};
54+
55+
auto reply = Request(SW_ARRAY_SIZE(argv), argv, argvlen);
56+
if (!reply.empty() && reply->type == REDIS_REPLY_STATUS && strncmp(reply->str, "OK", 2) == 0) {
57+
return true;
58+
} else {
59+
return false;
60+
}
61+
}
62+
63+
std::string RedisClient::Role() {
64+
const char *argv[] = {"ROLE"};
65+
size_t argvlen[] = {strlen(argv[0])};
66+
67+
auto reply = Request(SW_ARRAY_SIZE(argv), argv, argvlen);
68+
if (!reply.empty() && reply->str) {
69+
return string(reply->str, reply->len);
70+
} else {
71+
return "";
72+
}
73+
}
74+
5075
bool RedisClient::Set(const string &key, const string &value) {
5176
const char *argv[] = {"SET", key.c_str(), value.c_str()};
5277
size_t argvlen[] = {strlen(argv[0]), key.length(), value.length()};

0 commit comments

Comments
 (0)