1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Gearmand client and server library.
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libtest/common.h>
40 #include <libmemcached/common.h>
41 #include <tests/replication.h>
43 test_return_t
replication_set_test(memcached_st
*memc
)
45 memcached_return_t rc
;
46 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
47 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
49 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
50 test_true(rc
== MEMCACHED_SUCCESS
);
53 ** We are using the quiet commands to store the replicas, so we need
54 ** to ensure that all of them are processed before we can continue.
55 ** In the test we go directly from storing the object to trying to
56 ** receive the object from all of the different servers, so we
57 ** could end up in a race condition (the memcached server hasn't yet
58 ** processed the quiet command from the replication set when it process
59 ** the request from the other client (created by the clone)). As a
60 ** workaround for that we call memcached_quit to send the quit command
61 ** to the server and wait for the response ;-) If you use the test code
62 ** as an example for your own code, please note that you shouldn't need
68 ** "bubba" should now be stored on all of our servers. We don't have an
69 ** easy to use API to address each individual server, so I'll just iterate
70 ** through a bunch of "master keys" and I should most likely hit all of the
73 for (int x
= 'a'; x
<= 'z'; ++x
)
75 const char key
[2]= { (char)x
, 0 };
78 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
80 test_true(rc
== MEMCACHED_SUCCESS
);
81 test_true(val
!= NULL
);
85 memcached_free(memc_clone
);
90 test_return_t
replication_get_test(memcached_st
*memc
)
92 memcached_return_t rc
;
95 * Don't do the following in your code. I am abusing the internal details
96 * within the library, and this is not a supported interface.
97 * This is to verify correct behavior in the library
99 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
101 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
102 memcached_server_instance_st instance
=
103 memcached_server_instance_by_position(memc_clone
, host
);
105 ((memcached_server_write_instance_st
)instance
)->port
= 0;
107 for (int x
= 'a'; x
<= 'z'; ++x
)
109 const char key
[2]= { (char)x
, 0 };
112 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
114 test_true(rc
== MEMCACHED_SUCCESS
);
115 test_true(val
!= NULL
);
119 memcached_free(memc_clone
);
125 test_return_t
replication_mget_test(memcached_st
*memc
)
127 memcached_return_t rc
;
128 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
129 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
131 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
132 size_t len
[]= { 5, 4, 4, 4 };
134 for (size_t x
= 0; x
< 4; ++x
)
136 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
137 test_true(rc
== MEMCACHED_SUCCESS
);
141 ** We are using the quiet commands to store the replicas, so we need
142 ** to ensure that all of them are processed before we can continue.
143 ** In the test we go directly from storing the object to trying to
144 ** receive the object from all of the different servers, so we
145 ** could end up in a race condition (the memcached server hasn't yet
146 ** processed the quiet command from the replication set when it process
147 ** the request from the other client (created by the clone)). As a
148 ** workaround for that we call memcached_quit to send the quit command
149 ** to the server and wait for the response ;-) If you use the test code
150 ** as an example for your own code, please note that you shouldn't need
153 memcached_quit(memc
);
156 * Don't do the following in your code. I am abusing the internal details
157 * within the library, and this is not a supported interface.
158 * This is to verify correct behavior in the library
160 memcached_result_st result_obj
;
161 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
163 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
164 memcached_server_instance_st instance
=
165 memcached_server_instance_by_position(new_clone
, host
);
166 ((memcached_server_write_instance_st
)instance
)->port
= 0;
168 for (int x
= 'a'; x
<= 'z'; ++x
)
170 char key
[2]= { (char)x
, 0 };
172 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
173 test_true(rc
== MEMCACHED_SUCCESS
);
175 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
179 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
183 test_true(hits
== 4);
184 memcached_result_free(&result_obj
);
187 memcached_free(new_clone
);
190 memcached_free(memc_clone
);
195 test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
197 memcached_result_st result_obj
;
198 memcached_return_t rc
;
199 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
200 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
201 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
203 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
204 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
206 for (size_t x
= 0; x
< 7; ++x
)
208 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
209 test_true(rc
== MEMCACHED_SUCCESS
);
212 memcached_quit(memc
);
214 for (size_t x
= 0; x
< 7; ++x
)
216 const char key
[2]= { (char)x
, 0 };
218 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
219 test_true(rc
== MEMCACHED_SUCCESS
);
221 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
225 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
229 test_true(hits
== 7);
230 memcached_result_free(&result_obj
);
232 memcached_free(memc_clone
);
236 test_return_t
replication_delete_test(memcached_st
*memc
)
238 memcached_return_t rc
;
239 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
240 /* Delete the items from all of the servers except 1 */
241 uint64_t repl
= memcached_behavior_get(memc
,
242 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
243 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
245 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
246 size_t len
[]= { 5, 4, 4, 4 };
248 for (size_t x
= 0; x
< 4; ++x
)
250 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
251 test_true(rc
== MEMCACHED_SUCCESS
);
255 * Don't do the following in your code. I am abusing the internal details
256 * within the library, and this is not a supported interface.
257 * This is to verify correct behavior in the library
259 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
260 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
262 memcached_server_instance_st instance
=
263 memcached_server_instance_by_position(memc_clone
, x
);
265 ((memcached_server_write_instance_st
)instance
)->port
= 0;
266 if (++hash
== memc_clone
->number_of_hosts
)
270 memcached_result_st result_obj
;
271 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
273 for (size_t x
= 'a'; x
<= 'z'; ++x
)
275 const char key
[2]= { (char)x
, 0 };
277 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
278 test_true(rc
== MEMCACHED_SUCCESS
);
280 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
284 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
288 test_true(hits
== 4);
289 memcached_result_free(&result_obj
);
292 memcached_free(memc_clone
);
297 test_return_t
replication_randomize_mget_fail_test(memcached_st
*memc
)
299 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
300 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
302 for (int x
= int(MEMCACHED_SUCCESS
); x
< int(MEMCACHED_MAXIMUM_RETURN
); ++x
)
304 const char *key
= memcached_strerror(NULL
, memcached_return_t(x
));
305 memcached_return_t rc
= memcached_set(memc
,
307 key
, strlen(key
), 0, 0);
308 test_true(rc
== MEMCACHED_SUCCESS
);
311 memcached_flush_buffers(memc
);
313 // We need to now cause a failure in one server, never do this in your own
315 close(memc_clone
->servers
[1].fd
);
316 memc_clone
->servers
[1].port
= 1;
317 memc_clone
->servers
[1].address_info_next
= NULL
;
319 for (int x
= int(MEMCACHED_SUCCESS
); x
< int(MEMCACHED_MAXIMUM_RETURN
); ++x
)
321 const char *key
= memcached_strerror(NULL
, memcached_return_t(x
));
322 memcached_return_t rc
;
325 char *value
= memcached_get(memc_clone
, key
, strlen(key
), &value_length
, &flags
, &rc
);
326 test_true(rc
== MEMCACHED_SUCCESS
);
327 test_compare(strlen(key
), value_length
);
328 test_strcmp(key
, value
);
331 memcached_free(memc_clone
);