Improvements on valgrind detection.
[awesomized/libmemcached] / tests / libmemcached-1.0 / setup_and_teardowns.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Client and Server
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <config.h>
39 #include <libtest/test.hpp>
40
41 #include <libmemcached/util.h>
42
43 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
44
45 #include <sys/stat.h>
46
47 using namespace libtest;
48
49 memcached_return_t return_value_based_on_buffering(memcached_st *memc)
50 {
51 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS))
52 {
53 return MEMCACHED_BUFFERED;
54 }
55
56 return MEMCACHED_SUCCESS;
57 }
58
59 /**
60 @note This should be testing to see if the server really supports the binary protocol.
61 */
62 test_return_t pre_binary(memcached_st *memc)
63 {
64 test_skip(true, libmemcached_util_version_check(memc, 1, 4, 4));
65 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
66
67 return TEST_SUCCESS;
68 }
69
70 test_return_t pre_unix_socket(memcached_st *memc)
71 {
72 struct stat buf;
73
74 memcached_servers_reset(memc);
75 const char *socket_file= libtest::default_socket();
76 test_skip(true, bool(socket_file));
77
78 test_skip(0, stat(socket_file, &buf));
79
80 test_compare(MEMCACHED_SUCCESS,
81 memcached_server_add_unix_socket_with_weight(memc, socket_file, 0));
82
83 return TEST_SUCCESS;
84 }
85
86 test_return_t pre_nodelay(memcached_st *memc)
87 {
88 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
89 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
90
91 return TEST_SUCCESS;
92 }
93
94 test_return_t pre_settimer(memcached_st *memc)
95 {
96 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
97 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
98
99 return TEST_SUCCESS;
100 }
101
102 test_return_t pre_murmur(memcached_st *memc)
103 {
104 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR));
105 return TEST_SUCCESS;
106 }
107
108 test_return_t pre_jenkins(memcached_st *memc)
109 {
110 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
111
112 return TEST_SKIPPED;
113 }
114
115
116 test_return_t pre_md5(memcached_st *memc)
117 {
118 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
119
120 return TEST_SUCCESS;
121 }
122
123 test_return_t pre_crc(memcached_st *memc)
124 {
125 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
126
127 return TEST_SUCCESS;
128 }
129
130 test_return_t pre_hsieh(memcached_st *memc)
131 {
132 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH));
133 return TEST_SUCCESS;
134 }
135
136 test_return_t pre_hash_fnv1_64(memcached_st *memc)
137 {
138 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR));
139
140 return TEST_SUCCESS;
141 }
142
143 test_return_t pre_hash_fnv1a_64(memcached_st *memc)
144 {
145 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64));
146
147 return TEST_SUCCESS;
148 }
149
150 test_return_t pre_hash_fnv1_32(memcached_st *memc)
151 {
152 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
153
154 return TEST_SUCCESS;
155 }
156
157 test_return_t pre_hash_fnv1a_32(memcached_st *memc)
158 {
159 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
160
161 return TEST_SUCCESS;
162 }
163
164 test_return_t pre_behavior_ketama(memcached_st *memc)
165 {
166 memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
167 test_compare(MEMCACHED_SUCCESS, rc);
168
169 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
170 test_compare(value, uint64_t(1));
171
172 return TEST_SUCCESS;
173 }
174
175 test_return_t pre_behavior_ketama_weighted(memcached_st *memc)
176 {
177 memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
178 test_compare(MEMCACHED_SUCCESS, rc);
179
180 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
181 test_compare(value, uint64_t(1));
182
183 test_compare(MEMCACHED_SUCCESS,
184 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5));
185
186 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
187 test_compare(MEMCACHED_HASH_MD5, memcached_hash_t(value));
188
189 return TEST_SUCCESS;
190 }
191
192 test_return_t pre_replication(memcached_st *memc)
193 {
194 test_skip(TEST_SUCCESS, pre_binary(memc));
195
196 /*
197 * Make sure that we store the item on all servers
198 * (master + replicas == number of servers)
199 */
200 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, memcached_server_count(memc) - 1));
201 test_compare(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS), uint64_t(memcached_server_count(memc) - 1));
202
203 return TEST_SUCCESS;
204 }
205
206
207 test_return_t pre_replication_noblock(memcached_st *memc)
208 {
209 test_skip(TEST_SUCCESS, pre_replication(memc));
210
211 return pre_nonblock(memc);
212 }
213
214 test_return_t pre_nonblock(memcached_st *memc)
215 {
216 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
217
218 return TEST_SUCCESS;
219 }
220
221 test_return_t pre_cork(memcached_st *memc)
222 {
223 #ifdef __APPLE__
224 return TEST_SKIPPED;
225 #endif
226 bool set= true;
227 if (memcached_success(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CORK, set)))
228 return TEST_SUCCESS;
229
230 return TEST_SKIPPED;
231 }
232
233 test_return_t pre_cork_and_nonblock(memcached_st *memc)
234 {
235 #ifdef __APPLE__
236 return TEST_SKIPPED;
237 #endif
238 test_return_t test_rc;
239 if ((test_rc= pre_cork(memc)) != TEST_SUCCESS)
240 return test_rc;
241
242 return pre_nonblock(memc);
243 }
244
245 test_return_t pre_nonblock_binary(memcached_st *memc)
246 {
247 memcached_st *memc_clone= memcached_clone(NULL, memc);
248 test_true(memc_clone);
249
250 // The memcached_version needs to be done on a clone, because the server
251 // will not toggle protocol on an connection.
252 memcached_version(memc_clone);
253
254 memcached_return_t rc= MEMCACHED_FAILURE;
255 if (libmemcached_util_version_check(memc_clone, 1, 4, 4))
256 {
257 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
258 test_compare(MEMCACHED_SUCCESS,
259 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1));
260 test_compare(uint64_t(1), memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
261 }
262 else
263 {
264 memcached_free(memc_clone);
265 return TEST_SKIPPED;
266 }
267
268 memcached_free(memc_clone);
269
270 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
271 }