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