Skip to content

Commit

Permalink
fix tests, add core test for thread server
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 15, 2024
1 parent fa327bc commit 4352cea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
44 changes: 44 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion src/protocol/message_bus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ size_t MessageBus::get_memory_size() {

void MessageBus::init_pipe_socket(network::Socket *sock) {
int pipe_fd = sock->get_fd();
if (pipe_fd >= pipe_sockets_.size()) {
if ((size_t) pipe_fd >= pipe_sockets_.size()) {
pipe_sockets_.resize(pipe_fd + 1);
}
auto _socket = make_socket(pipe_fd, SW_FD_PIPE);
Expand Down
1 change: 1 addition & 0 deletions src/server/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Factory *Server::create_thread_factory() {
}
reactor_threads = new ReactorThread[reactor_num]();
reactor_pipe_num = 1;
worker_thread_start = [](const WorkerFn &fn) { fn(); };
return new ThreadFactory(this);
}

Expand Down

0 comments on commit 4352cea

Please sign in to comment.