Add --with-memcached to be able to specify the memcached binary to use for make test
[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 "libmemcached/libmemcached_config.h"
15 #include "server.h"
16
17 void server_startup(server_startup_st *construct)
18 {
19 unsigned int x;
20
21 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
22 {
23 printf("servers %s\n", construct->server_list);
24 construct->servers= memcached_servers_parse(construct->server_list);
25 construct->server_list= NULL;
26 construct->count= 0;
27 }
28 else
29 {
30 {
31 char server_string_buffer[8096];
32 char *end_ptr;
33 end_ptr= server_string_buffer;
34
35 for (x= 0; x < construct->count; x++)
36 {
37 char buffer[1024]; /* Nothing special for number */
38 int count;
39 int status;
40
41 if (construct->udp){
42 if(x == 0) {
43 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -U %u -m 128",
44 MEMCACHED_BINARY, x, x+ TEST_PORT_BASE);
45 } else {
46 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -U %u",
47 MEMCACHED_BINARY, x, x+ TEST_PORT_BASE);
48 }
49 }
50 else{
51 if(x == 0) {
52 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -m 128",
53 MEMCACHED_BINARY, x, x+ TEST_PORT_BASE);
54 } else {
55 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u",
56 MEMCACHED_BINARY, x, x+ TEST_PORT_BASE);
57 }
58 }
59 status= system(buffer);
60 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
61 end_ptr+= count;
62 }
63 *end_ptr= 0;
64
65 construct->server_list= strdup(server_string_buffer);
66 }
67 printf("servers %s\n", construct->server_list);
68 construct->servers= memcached_servers_parse(construct->server_list);
69 }
70
71 assert(construct->servers);
72
73 srandom(time(NULL));
74
75 for (x= 0; x < memcached_server_list_count(construct->servers); x++)
76 {
77 printf("\t%s : %u\n", construct->servers[x].hostname, construct->servers[x].port);
78 assert(construct->servers[x].fd == -1);
79 assert(construct->servers[x].cursor_active == 0);
80 }
81
82 printf("\n");
83 }
84
85 void server_shutdown(server_startup_st *construct)
86 {
87 unsigned int x;
88
89 if (construct->server_list)
90 {
91 for (x= 0; x < construct->count; x++)
92 {
93 char buffer[1024]; /* Nothing special for number */
94 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
95 system(buffer);
96
97 sprintf(buffer, "/tmp/%umemc.pid", x);
98 unlink(buffer);
99 }
100
101 free(construct->server_list);
102 }
103 }