Skip to content

Commit

Permalink
Add core tests (swoole#5633)
Browse files Browse the repository at this point in the history
* add core tests, --filter=[core]

* fix --filter=[core]

* fix2 --filter=[core]

* fix3 --filter=[core]

* fix 4 --filter=[core]

* fix 5 --filter=[core]
  • Loading branch information
matyhtf authored Dec 23, 2024
1 parent 88a7b74 commit 25837a8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ jobs:
build:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '--filter=') || contains(github.event.head_commit.message, '[core]')"
timeout-minutes: 10
services:
tinyproxy:
image: "vimagick/tinyproxy"
ports:
- 8888:8888
nginx:
image: "nginx"
ports:
- "80:80"
env:
NGINX_PORT: "[::]:80"
socks5:
image: "xkuma/socks5"
ports:
Expand Down
21 changes: 19 additions & 2 deletions core-tests/src/coroutine/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ using swoole::coroutine::Socket;
using swoole::coroutine::System;
using swoole::network::Address;
using swoole::network::IOVector;
using swoole::test::Server;
using swoole::test::create_http_proxy;
using swoole::test::create_socks5_proxy;
using swoole::test::Server;

const std::string host = "www.baidu.com";

Expand Down Expand Up @@ -66,6 +66,24 @@ TEST(coroutine_socket, connect_with_dns) {
});
}

TEST(coroutine_socket, tcp6) {
coroutine::run([](void *arg) {
Socket sock(SW_SOCK_TCP6);
bool retval = sock.connect("::1", 80);
ASSERT_EQ(retval, true);
ASSERT_EQ(sock.errCode, 0);
});
}

TEST(coroutine_socket, unixsock_fail) {
coroutine::run([](void *arg) {
Socket sock(SW_SOCK_UNIX_STREAM);
bool retval = sock.connect("/tmp/unix.sock");
ASSERT_EQ(retval, false);
ASSERT_EQ(sock.errCode, ENOENT);
});
}

TEST(coroutine_socket, recv_success) {
pid_t pid;
int port = swoole::test::get_random_port();
Expand Down Expand Up @@ -1028,7 +1046,6 @@ TEST(coroutine_socket, https_get_with_http_proxy) {
});
}


#ifdef SW_USE_OPENSSL
TEST(coroutine_socket, ssl) {
coroutine::run([&](void *arg) {
Expand Down
56 changes: 56 additions & 0 deletions core-tests/src/os/process_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define sysv_signal signal
#endif

#include "swoole_signal.h"

using namespace swoole;

static void test_func(ProcessPool &pool) {
Expand Down Expand Up @@ -129,6 +131,7 @@ TEST(process_pool, stream_protocol) {

constexpr int magic_number = 99900011;
static ProcessPool *current_pool = nullptr;
static Worker *current_worker = nullptr;

TEST(process_pool, shutdown) {
ProcessPool pool{};
Expand Down Expand Up @@ -172,3 +175,56 @@ TEST(process_pool, shutdown) {

ASSERT_EQ(*shm_value, magic_number);
}

TEST(process_pool, async) {
ProcessPool pool{};
ASSERT_EQ(pool.create(1, 0, SW_IPC_UNIXSOCK), SW_OK);

// init
pool.set_max_packet_size(8192);
pool.set_protocol(SW_PROTOCOL_TASK);
int *shm_value = (int *) sw_mem_pool()->alloc(sizeof(int));
pool.ptr = shm_value;
pool.async = true;

pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
int *shm_value = (int *) pool->ptr;
*shm_value = magic_number;
current_worker = worker;

swoole_signal_set(SIGTERM, [](int sig) {
int *shm_value = (int *) current_pool->ptr;
(*shm_value)++;
current_pool->stop(current_worker);
});

usleep(10);
};

pool.onMessage = [](ProcessPool *pool, RecvData *msg) {
int *shm_value = (int *) pool->ptr;
(*shm_value)++;
kill(pool->master_pid, SIGTERM);
};

current_pool = &pool;
sysv_signal(SIGTERM, [](int sig) { current_pool->running = false; });

// start
ASSERT_EQ(pool.start(), SW_OK);

EventData msg{};
msg.info.len = 128;
swoole_random_string(msg.data, msg.info.len);
int worker_id = -1;
pool.dispatch_blocking(&msg, &worker_id);

// wait
ASSERT_EQ(pool.wait(), SW_OK);

// shutdown
pool.shutdown();
pool.destroy();

ASSERT_EQ(*shm_value, magic_number + 2);
}
2 changes: 1 addition & 1 deletion include/swoole_coroutine_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Socket {
Socket(SocketType type = SW_SOCK_TCP);
Socket(int _fd, SocketType _type);
~Socket();
bool connect(std::string host, int port, int flags = 0);
bool connect(std::string host, int port = 0, int flags = 0);
bool connect(const struct sockaddr *addr, socklen_t addrlen);
bool shutdown(int how = SHUT_RDWR);
bool cancel(const EventType event);
Expand Down

0 comments on commit 25837a8

Please sign in to comment.