1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Libmemcached 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 "mem_config.h"
40 #include "libtest/test.hpp"
43 using namespace libtest
;
45 #include "libmemcached-1.0/memcached.h"
46 #include "tests/debug.h"
47 #include "tests/print.h"
49 #include "libmemcached/instance.hpp"
51 /* Dump each server's keys */
52 static memcached_return_t
print_keys_callback(const memcached_st
*,
58 Out
<< "\t" << key
<< " (" << key_length
<< ")";
61 return MEMCACHED_SUCCESS
;
64 static memcached_return_t
server_wrapper_for_dump_callback(const memcached_st
*,
65 const memcached_instance_st
* server
,
68 memcached_st
*memc
= memcached_create(NULL
);
70 if (strcmp(memcached_server_type(server
), "SOCKET") == 0)
72 if (memcached_failed(memcached_server_add_unix_socket(memc
, memcached_server_name(server
))))
74 return MEMCACHED_FAILURE
;
79 if (memcached_failed(memcached_server_add(memc
, memcached_server_name(server
), memcached_server_port(server
))))
81 return MEMCACHED_FAILURE
;
85 memcached_dump_fn callbacks
[1];
87 callbacks
[0]= &print_keys_callback
;
89 Out
<< memcached_server_name(server
) << ":" << memcached_server_port(server
);
91 if (memcached_failed(memcached_dump(memc
, callbacks
, NULL
, 1)))
93 return MEMCACHED_FAILURE
;
98 return MEMCACHED_SUCCESS
;
102 test_return_t
confirm_keys_exist(memcached_st
*memc
, const char * const *keys
, const size_t number_of_keys
, bool key_matches_value
, bool require_all
)
104 for (size_t x
= 0; x
< number_of_keys
; ++x
)
106 memcached_return_t rc
;
108 char *value
= memcached_get(memc
,
109 test_string_make_from_cstr(keys
[x
]), // Keys
115 if (key_matches_value
)
117 test_strcmp(keys
[x
], value
);
120 else if (memcached_success(rc
))
122 test_warn(value
, "get() did not return a value");
123 if (value
and key_matches_value
)
125 test_strcmp(keys
[x
], value
);
138 test_return_t
confirm_keys_dont_exist(memcached_st
*memc
, const char * const *keys
, const size_t number_of_keys
)
140 for (size_t x
= 0; x
< number_of_keys
; ++x
)
142 memcached_return_t rc
;
144 char *value
= memcached_get(memc
,
145 test_string_make_from_cstr(keys
[x
]), // Keys
149 test_compare(MEMCACHED_NOTFOUND
, rc
);
156 test_return_t
print_keys_by_server(memcached_st
*memc
)
158 memcached_server_fn callback
[]= { server_wrapper_for_dump_callback
};
159 test_compare(MEMCACHED_SUCCESS
, memcached_server_cursor(memc
, callback
, NULL
, test_array_length(callback
)));
164 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
169 (void)ptr
; (void)key
; (void)key_length
;
170 size_t *counter
= (size_t *)context
;
172 *counter
= *counter
+ 1;
174 return MEMCACHED_SUCCESS
;
177 size_t confirm_key_count(memcached_st
*memc
)
179 memcached_st
*clone
= memcached_clone(NULL
, memc
);
180 if (memcached_failed(memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, false)))
182 memcached_free(clone
);
186 memcached_dump_fn callbacks
[1];
188 callbacks
[0]= &callback_dump_counter
;
191 if (memcached_failed(memcached_dump(clone
, callbacks
, (void *)&count
, 1)))
193 memcached_free(clone
);
197 memcached_free(clone
);
201 void print_servers(memcached_st
*memc
)
203 memcached_server_fn callbacks
[1];
204 callbacks
[0]= server_print_callback
;
205 memcached_server_cursor(memc
, callbacks
, NULL
, 1);