Update
[awesomized/libmemcached] / tests / cycle.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37
38 /*
39 Test that we are cycling the servers we are creating during testing.
40 */
41
42 #include <config.h>
43 #include <libtest/test.hpp>
44
45 #include <libmemcached/memcached.h>
46 #include <libmemcached/is.h>
47 #include <libmemcached/util.h>
48
49 #include <iostream>
50
51
52 #include <libtest/server.h>
53
54 using namespace libtest;
55
56 #ifndef __INTEL_COMPILER
57 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
58 #endif
59
60 static test_return_t alive(memcached_st *memc)
61 {
62 test_true(memc);
63 test_true(memcached_is_allocated(memc));
64 for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
65 {
66 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
67 test_true(instance);
68
69 test_true(libmemcached_util_ping(memcached_server_name(instance),
70 memcached_server_port(instance), NULL));
71 }
72
73 return TEST_SUCCESS;
74 }
75
76 static test_return_t valid(memcached_st *memc)
77 {
78 test_true(memc);
79 test_true(memcached_is_allocated(memc));
80
81 for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
82 {
83 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
84 test_true(instance);
85
86 pid_t pid= libmemcached_util_getpid(memcached_server_name(instance),
87 memcached_server_port(instance), NULL);
88 test_true(pid != -1);
89 }
90
91 return TEST_SUCCESS;
92 }
93
94 static test_return_t kill_test(memcached_st *)
95 {
96 static struct timespec global_sleep_value= { 2, 0 };
97
98 #ifdef WIN32
99 sleep(1);
100 #else
101 nanosleep(&global_sleep_value, NULL);
102 #endif
103
104 return TEST_SUCCESS;
105 }
106
107 test_st ping_tests[] ={
108 {"alive", true, (test_callback_fn*)alive },
109 {0, 0, 0}
110 };
111
112 test_st getpid_tests[] ={
113 {"valid", true, (test_callback_fn*)valid },
114 {0, 0, 0}
115 };
116
117 test_st kill_tests[] ={
118 {"kill", true, (test_callback_fn*)kill_test },
119 {0, 0, 0}
120 };
121
122 collection_st collection[] ={
123 {"libmemcached_util_ping()", 0, 0, ping_tests},
124 {"libmemcached_util_getpid()", 0, 0, getpid_tests},
125 {"kill", 0, 0, kill_tests},
126 {0, 0, 0, 0}
127 };
128
129
130 #include "tests/libmemcached_world.h"
131
132 void get_world(Framework *world)
133 {
134 world->collections= collection;
135
136 world->_create= (test_callback_create_fn*)world_create;
137 world->_destroy= (test_callback_destroy_fn*)world_destroy;
138
139 world->item.set_startup((test_callback_fn*)world_test_startup);
140 world->item.set_pre((test_callback_fn*)world_pre_run);
141 world->item.set_post((test_callback_fn*)world_post_run);
142
143 world->set_on_error((test_callback_error_fn*)world_on_error);
144
145 world->collection_startup= (test_callback_fn*)world_container_startup;
146 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
147
148 world->set_runner(&defualt_libmemcached_runner);
149 world->set_socket();
150 }
151