Merge in all of libtest updates.
[m6w6/libmemcached] / tests / debug.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached client and server library.
4 *
5 * Copyright (C) 2011 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
40 #include <libtest/test.hpp>
41 #include <climits>
42
43 using namespace libtest;
44
45 #include <libmemcached/memcached.h>
46 #include <tests/debug.h>
47
48 /* Dump each server's keys */
49 static memcached_return_t print_keys_callback(const memcached_st *,
50 const char *key,
51 size_t key_length,
52 void *)
53 {
54
55 Out << "\t" << key << " (" << key_length << ")";
56
57
58 return MEMCACHED_SUCCESS;
59 }
60
61 static memcached_return_t server_wrapper_for_dump_callback(const memcached_st *,
62 memcached_server_instance_st server,
63 void *)
64 {
65 memcached_st *memc= memcached_create(NULL);
66
67 if (server->type == MEMCACHED_CONNECTION_UNIX_SOCKET)
68 {
69 if (memcached_failed(memcached_server_add_unix_socket(memc, memcached_server_name(server))))
70 {
71 return MEMCACHED_FAILURE;
72 }
73 }
74 else
75 {
76 if (memcached_failed(memcached_server_add(memc, memcached_server_name(server), memcached_server_port(server))))
77 {
78 return MEMCACHED_FAILURE;
79 }
80 }
81
82 memcached_dump_fn callbacks[1];
83
84 callbacks[0]= &print_keys_callback;
85
86 Out << memcached_server_name(server) << ":" << memcached_server_port(server);
87
88 if (memcached_failed(memcached_dump(memc, callbacks, NULL, 1)))
89 {
90 return MEMCACHED_FAILURE;
91 }
92
93 memcached_free(memc);
94
95 return MEMCACHED_SUCCESS;
96 }
97
98
99 test_return_t confirm_keys_exist(memcached_st *memc, const char * const *keys, const size_t number_of_keys, bool key_matches_value)
100 {
101 for (size_t x= 0; x < number_of_keys; ++x)
102 {
103 memcached_return_t rc;
104 size_t value_length;
105 char *value= memcached_get(memc,
106 test_string_make_from_cstr(keys[x]), // Keys
107 &value_length,
108 0, &rc);
109 test_true_got(value, keys[x]);
110 if (key_matches_value)
111 {
112 test_strcmp(keys[x], value);
113 }
114 free(value);
115 }
116
117 return TEST_SUCCESS;
118 }
119
120 test_return_t confirm_keys_dont_exist(memcached_st *memc, const char * const *keys, const size_t number_of_keys)
121 {
122 for (size_t x= 0; x < number_of_keys; ++x)
123 {
124 memcached_return_t rc;
125 size_t value_length;
126 char *value= memcached_get(memc,
127 test_string_make_from_cstr(keys[x]), // Keys
128 &value_length,
129 0, &rc);
130 test_false(value);
131 test_compare(MEMCACHED_NOTFOUND, rc);
132 }
133
134 return TEST_SUCCESS;
135 }
136
137
138 test_return_t print_keys_by_server(memcached_st *memc)
139 {
140 memcached_server_fn callback[]= { server_wrapper_for_dump_callback };
141 test_compare(MEMCACHED_SUCCESS, memcached_server_cursor(memc, callback, NULL, test_array_length(callback)));
142
143 return TEST_SUCCESS;
144 }
145
146 static memcached_return_t callback_dump_counter(const memcached_st *ptr,
147 const char *key,
148 size_t key_length,
149 void *context)
150 {
151 (void)ptr; (void)key; (void)key_length;
152 size_t *counter= (size_t *)context;
153
154 *counter= *counter + 1;
155
156 return MEMCACHED_SUCCESS;
157 }
158
159 size_t confirm_key_count(memcached_st *memc)
160 {
161 memcached_st *clone= memcached_clone(NULL, memc);
162 if (memcached_failed(memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, false)))
163 {
164 memcached_free(clone);
165 return ULONG_MAX;
166 }
167
168 memcached_dump_fn callbacks[1];
169
170 callbacks[0]= &callback_dump_counter;
171
172 size_t count= 0;
173 if (memcached_failed(memcached_dump(clone, callbacks, (void *)&count, 1)))
174 {
175 memcached_free(clone);
176 return ULONG_MAX;
177 }
178
179 memcached_free(clone);
180 return count;
181 }