We now sort servers to make sure the client applications will always have the
[m6w6/libmemcached] / tests / server.c
1 /*
2 Startup, and shutdown the memcached servers.
3 */
4
5 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 #include <assert.h>
11 #include <memcached.h>
12 #include <unistd.h>
13 #include "server.h"
14
15 void server_startup(server_startup_st *construct)
16 {
17 unsigned int x;
18
19 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
20 {
21 printf("servers %s\n", construct->server_list);
22 construct->servers= memcached_servers_parse(construct->server_list);
23 construct->server_list= NULL;
24 construct->count= 0;
25 }
26 else
27 {
28 {
29 char server_string_buffer[8096];
30 char *end_ptr;
31 end_ptr= server_string_buffer;
32
33 for (x= 0; x < construct->count; x++)
34 {
35 char buffer[1024]; /* Nothing special for number */
36 int count;
37
38 if (construct->udp)
39 sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -U %u", x, x+ TEST_PORT_BASE);
40 else
41 sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -p %u", x, x+ TEST_PORT_BASE);
42 system(buffer);
43 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
44 end_ptr+= count;
45 }
46 *end_ptr= 0;
47
48 construct->server_list= strdup(server_string_buffer);
49 }
50 printf("servers %s\n", construct->server_list);
51 construct->servers= memcached_servers_parse(construct->server_list);
52 }
53
54 assert(construct->servers);
55
56 srandom(time(NULL));
57
58 for (x= 0; x < memcached_server_list_count(construct->servers); x++)
59 {
60 printf("\t%s : %u\n", construct->servers[x].hostname, construct->servers[x].port);
61 assert(construct->servers[x].fd == -1);
62 assert(construct->servers[x].cursor_active == 0);
63 }
64
65 printf("\n");
66 }
67
68 void server_shutdown(server_startup_st *construct)
69 {
70 unsigned int x;
71
72 if (construct->server_list)
73 {
74 for (x= 0; x < construct->count; x++)
75 {
76 char buffer[1024]; /* Nothing special for number */
77 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
78 system(buffer);
79
80 sprintf(buffer, "/tmp/%umemc.pid", x);
81 unlink(buffer);
82 }
83
84 free(construct->server_list);
85 }
86 }