9bf0e92a838fca9cbab14ac07fed51d20c322955
[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
48 memcached_return_t return_value_based_on_buffering(memcached_st *memc)
49 {
50 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS))
51 {
52 return MEMCACHED_BUFFERED;
53 }
54
55 return MEMCACHED_SUCCESS;
56 }
57
58 /**
59 @note This should be testing to see if the server really supports the binary protocol.
60 */
61 test_return_t pre_binary(memcached_st *memc)
62 {
63 test_skip(true, libmemcached_util_version_check(memc, 1, 4, 4));
64 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
65
66 return TEST_SUCCESS;
67 }
68
69 test_return_t pre_unix_socket(memcached_st *memc)
70 {
71 struct stat buf;
72
73 memcached_servers_reset(memc);
74 const char *socket_file= libtest::default_socket();
75 test_skip(true, bool(socket_file));
76
77 test_skip(0, stat(socket_file, &buf));
78
79 test_compare(MEMCACHED_SUCCESS,
80 memcached_server_add_unix_socket_with_weight(memc, socket_file, 0));
81
82 return TEST_SUCCESS;
83 }
84
85 test_return_t pre_nodelay(memcached_st *memc)
86 {
87 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
88 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
89
90 return TEST_SUCCESS;
91 }
92
93 test_return_t pre_settimer(memcached_st *memc)
94 {
95 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
96 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
97
98 return TEST_SUCCESS;
99 }
100
101 test_return_t pre_murmur(memcached_st *memc)
102 {
103 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR));
104 return TEST_SUCCESS;
105 }
106
107 test_return_t pre_jenkins(memcached_st *memc)
108 {
109 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
110
111 return TEST_SKIPPED;
112 }
113
114
115 test_return_t pre_md5(memcached_st *memc)
116 {
117 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
118
119 return TEST_SUCCESS;
120 }
121
122 test_return_t pre_crc(memcached_st *memc)
123 {
124 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
125
126 return TEST_SUCCESS;
127 }
128
129 test_return_t pre_hsieh(memcached_st *memc)
130 {
131 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH));
132 return TEST_SUCCESS;
133 }
134
135 test_return_t pre_hash_fnv1_64(memcached_st *memc)
136 {
137 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR));
138
139 return TEST_SUCCESS;
140 }
141
142 test_return_t pre_hash_fnv1a_64(memcached_st *memc)
143 {
144 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64));
145
146 return TEST_SUCCESS;
147 }
148
149 test_return_t pre_hash_fnv1_32(memcached_st *memc)
150 {
151 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
152
153 return TEST_SUCCESS;
154 }
155
156 test_return_t pre_hash_fnv1a_32(memcached_st *memc)
157 {
158 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
159
160 return TEST_SUCCESS;
161 }
162
163 test_return_t pre_behavior_ketama(memcached_st *memc)
164 {
165 memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
166 test_compare(MEMCACHED_SUCCESS, rc);
167
168 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
169 test_compare(value, uint64_t(1));
170
171 return TEST_SUCCESS;
172 }
173
174 test_return_t pre_behavior_ketama_weighted(memcached_st *memc)
175 {
176 memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
177 test_compare(MEMCACHED_SUCCESS, rc);
178
179 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
180 test_compare(value, uint64_t(1));
181
182 test_compare(MEMCACHED_SUCCESS,
183 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5));
184
185 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
186 test_compare(MEMCACHED_HASH_MD5, memcached_hash_t(value));
187
188 return TEST_SUCCESS;
189 }
190
191 test_return_t pre_replication(memcached_st *memc)
192 {
193 test_skip(TEST_SUCCESS, pre_binary(memc));
194
195 /*
196 * Make sure that we store the item on all servers
197 * (master + replicas == number of servers)
198 */
199 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, memcached_server_count(memc) - 1));
200 test_compare(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS), uint64_t(memcached_server_count(memc) - 1));
201
202 return TEST_SUCCESS;
203 }
204
205
206 test_return_t pre_replication_noblock(memcached_st *memc)
207 {
208 test_skip(TEST_SUCCESS, pre_replication(memc));
209
210 return pre_nonblock(memc);
211 }
212
213 test_return_t pre_nonblock(memcached_st *memc)
214 {
215 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
216
217 return TEST_SUCCESS;
218 }
219
220 test_return_t pre_cork(memcached_st *memc)
221 {
222 #ifdef __APPLE__
223 return TEST_SKIPPED;
224 #endif
225 bool set= true;
226 if (memcached_success(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CORK, set)))
227 return TEST_SUCCESS;
228
229 return TEST_SKIPPED;
230 }
231
232 test_return_t pre_cork_and_nonblock(memcached_st *memc)
233 {
234 #ifdef __APPLE__
235 return TEST_SKIPPED;
236 #endif
237 test_return_t test_rc;
238 if ((test_rc= pre_cork(memc)) != TEST_SUCCESS)
239 return test_rc;
240
241 return pre_nonblock(memc);
242 }
243
244 test_return_t pre_nonblock_binary(memcached_st *memc)
245 {
246 memcached_st *memc_clone= memcached_clone(NULL, memc);
247 test_true(memc_clone);
248
249 // The memcached_version needs to be done on a clone, because the server
250 // will not toggle protocol on an connection.
251 memcached_version(memc_clone);
252
253 memcached_return_t rc= MEMCACHED_FAILURE;
254 if (libmemcached_util_version_check(memc_clone, 1, 4, 4))
255 {
256 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
257 test_compare(MEMCACHED_SUCCESS,
258 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1));
259 test_compare(uint64_t(1), memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
260 }
261 else
262 {
263 memcached_free(memc_clone);
264 return TEST_SKIPPED;
265 }
266
267 memcached_free(memc_clone);
268
269 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
270 }