4 #include "libmemcached/memcached.hpp"
11 #include <sys/types.h>
16 #include <libtest/server.h>
18 #include <libtest/test.h>
24 using namespace memcache
;
27 test_return_t
basic_test(memcached_st
*memc
);
28 test_return_t
increment_test(memcached_st
*memc
);
29 test_return_t
basic_master_key_test(memcached_st
*memc
);
30 test_return_t
mget_result_function(memcached_st
*memc
);
31 test_return_t
basic_behavior(memcached_st
*memc
);
32 test_return_t
mget_test(memcached_st
*memc
);
33 memcached_return_t
callback_counter(const memcached_st
*,
34 memcached_result_st
*,
38 static void populate_vector(vector
<char> &vec
, const string
&str
)
40 vec
.reserve(str
.length());
41 vec
.assign(str
.begin(), str
.end());
44 static void copy_vec_to_string(vector
<char> &vec
, string
&str
)
49 str
.assign(vec
.begin(), vec
.end());
53 test_return_t
basic_test(memcached_st
*memc
)
56 const string
value_set("This is some data");
57 std::vector
<char> value
;
58 std::vector
<char> test_value
;
60 populate_vector(value
, value_set
);
62 foo
.set("mine", value
, 0, 0);
63 foo
.get("mine", test_value
);
65 assert((memcmp(&test_value
[0], &value
[0], test_value
.size()) == 0));
68 * Simple test of the exceptions here...this should throw an exception
69 * saying that the key is empty.
73 foo
.set("", value
, 0, 0);
83 test_return_t
increment_test(memcached_st
*memc
)
87 const string
key("blah");
88 const string
inc_value("1");
89 std::vector
<char> inc_val
;
90 vector
<char> ret_value
;
92 uint64_t int_inc_value
;
93 uint64_t int_ret_value
;
95 populate_vector(inc_val
, inc_value
);
97 rc
= mcach
.set(key
, inc_val
, 0, 0);
102 mcach
.get(key
, ret_value
);
103 if (ret_value
.empty())
107 copy_vec_to_string(ret_value
, ret_string
);
109 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
110 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
111 assert(int_ret_value
== int_inc_value
);
113 rc
= mcach
.increment(key
, 1, &int_ret_value
);
115 assert(int_ret_value
== 2);
117 rc
= mcach
.increment(key
, 1, &int_ret_value
);
119 assert(int_ret_value
== 3);
121 rc
= mcach
.increment(key
, 5, &int_ret_value
);
123 assert(int_ret_value
== 8);
128 test_return_t
basic_master_key_test(memcached_st
*memc
)
131 const string
value_set("Data for server A");
133 vector
<char> test_value
;
134 const string
master_key_a("server-a");
135 const string
master_key_b("server-b");
136 const string
key("xyz");
138 populate_vector(value
, value_set
);
140 foo
.setByKey(master_key_a
, key
, value
, 0, 0);
141 foo
.getByKey(master_key_a
, key
, test_value
);
143 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
147 foo
.getByKey(master_key_b
, key
, test_value
);
148 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
153 /* Count the results */
154 memcached_return_t
callback_counter(const memcached_st
*,
155 memcached_result_st
*,
158 unsigned int *counter
= static_cast<unsigned int *>(context
);
160 *counter
= *counter
+ 1;
162 return MEMCACHED_SUCCESS
;
165 test_return_t
mget_result_function(memcached_st
*memc
)
169 string
key1("fudge");
173 vector
< vector
<char> *> values
;
177 populate_vector(val1
, key1
);
178 populate_vector(val2
, key2
);
179 populate_vector(val3
, key3
);
181 keys
.push_back(key1
);
182 keys
.push_back(key2
);
183 keys
.push_back(key3
);
185 values
.push_back(&val1
);
186 values
.push_back(&val2
);
187 values
.push_back(&val3
);
188 unsigned int counter
;
189 memcached_execute_fn callbacks
[1];
191 /* We need to empty the server before we continue the test */
193 rc
= mc
.setAll(keys
, values
, 50, 9);
199 callbacks
[0]= &callback_counter
;
201 rc
= mc
.fetchExecute(callbacks
, static_cast<void *>(&counter
), 1);
203 assert(counter
== 3);
208 test_return_t
mget_test(memcached_st
*memc
)
212 memcached_return_t mc_rc
;
214 vector
< vector
<char> *> values
;
216 keys
.push_back("fudge");
217 keys
.push_back("son");
218 keys
.push_back("food");
222 populate_vector(val1
, "fudge");
223 populate_vector(val2
, "son");
224 populate_vector(val3
, "food");
226 values
.push_back(&val1
);
227 values
.push_back(&val2
);
228 values
.push_back(&val3
);
231 vector
<char> return_value
;
233 /* We need to empty the server before we continue the test */
240 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
242 assert(return_value
.size() != 0);
243 return_value
.clear();
245 assert(mc_rc
== MEMCACHED_END
);
247 rc
= mc
.setAll(keys
, values
, 50, 9);
253 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
255 assert(return_key
.length() == return_value
.size());
256 assert(!memcmp(&return_value
[0], return_key
.c_str(), return_value
.size()));
262 test_return_t
basic_behavior(memcached_st
*memc
)
267 rc
= mc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, value
);
269 uint64_t behavior
= mc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
);
270 assert(behavior
== value
);
277 reinterpret_cast<test_callback_fn
>(basic_test
) },
278 { "basic_master_key", 0,
279 reinterpret_cast<test_callback_fn
>(basic_master_key_test
) },
280 { "increment_test", 0,
281 reinterpret_cast<test_callback_fn
>(increment_test
) },
283 reinterpret_cast<test_callback_fn
>(mget_test
) },
284 { "mget_result_function", 1,
285 reinterpret_cast<test_callback_fn
>(mget_result_function
) },
286 { "basic_behavior", 0,
287 reinterpret_cast<test_callback_fn
>(basic_behavior
) },
291 collection_st collection
[] ={
292 {"block", 0, 0, tests
},
296 #define SERVERS_TO_CREATE 5
298 #include "libmemcached_world.h"
300 void get_world(world_st
*world
)
302 world
->collections
= collection
;
304 world
->create
= reinterpret_cast<test_callback_create_fn
>(world_create
);
305 world
->destroy
= reinterpret_cast<test_callback_fn
>(world_destroy
);
307 world
->test
.startup
= reinterpret_cast<test_callback_fn
>(world_test_startup
);
308 world
->test
.flush
= reinterpret_cast<test_callback_fn
>(world_flush
);
309 world
->test
.pre_run
= reinterpret_cast<test_callback_fn
>(world_pre_run
);
310 world
->test
.post_run
= reinterpret_cast<test_callback_fn
>(world_post_run
);
311 world
->test
.on_error
= reinterpret_cast<test_callback_error_fn
>(world_on_error
);
313 world
->collection
.startup
= reinterpret_cast<test_callback_fn
>(world_container_startup
);
314 world
->collection
.shutdown
= reinterpret_cast<test_callback_fn
>(world_container_shutdown
);
316 world
->runner
= &defualt_libmemcached_runner
;