c8e1d98fa42fa4692d85931c58006bc699ed2d23
[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 #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 WATCHPOINT;
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
39 if (construct->udp)
40 sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -U %u", x, x+ TEST_PORT_BASE);
41 else
42 sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -p %u", x, x+ TEST_PORT_BASE);
43 system(buffer);
44 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
45 end_ptr+= count;
46 }
47 *end_ptr= 0;
48
49 construct->server_list= strdup(server_string_buffer);
50 }
51 printf("servers %s\n", construct->server_list);
52 construct->servers= memcached_servers_parse(construct->server_list);
53 }
54
55 assert(construct->servers);
56
57 srandom(time(NULL));
58
59 for (x= 0; x < memcached_server_list_count(construct->servers); x++)
60 {
61 printf("\t%s : %u\n", construct->servers[x].hostname, construct->servers[x].port);
62 assert(construct->servers[x].fd == -1);
63 assert(construct->servers[x].cursor_active == 0);
64 }
65
66 printf("\n");
67 }
68
69 void server_shutdown(server_startup_st *construct)
70 {
71 unsigned int x;
72
73 if (construct->server_list)
74 {
75 for (x= 0; x < construct->count; x++)
76 {
77 char buffer[1024]; /* Nothing special for number */
78 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
79 system(buffer);
80
81 sprintf(buffer, "/tmp/%umemc.pid", x);
82 unlink(buffer);
83 }
84
85 free(construct->server_list);
86 }
87 }