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