|
129
|
1 #include "seobeo/seobeo.h"
|
|
|
2 #include "seobeo/snapshot_creator.h"
|
|
|
3 #include <stdio.h>
|
|
|
4 #include <stdlib.h>
|
|
|
5 #include <unistd.h>
|
|
|
6 #include <sys/wait.h>
|
|
|
7 #include <signal.h>
|
|
|
8
|
|
|
9 #define TEST_PORT "6969"
|
|
|
10 #define TEST_HOST "127.0.0.1"
|
|
|
11 #define TEST_URL "http://127.0.0.1:6969"
|
|
|
12 #define MAX_RESPONSE_SIZE (1024 * 1024)
|
|
|
13 #ifndef SNAPSHOT_DIR
|
|
|
14 #define SNAPSHOT_DIR "mrjunejune/test/snapshots"
|
|
|
15 #endif
|
|
|
16
|
|
|
17 pid_t start_test_server(const char *server_binary);
|
|
|
18 void stop_test_server(pid_t server_pid);
|
|
|
19
|
|
|
20
|
|
|
21 pid_t start_test_server(const char *server_binary)
|
|
|
22 {
|
|
|
23 pid_t server_pid = fork();
|
|
|
24
|
|
|
25 if (server_pid < 0)
|
|
|
26 {
|
|
|
27 perror("fork");
|
|
|
28 return -1;
|
|
|
29 }
|
|
|
30
|
|
|
31 if (server_pid == 0)
|
|
|
32 {
|
|
|
33 printf("Starting server on port %s...\n", TEST_PORT);
|
|
|
34 execl(server_binary, server_binary, NULL);
|
|
|
35 perror("execl failed");
|
|
|
36 exit(1);
|
|
|
37 }
|
|
|
38
|
|
|
39 printf("Server started (PID: %d)\n", server_pid);
|
|
|
40
|
|
|
41 usleep(100000);
|
|
|
42 int status;
|
|
|
43 pid_t result = waitpid(server_pid, &status, WNOHANG);
|
|
|
44 if (result != 0)
|
|
|
45 {
|
|
|
46 if (WIFEXITED(status))
|
|
|
47 {
|
|
|
48 fprintf(stderr, "Server exited immediately with code: %d\n", WEXITSTATUS(status));
|
|
|
49 }
|
|
|
50 else if (WIFSIGNALED(status))
|
|
|
51 {
|
|
|
52 fprintf(stderr, "Server was killed by signal: %d\n", WTERMSIG(status));
|
|
|
53 }
|
|
|
54 return -1;
|
|
|
55 }
|
|
|
56
|
|
|
57 sleep(2);
|
|
|
58 printf("Server ready\n\n");
|
|
|
59
|
|
|
60 return server_pid;
|
|
|
61 }
|
|
|
62
|
|
|
63 void stop_test_server(pid_t server_pid)
|
|
|
64 {
|
|
|
65 if (server_pid > 0)
|
|
|
66 {
|
|
|
67 printf("\nStopping server (PID: %d)...\n", server_pid);
|
|
|
68 kill(server_pid, SIGTERM);
|
|
|
69 waitpid(server_pid, NULL, 0);
|
|
|
70 printf("Server stopped\n");
|
|
|
71 }
|
|
|
72 }
|
|
|
73
|
|
|
74
|