1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
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.
43 #include <libmemcached/memcached.hpp>
44 #include <libtest/test.hpp>
50 #include <sys/types.h>
59 using namespace memcache
;
60 using namespace libtest
;
63 test_return_t
basic_test(memcached_st
*memc
);
64 test_return_t
increment_test(memcached_st
*memc
);
65 test_return_t
basic_master_key_test(memcached_st
*memc
);
66 test_return_t
mget_result_function(memcached_st
*memc
);
67 test_return_t
basic_behavior(memcached_st
*memc
);
68 test_return_t
mget_test(memcached_st
*memc
);
69 memcached_return_t
callback_counter(const memcached_st
*,
70 memcached_result_st
*,
74 static void populate_vector(vector
<char> &vec
, const string
&str
)
76 vec
.reserve(str
.length());
77 vec
.assign(str
.begin(), str
.end());
80 static void copy_vec_to_string(vector
<char> &vec
, string
&str
)
85 str
.assign(vec
.begin(), vec
.end());
89 test_return_t
basic_test(memcached_st
*memc
)
92 const string
value_set("This is some data");
93 std::vector
<char> value
;
94 std::vector
<char> test_value
;
96 populate_vector(value
, value_set
);
98 test_true(foo
.set("mine", value
, 0, 0));
99 test_true(foo
.get("mine", test_value
));
101 test_memcmp(&test_value
[0], &value
[0], test_value
.size());
102 test_false(foo
.set("", value
, 0, 0));
107 test_return_t
increment_test(memcached_st
*original
)
109 Memcache
mcach(original
);
110 const string
key("blah");
111 const string
inc_value("1");
112 std::vector
<char> inc_val
;
113 vector
<char> ret_value
;
115 uint64_t int_inc_value
;
116 uint64_t int_ret_value
;
118 populate_vector(inc_val
, inc_value
);
120 test_true(mcach
.set(key
, inc_val
, 0, 0));
122 test_true(mcach
.get(key
, ret_value
));
123 test_false(ret_value
.empty());
124 copy_vec_to_string(ret_value
, ret_string
);
126 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
127 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
128 test_compare(int_inc_value
, int_ret_value
);
130 test_true(mcach
.increment(key
, 1, &int_ret_value
));
131 test_compare(uint64_t(2), int_ret_value
);
133 test_true(mcach
.increment(key
, 1, &int_ret_value
));
134 test_compare(uint64_t(3), int_ret_value
);
136 test_true(mcach
.increment(key
, 5, &int_ret_value
));
137 test_compare(uint64_t(8), int_ret_value
);
142 test_return_t
basic_master_key_test(memcached_st
*original
)
144 Memcache
foo(original
);
145 const string
value_set("Data for server A");
147 vector
<char> test_value
;
148 const string
master_key_a("server-a");
149 const string
master_key_b("server-b");
150 const string
key("xyz");
152 populate_vector(value
, value_set
);
154 test_true(foo
.setByKey(master_key_a
, key
, value
, 0, 0));
155 test_true(foo
.getByKey(master_key_a
, key
, test_value
));
157 test_compare(value
.size(), test_value
.size());
158 test_memcmp(&value
[0], &test_value
[0], value
.size());
163 test_false(foo
.getByKey(master_key_b
, key
, test_value
));
164 test_zero(test_value
.size());
170 /* Count the results */
171 memcached_return_t
callback_counter(const memcached_st
*,
172 memcached_result_st
*,
175 unsigned int *counter
= static_cast<unsigned int *>(context
);
177 *counter
= *counter
+1;
179 return MEMCACHED_SUCCESS
;
182 test_return_t
mget_test(memcached_st
*original
)
184 Memcache
memc(original
);
185 memcached_return_t mc_rc
;
187 vector
< vector
<char> *> values
;
189 keys
.push_back("fudge");
190 keys
.push_back("son");
191 keys
.push_back("food");
195 populate_vector(val1
, "fudge");
196 populate_vector(val2
, "son");
197 populate_vector(val3
, "food");
199 values
.push_back(&val1
);
200 values
.push_back(&val2
);
201 values
.push_back(&val3
);
204 vector
<char> return_value
;
206 /* We need to empty the server before we continue the test */
207 test_true(memc
.flush());
209 test_true(memc
.mget(keys
));
211 test_compare(MEMCACHED_NOTFOUND
,
212 memc
.fetch(return_key
, return_value
));
214 test_true(memc
.setAll(keys
, values
, 50, 9));
216 test_true(memc
.mget(keys
));
218 while (memcached_success(mc_rc
= memc
.fetch(return_key
, return_value
)))
220 test_compare(return_key
.length(), return_value
.size());
221 test_memcmp(&return_value
[0], return_key
.c_str(), return_value
.size());
224 test_compare(values
.size(), count
);
229 test_return_t
basic_behavior(memcached_st
*original
)
231 Memcache
memc(original
);
233 test_true(memc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, value
));
234 uint64_t behavior
= memc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
);
235 test_compare(behavior
, value
);
242 reinterpret_cast<test_callback_fn
*>(basic_test
) },
243 { "basic_master_key", 0,
244 reinterpret_cast<test_callback_fn
*>(basic_master_key_test
) },
245 { "increment_test", 0,
246 reinterpret_cast<test_callback_fn
*>(increment_test
) },
248 reinterpret_cast<test_callback_fn
*>(mget_test
) },
249 { "basic_behavior", 0,
250 reinterpret_cast<test_callback_fn
*>(basic_behavior
) },
254 collection_st collection
[] ={
255 {"block", 0, 0, tests
},
259 #define SERVERS_TO_CREATE 5
261 #include "libmemcached_world.h"
263 void get_world(Framework
*world
)
265 world
->collections
= collection
;
267 world
->_create
= world_create
;
268 world
->_destroy
= world_destroy
;
270 world
->item
._startup
= reinterpret_cast<test_callback_fn
*>(world_test_startup
);
271 world
->item
._flush
= reinterpret_cast<test_callback_fn
*>(world_flush
);
272 world
->item
.set_pre(reinterpret_cast<test_callback_fn
*>(world_pre_run
));
273 world
->item
.set_post(reinterpret_cast<test_callback_fn
*>(world_post_run
));
274 world
->_on_error
= reinterpret_cast<test_callback_error_fn
*>(world_on_error
);
276 world
->collection_startup
= reinterpret_cast<test_callback_fn
*>(world_container_startup
);
277 world
->collection_shutdown
= reinterpret_cast<test_callback_fn
*>(world_container_shutdown
);
279 world
->set_runner(&defualt_libmemcached_runner
);