Mercurial
diff seobeo/tests/seobeo_web_server_test.c @ 121:7b1719fa918c
[Seobeo] Added web socket server.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 08 Jan 2026 06:45:10 -0800 |
| parents | |
| children | 7eb79fd91c7e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/seobeo/tests/seobeo_web_server_test.c Thu Jan 08 06:45:10 2026 -0800 @@ -0,0 +1,166 @@ +#include "seobeo/seobeo.h" + +pid_t start_test_server(const char *server_binary) +{ + pid_t server_pid = fork(); + + if (server_pid < 0) + { + perror("fork"); + return -1; + } + + if (server_pid == 0) + { + printf("Starting server on port 8000...\n"); + execlp(server_binary, NULL); + perror("execl failed"); + exit(1); + } + + printf("Server started (PID: %d)\n", server_pid); + + usleep(100000); + int status; + pid_t result = waitpid(server_pid, &status, WNOHANG); + if (result != 0) + { + if (WIFEXITED(status)) + { + fprintf(stderr, "Server exited immediately with code: %d\n", WEXITSTATUS(status)); + } + else if (WIFSIGNALED(status)) + { + fprintf(stderr, "Server was killed by signal: %d\n", WTERMSIG(status)); + } + return -1; + } + + sleep(2); + printf("Server ready\n\n"); + + return server_pid; +} + +void Test_Echo() +{ + printf("\n=== Test: Multiple Messages ===\n"); + + Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/echo"); + if (!p_ws) + { + printf("Failed to connect\n"); + return; + } + + const char *messages[] = { + "Message 1", + "Message 2", + "Message 3" + }; + + for (int i = 0; i < 3; i++) + { + printf("Sending: %s\n", messages[i]); + Seobeo_WebSocket_Send_Text(p_ws, messages[i]); + usleep(100000); + } + + printf("Receiving responses...\n"); + int received = 0; + int attempts = 0; + + while (received < 3 && attempts < 200) + { + Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); + if (p_msg) + { + if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) + { + printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); + received++; + } + Seobeo_WebSocket_Message_Destroy(p_msg); + } + + usleep(10000); + attempts++; + } + printf("Received %d/%d messages\n", received, 3); + Seobeo_WebSocket_Destroy(p_ws); +} + +void Test_Chat() +{ + printf("\n=== Test: Multiple Messages ===\n"); + + Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat"); + if (!p_ws) + { + printf("Failed to connect\n"); + return; + } + + const char *messages[] = { + "Message 1", + "Message 2", + "Message 3" + }; + + for (int i = 0; i < 3; i++) + { + printf("Sending: %s\n", messages[i]); + Seobeo_WebSocket_Send_Text(p_ws, messages[i]); + usleep(100000); + } + + printf("Receiving responses...\n"); + int received = 0; + int attempts = 0; + + while (received < 3 && attempts < 200) + { + Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws); + if (p_msg) + { + if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) + { + printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data); + received++; + } + Seobeo_WebSocket_Message_Destroy(p_msg); + } + + usleep(10000); + attempts++; + } + printf("Received %d/%d messages\n", received, 3); + Seobeo_WebSocket_Destroy(p_ws); +} + +void stop_test_server(pid_t server_pid) +{ + if (server_pid > 0) + { + printf("\nStopping server (PID: %d)...\n", server_pid); + kill(server_pid, SIGTERM); + waitpid(server_pid, NULL, 0); + printf("Server stopped\n"); + } +} + +int main(int argc, char *argv[]) +{ + const char *server_binary = "./websocket_server_example"; + if (argc > 1) + { + server_binary = argv[1]; + } + + pid_t server_pid = start_test_server(server_binary); + + Test_Echo(); + Test_Chat(); + + stop_test_server(server_pid); +}