58a9d2764ba6a7757fa1395e7e3abd59b45e0ffe
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Startup, and shutdown the memcached servers.
16 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
26 #include <libmemcached/memcached.h>
27 #include <libmemcached/util.h>
31 void server_startup(server_startup_st
*construct
)
33 if ((construct
->server_list
= getenv("MEMCACHED_SERVERS")))
35 printf("servers %s\n", construct
->server_list
);
36 construct
->servers
= memcached_servers_parse(construct
->server_list
);
37 construct
->server_list
= NULL
;
43 char server_string_buffer
[8096];
45 end_ptr
= server_string_buffer
;
47 for (uint32_t x
= 0; x
< construct
->count
; x
++)
49 char buffer
[1024]; /* Nothing special for number */
53 sprintf(buffer
, "/tmp/%umemc.pid", x
);
54 if (access(buffer
, F_OK
) == 0)
56 FILE *fp
= fopen(buffer
, "r");
61 if (fgets(buffer
, sizeof(buffer
), fp
) != NULL
)
63 pid_t pid
= (pid_t
)atoi(buffer
);
74 sprintf(buffer
, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128",
75 MEMCACHED_BINARY
, x
, x
+ TEST_PORT_BASE
, x
+ TEST_PORT_BASE
);
79 sprintf(buffer
, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u",
80 MEMCACHED_BINARY
, x
, x
+ TEST_PORT_BASE
, x
+ TEST_PORT_BASE
);
82 if (libmemcached_util_ping("localhost", (in_port_t
)(x
+ TEST_PORT_BASE
), NULL
))
84 fprintf(stderr
, "Server on port %u already exists\n", x
+ TEST_PORT_BASE
);
88 status
= system(buffer
);
89 fprintf(stderr
, "STARTING SERVER: %s status:%d\n", buffer
, status
);
91 count
= sprintf(end_ptr
, "localhost:%u,", x
+ TEST_PORT_BASE
);
96 construct
->server_list
= strdup(server_string_buffer
);
98 printf("servers %s\n", construct
->server_list
);
99 construct
->servers
= memcached_servers_parse(construct
->server_list
);
102 assert(construct
->servers
);
104 srandom((unsigned int)time(NULL
));
106 for (uint32_t x
= 0; x
< memcached_server_list_count(construct
->servers
); x
++)
108 printf("\t%s : %d\n", memcached_server_name(&construct
->servers
[x
]), memcached_server_port(&construct
->servers
[x
]));
109 assert(construct
->servers
[x
].fd
== -1);
110 assert(construct
->servers
[x
].cursor_active
== 0);
116 void server_shutdown(server_startup_st
*construct
)
118 if (construct
->server_list
)
120 for (uint32_t x
= 0; x
< construct
->count
; x
++)
122 char buffer
[1024]; /* Nothing special for number */
123 sprintf(buffer
, "cat /tmp/%umemc.pid | xargs kill", x
);
124 /* We have to check the return value of this or the compiler will yell */
125 int sys_ret
= system(buffer
);
126 assert(sys_ret
!= -1);
127 sprintf(buffer
, "/tmp/%umemc.pid", x
);
131 free(construct
->server_list
);