Skip to content

Commit c065def

Browse files
authored
Add macos unit tests, optimize code, refactor timer (#5796)
1 parent 126d97e commit c065def

File tree

110 files changed

+1180
-992
lines changed

Some content is hidden

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

110 files changed

+1180
-992
lines changed

.github/workflows/core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747

4848
- name: build
4949
run: |
50-
cmake . -D CODE_COVERAGE=ON -D enable_thread=1 -D verbose=1 || exit 1
50+
cmake . -D CODE_COVERAGE=ON -D enable_thread=1 -D verbose=1 -D enable_asan=1 || exit 1
5151
make VERBOSE=1 -j $(nproc) core-tests || exit 1
5252
5353
- name: run tests

.github/workflows/framework.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ on:
44
push:
55
pull_request:
66

7-
env:
8-
CPPFLAGS: "-I/opt/homebrew/opt/pcre2/include/"
9-
107
jobs:
118
linux:
129
runs-on: ubuntu-latest
@@ -108,7 +105,9 @@ jobs:
108105
- name: Build Swoole
109106
run: |
110107
phpize
111-
./configure CPPFLAGS="${CPPFLAGS}" --enable-openssl --enable-mysqlnd --enable-swoole-curl --enable-cares
108+
export CPPFLAGS="${CPPFLAGS} -I/opt/homebrew/opt/pcre2/include/"
109+
export CFLAGS="${CFLAGS} -I/opt/homebrew/opt/pcre2/include/"
110+
./configure --enable-openssl --enable-mysqlnd --enable-swoole-curl --enable-cares
112111
make -j$(sysctl -n hw.ncpu)
113112
sudo make install
114113
php --ri swoole

.github/workflows/unit.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,54 @@ jobs:
4343
export SWOOLE_BUILD_DIR=$(realpath .)
4444
export PHP_VERSION=${{ matrix.php }}
4545
${{runner.workspace}}/swoole-src/scripts/route.sh
46+
macos:
47+
runs-on: macos-latest
48+
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[macos-unit]')"
49+
timeout-minutes: 30
50+
strategy:
51+
fail-fast: false
52+
matrix:
53+
# php-version: [ '8.1', '8.2', '8.3', '8.4' ]
54+
php-version: [ '8.1' ]
55+
name: ${{ matrix.php-version }} - macOS
56+
steps:
57+
- name: Checkout code
58+
uses: actions/checkout@v4
59+
60+
- name: Setup PHP
61+
uses: shivammathur/setup-php@v2
62+
with:
63+
php-version: ${{ matrix.php-version }}
64+
extensions: dom, curl, libxml, mbstring, zip, redis, pdo, pdo_mysql, bcmath, sockets
65+
tools: phpize, composer:v2
66+
ini-values: extension=swoole
67+
coverage: none
68+
69+
- name: Install dependencies
70+
run: |
71+
brew install redis tinyproxy nginx md5sha1sum
72+
brew services start redis
73+
brew services start nginx
74+
brew services start tinyproxy
75+
76+
- name: Build Swoole
77+
run: |
78+
phpize
79+
export PCRE2_INCLUDE_DIR="/opt/homebrew/opt/pcre2/include"
80+
export CPPFLAGS="${CPPFLAGS} -I${PCRE2_INCLUDE_DIR}"
81+
export CFLAGS="${CFLAGS} -I${PCRE2_INCLUDE_DIR}"
82+
./configure CPPFLAGS="${CPPFLAGS}" --enable-openssl --enable-sockets --enable-mysqlnd --enable-swoole-curl --enable-cares --enable-zstd
83+
make -j$(sysctl -n hw.ncpu)
84+
sudo make install
85+
php --ri swoole
86+
uname -a
87+
88+
- name: Run unit tests
89+
run: |
90+
export SWOOLE_CI_TYPE=NORMAL
91+
export SWOOLE_BRANCH=${GITHUB_REF##*/}
92+
export SWOOLE_BUILD_DIR=$(realpath .)
93+
export PHP_VERSION=${{ matrix.php-version }}
94+
export SWOOLE_CI_IN_MACOS=1
95+
cd ${{runner.workspace}}/swoole-src
96+
./scripts/run-tests.sh

tests/.php-cs-fixer.dist.php renamed to .php-cs-fixer.dist.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
],
5050
'sort_algorithm' => 'alpha',
5151
],
52+
'global_namespace_import' => [
53+
'import_classes' => false,
54+
'import_constants' => false,
55+
'import_functions' => false,
56+
],
5257
'single_line_comment_style' => [
5358
'comment_types' => [
5459
],
@@ -81,7 +86,7 @@
8186
'phpdoc_separation' => false,
8287
'phpdoc_summary' => false,
8388
'single_quote' => true,
84-
'increment_style' => [],
89+
'increment_style' => ['style' => 'post'],
8590
'standardize_increment' => false,
8691
'standardize_not_equals' => true,
8792
'multiline_comment_opening_closing' => true,

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ target_link_libraries(core-tests swoole pthread ssl crypto ${GTEST_BOTH_LIBRARIE
229229

230230
# find libpq
231231
if (DEFINED libpq_dir)
232-
target_include_directories(ext-swoole BEFORE ${LIBPQ_INCLUDE_DIRS})
232+
include_directories(BEFORE ${libpq_dir}/include)
233+
link_directories(${libpq_dir}/lib)
233234
else()
234235
pkg_check_modules(LIBPQ REQUIRED libpq)
235236
target_include_directories(ext-swoole PRIVATE ${LIBPQ_INCLUDE_DIRS})

config.m4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ if test "$PHP_SWOOLE" != "no"; then
388388
AC_CHECK_LIB(c, signalfd, AC_DEFINE(HAVE_SIGNALFD, 1, [have signalfd]))
389389
AC_CHECK_LIB(c, eventfd, AC_DEFINE(HAVE_EVENTFD, 1, [have eventfd]))
390390
AC_CHECK_LIB(c, epoll_create, AC_DEFINE(HAVE_EPOLL, 1, [have epoll]))
391-
AC_CHECK_LIB(c, poll, AC_DEFINE(HAVE_POLL, 1, [have poll]))
392391
AC_CHECK_LIB(c, sendfile, AC_DEFINE(HAVE_SENDFILE, 1, [have sendfile]))
393392
AC_CHECK_LIB(c, kqueue, AC_DEFINE(HAVE_KQUEUE, 1, [have kqueue]))
394393
AC_CHECK_LIB(c, backtrace, AC_DEFINE(HAVE_EXECINFO, 1, [have execinfo]))

core-tests/include/httplib_server.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,8 @@ class CoSocketStream : public detail::SocketStream {
10001000
: detail::SocketStream(
10011001
sock->get_fd(), read_timeout_sec, read_timeout_usec, write_timeout_sec, write_timeout_usec) {
10021002
sock_ = sock;
1003-
sock->set_timeout((double) read_timeout_sec + ((double) read_timeout_usec / 1000000), Socket::TIMEOUT_READ);
1004-
sock->set_timeout((double) write_timeout_sec + ((double) write_timeout_usec / 1000000), Socket::TIMEOUT_WRITE);
1003+
sock->set_timeout((double) read_timeout_sec + ((double) read_timeout_usec / 1000000), SW_TIMEOUT_READ);
1004+
sock->set_timeout((double) write_timeout_sec + ((double) write_timeout_usec / 1000000), SW_TIMEOUT_WRITE);
10051005
}
10061006
~CoSocketStream() {}
10071007
bool is_readable() const {

core-tests/src/coroutine/socket.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ TEST(coroutine_socket, connect_timeout) {
5151
Socket sock(SW_SOCK_TCP);
5252

5353
sock.set_timeout(0.5);
54-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_DNS), 0.5);
55-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_CONNECT), 0.5);
56-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_READ), 0.5);
57-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_WRITE), 0.5);
54+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_DNS), 0.5);
55+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_CONNECT), 0.5);
56+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_READ), 0.5);
57+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_WRITE), 0.5);
5858

