Skip to content

Commit

Permalink
Optimize thread server (#5448)
Browse files Browse the repository at this point in the history
* fix msgbus ts

* optimize

* fix user process with thread mode

* fix

* fix tests

* optimize naming

* fix warning

* fix test, optimize code

* fix

* fix tests, add core test for thread server

* fix tests [3]

* [test][zts] fix tests [4]

* [test][zts] fix tests [5]

* fix tests[6]

* fix tests[7]

* [test][framework][lib] fix tests

* [workflow-filter][framework][core] Refactor github workflows

* Update workflow --filter=[framework][core] --valgrind

* Update workflow --filter=[core] --valgrind

* fix core tests, --filter=[core] --valgrind

* fix core tests, --filter=[core]

* fix core tests, --filter=[core]
  • Loading branch information
matyhtf committed Aug 19, 2024
1 parent b954984 commit 5ab6214
Show file tree
Hide file tree
Showing 33 changed files with 483 additions and 199 deletions.
14 changes: 14 additions & 0 deletions .github/WORKFLOW-PARAMETERS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Workflow Parameters
Adding parameters in the Git commit log can control the workflow.

## --filter
This parameter specifies which workflows to run, instead of running all of them.
The command format is: `--filter=[flow1][flow2][flow...]` ,
and it supports setting multiple workflows, with names matching the filename of the yml file.

## --valgrind
Setting this parameter will cause the test program to be run with `valgrind`.

```shell
git commit -m "commit message --filter=[core][unit] --valgrind"
```
12 changes: 9 additions & 3 deletions .github/workflows/lib.yml → .github/workflows/core.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: lib-swoole
name: Core Tests

on: [ push, pull_request ]

Expand All @@ -8,7 +8,7 @@ env:
jobs:
build:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[core]')"
services:
tinyproxy:
image: "vimagick/tinyproxy"
Expand All @@ -33,10 +33,16 @@ jobs:
run: phpize && ./configure --enable-sockets --enable-mysqlnd --enable-openssl

- name: make
run: cmake . -DCODE_COVERAGE=ON &&
run: cmake . -DCODE_COVERAGE=ON -DSW_THREAD=1 &&
make VERBOSE=1 -j &&
sudo make install

- name: make test with valgrind
if: "contains(github.event.head_commit.message, '--valgrind')"
run: |
sudo apt install -y valgrind
cd core-tests && SWOOLE_VALGRIND=1 ./run.sh
- name: make test
run:
cd core-tests && ./run.sh
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ext.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ext-swoole
name: Compile Tests

on: [ push, pull_request ]

Expand All @@ -7,7 +7,7 @@ env:

jobs:
build-ubuntu-latest:
if: "!contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[ext]')"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
make clean && make -j$(nproc)

build-macos-latest:
if: "!contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=')"
runs-on: macos-latest
steps:
- name: install dependencies
Expand Down Expand Up @@ -70,7 +70,7 @@ jobs:
make clean && make -j$(sysctl -n hw.ncpu)

build-alpine-latest:
if: "!contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=')"
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/framework.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Frameworks Tests
name: Framework Tests

on:
push:
Expand All @@ -10,7 +10,7 @@ env:
jobs:
linux:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[framework]')"
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -43,15 +43,15 @@ jobs:
php --ri swoole
- name: Laravel Octane Tests
if: matrix.framework == 'Laravel Octane' && matrix.php-version != '8.0'
if: matrix.framework == 'Laravel Octane' && matrix.php-version != '8.3'
run: |
git clone https://github.com/laravel/octane.git --depth=1
cd octane/
composer update --prefer-dist --no-interaction --no-progress
vendor/bin/phpunit
- name: Hyperf Tests
if: matrix.framework == 'Hyperf' && matrix.php-version != '8.0'
if: matrix.framework == 'Hyperf' && matrix.php-version != '8.3'
env:
SW_VERSION: 'master'
MYSQL_VERSION: '5.7'
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
composer test
macos:
if: "!contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[framework]')"
runs-on: macos-latest
strategy:
fail-fast: false
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/iouring.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Async IO With IOUring
name: Linux io_uring Tests

on: [push, pull_request]

jobs:
test-linux:
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[iouring]')"
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/thread.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [push, pull_request]
jobs:
test-linux:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[nts]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[thread]')"
strategy:
fail-fast: false
matrix:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: test-linux
name: Unit Tests

on: [push, pull_request]

jobs:
test-linux:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[zts]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[unit]')"
strategy:
fail-fast: false
matrix:
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Run pecl-install.sh
run: |
sudo ${{runner.workspace}}/swoole-src/scripts/pecl-install.sh
- name: Run Swoole test
- name: Run unit tests
run: |
export SWOOLE_BRANCH=${GITHUB_REF##*/}
export SWOOLE_BUILD_DIR=$(realpath .)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: conflict-exts
name: Xdebug Support Tests

on: [push, pull_request]

jobs:
tests:
if: "github.repository_owner == 'swoole' && !contains(github.event.head_commit.message, '[test]')"
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[xdebug]')"
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand Down
12 changes: 10 additions & 2 deletions core-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ ipcs -q

tasks=$(./bin/core_tests --gtest_list_tests | awk '/\./')
for task in $tasks; do

if [ "${SWOOLE_VALGRIND}" = 1 ]; then
# --leak-check=full --show-leak-kinds=all --track-origins=yes
execute_command="valgrind ./bin/core_tests"
else
execute_command="./bin/core_tests"
fi

if [ $task = "log." ]; then
./bin/core_tests --gtest_filter=$task*
$execute_command --gtest_filter=$task*
else
sudo ./bin/core_tests --gtest_filter=$task*
sudo $execute_command --gtest_filter=$task*
fi

if [ $? -ne 0 ] && [ "${GITHUB_ACTIONS}" = true ]; then
Expand Down
4 changes: 2 additions & 2 deletions core-tests/src/core/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ TEST(string, ends_with) {

TEST(string, append_number) {
string data = "hello";
auto str = swoole::make_string(data.length());
auto str = swoole::make_string(data.length() + 32);
str->append(data.c_str(), data.length());
str->append(123);
str->str[str->length] = '\0';
str->set_null_terminated();
EXPECT_STREQ(str->str, data.append("123").c_str());

str->print(true);
Expand Down
46 changes: 45 additions & 1 deletion core-tests/src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,50 @@ TEST(server, process) {
delete lock;
}

TEST(server, thread) {
Server serv(Server::MODE_THREAD);
serv.worker_num = 2;

sw_logger()->set_level(SW_LOG_WARNING);

ListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
ASSERT_TRUE(port);

mutex lock;
lock.lock();

ASSERT_EQ(serv.create(), SW_OK);

std::thread t1([&]() {
swoole_signal_block_all();

lock.lock();

network::SyncClient c(SW_SOCK_TCP);
c.connect(TEST_HOST, port->port);
c.send(packet, strlen(packet));
char buf[1024];
c.recv(buf, sizeof(buf));
c.close();

serv.shutdown();
});

serv.onWorkerStart = [&lock](Server *serv, Worker *worker) { lock.unlock(); };

serv.onReceive = [](Server *serv, RecvData *req) -> int {
EXPECT_EQ(string(req->data, req->info.len), string(packet));

string resp = string("Server: ") + string(packet);
serv->send(req->info.fd, resp.c_str(), resp.length());

return SW_OK;
};

serv.start();
t1.join();
}

TEST(server, reload_all_workers) {
Server serv(Server::MODE_PROCESS);
serv.worker_num = 2;
Expand Down Expand Up @@ -1035,7 +1079,7 @@ TEST(server, reopen_log) {
return;
}
EXPECT_TRUE(access(filename.c_str(), R_OK) != -1);
remove(filename.c_str());
unlink(filename.c_str());
EXPECT_TRUE(access(filename.c_str(), R_OK) == -1);
kill(serv->gs->master_pid, SIGRTMIN);
sleep(2);
Expand Down
Loading

0 comments on commit 5ab6214

Please sign in to comment.