59-
sock.set_timeout(1.5, Socket::TIMEOUT_RDWR);
60-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_DNS), 0.5);
61-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_CONNECT), 0.5);
62-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_READ), 1.5);
63-
ASSERT_EQ(sock.get_timeout(Socket::TIMEOUT_WRITE), 1.5);
59+
sock.set_timeout(1.5, SW_TIMEOUT_RDWR);
60+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_DNS), 0.5);
61+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_CONNECT), 0.5);
62+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_READ), 1.5);
63+
ASSERT_EQ(sock.get_timeout(SW_TIMEOUT_WRITE), 1.5);
6464

6565
bool retval = sock.connect("192.0.0.1", 9801);
6666
ASSERT_EQ(retval, false);
@@ -84,14 +84,14 @@ TEST(coroutine_socket, timeout_controller) {
8484
});
8585

8686
Socket sock(SW_SOCK_TCP);
87-
Socket::TimeoutController tc(&sock, 0.5, Socket::TIMEOUT_ALL);
87+
Socket::TimeoutController tc(&sock, 0.5, SW_TIMEOUT_ALL);
8888
ASSERT_TRUE(sock.connect("127.0.0.1", port));
8989

9090
char buf[128];
9191
off_t offset = 0;
9292
sock.errCode = 0;
9393
while (true) {
94-
if (sw_unlikely(tc.has_timedout(Socket::TIMEOUT_READ))) {
94+
if (sw_unlikely(tc.has_timedout(SW_TIMEOUT_READ))) {
9595
break;
9696
}
9797
auto rv = sock.recv(buf + offset, sizeof(buf) - offset);
@@ -100,7 +100,7 @@ TEST(coroutine_socket, timeout_controller) {
100100
}
101101
offset += rv;
102102
}
103-
ASSERT_TRUE(tc.has_timedout(Socket::TIMEOUT_READ));
103+
ASSERT_TRUE(tc.has_timedout(SW_TIMEOUT_READ));
104104
ASSERT_EQ(sock.errCode, ETIMEDOUT);
105105
});
106106
}
@@ -121,7 +121,7 @@ TEST(coroutine_socket, timeout_setter) {
121121
});
122122

123123
Socket sock(SW_SOCK_TCP);
124-
Socket::TimeoutSetter ts(&sock, 0.5, Socket::TIMEOUT_ALL);
124+
Socket::TimeoutSetter ts(&sock, 0.5, SW_TIMEOUT_ALL);
125125
ASSERT_TRUE(sock.connect("127.0.0.1", port));
126126

127127
char buf[128];

core-tests/src/network/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ static void test_recv_timeout(Client &c) {
10371037
while (true) {
10381038
auto rv = c.recv(buf->str, buf->size);
10391039
DEBUG() << "rv: " << rv << ", error=" << errno << "\n";
1040-
if (errno == ETIMEDOUT) {
1040+
if (c.has_timedout()) {
10411041
break;
10421042
}
10431043
}

core-tests/src/network/socket.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ TEST(socket, fail) {
9191
ASSERT_FALSE(sock->cork());
9292
ASSERT_FALSE(sock->uncork());
9393

94-
ASSERT_FALSE(sock->set_recv_timeout(0.1));
95-
ASSERT_FALSE(sock->set_send_timeout(0.1));
94+
ASSERT_FALSE(sock->set_kernel_read_timeout(0.1));
95+
ASSERT_FALSE(sock->set_kernel_write_timeout(0.1));
9696

9797
sock->move_fd();
9898
sock->free();
@@ -320,7 +320,7 @@ TEST(socket, sendfile_sync) {
320320
ASSERT_EQ(cli->connect(addr), SW_OK);
321321
int len = htonl(str->get_length());
322322
cli->send(&len, sizeof(len), 0);
323-
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0, -1), SW_OK);
323+
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0), SW_OK);
324324
cli->free();
325325
});
326326

@@ -335,27 +335,27 @@ TEST(socket, sendfile) {
335335
addr.assign(SW_SOCK_TCP, TEST_HTTP_DOMAIN, 80);
336336
ASSERT_EQ(cli->connect(addr), SW_OK);
337337

338-
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0, -1), SW_ERR);
338+
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0), SW_ERR);
339339
ASSERT_EQ(errno, ENOENT);
340340

341341
File fp(file, File::WRITE | File::CREATE);
342342
ASSERT_TRUE(fp.ready());
343343

344-
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0, -1), SW_ERR);
344+
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0), SW_ERR);
345345
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_FILE_EMPTY);
346346

347347
fp.write(SW_STRL(TEST_STR));
348348
fp.close();
349349

350-
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 10, 100, -1), SW_ERR);
350+
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 10, 100), SW_ERR);
351351
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_INVALID_PARAMS);
352352

353353
ASSERT_TRUE(fp.open(file, File::WRITE | File::APPEND));
354354
auto req = test::http_get_request(TEST_HTTP_DOMAIN, "/");
355355
fp.write(req);
356356
fp.close();
357357

358-
ASSERT_EQ(cli->sendfile_sync(file.c_str(), strlen(TEST_STR), 0, -1), SW_OK);
358+
ASSERT_EQ(cli->sendfile_sync(file.c_str(), strlen(TEST_STR), 0), SW_OK);
359359

360360
char rbuf[4096];
361361
auto n = cli->recv_sync(rbuf, sizeof(rbuf), 0);

0 commit comments

Comments
 (0)