4 #include "libmemcached/memcached.hpp"
11 #include <sys/types.h>
23 using namespace memcache
;
26 test_return_t
basic_test(memcached_st
*memc
);
27 test_return_t
increment_test(memcached_st
*memc
);
28 test_return_t
basic_master_key_test(memcached_st
*memc
);
29 test_return_t
mget_result_function(memcached_st
*memc
);
30 test_return_t
basic_behavior(memcached_st
*memc
);
31 test_return_t
mget_test(memcached_st
*memc
);
32 memcached_return_t
callback_counter(memcached_st
*,
33 memcached_result_st
*,
37 static void populate_vector(vector
<char> &vec
, const string
&str
)
39 vec
.reserve(str
.length());
40 vec
.assign(str
.begin(), str
.end());
43 static void copy_vec_to_string(vector
<char> &vec
, string
&str
)
48 str
.assign(vec
.begin(), vec
.end());
52 test_return_t
basic_test(memcached_st
*memc
)
55 const string
value_set("This is some data");
56 std::vector
<char> value
;
57 std::vector
<char> test_value
;
59 populate_vector(value
, value_set
);
61 foo
.set("mine", value
, 0, 0);
62 foo
.get("mine", test_value
);
64 assert((memcmp(&test_value
[0], &value
[0], test_value
.size()) == 0));
67 * Simple test of the exceptions here...this should throw an exception
68 * saying that the key is empty.
72 foo
.set("", value
, 0, 0);
82 test_return_t
increment_test(memcached_st
*memc
)
86 const string
key("blah");
87 const string
inc_value("1");
88 std::vector
<char> inc_val
;
89 vector
<char> ret_value
;
91 uint64_t int_inc_value
;
92 uint64_t int_ret_value
;
94 populate_vector(inc_val
, inc_value
);
96 rc
= mcach
.set(key
, inc_val
, 0, 0);
101 mcach
.get(key
, ret_value
);
102 if (ret_value
.empty())
106 copy_vec_to_string(ret_value
, ret_string
);
108 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
109 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
110 assert(int_ret_value
== int_inc_value
);
112 rc
= mcach
.increment(key
, 1, &int_ret_value
);
114 assert(int_ret_value
== 2);
116 rc
= mcach
.increment(key
, 1, &int_ret_value
);
118 assert(int_ret_value
== 3);
120 rc
= mcach
.increment(key
, 5, &int_ret_value
);
122 assert(int_ret_value
== 8);
127 test_return_t
basic_master_key_test(memcached_st
*memc
)
130 const string
value_set("Data for server A");
132 vector
<char> test_value
;
133 const string
master_key_a("server-a");
134 const string
master_key_b("server-b");
135 const string
key("xyz");
137 populate_vector(value
, value_set
);
139 foo
.setByKey(master_key_a
, key
, value
, 0, 0);
140 foo
.getByKey(master_key_a
, key
, test_value
);
142 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
146 foo
.getByKey(master_key_b
, key
, test_value
);
147 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
152 /* Count the results */
153 memcached_return_t
callback_counter(memcached_st
*,
154 memcached_result_st
*,
157 unsigned int *counter
= static_cast<unsigned int *>(context
);
159 *counter
= *counter
+ 1;
161 return MEMCACHED_SUCCESS
;
164 test_return_t
mget_result_function(memcached_st
*memc
)
168 string
key1("fudge");
172 vector
< vector
<char> *> values
;
176 populate_vector(val1
, key1
);
177 populate_vector(val2
, key2
);
178 populate_vector(val3
, key3
);
180 keys
.push_back(key1
);
181 keys
.push_back(key2
);
182 keys
.push_back(key3
);
184 values
.push_back(&val1
);
185 values
.push_back(&val2
);
186 values
.push_back(&val3
);
187 unsigned int counter
;
188 memcached_execute_fn callbacks
[1];
190 /* We need to empty the server before we continue the test */
192 rc
= mc
.setAll(keys
, values
, 50, 9);
198 callbacks
[0]= &callback_counter
;
200 rc
= mc
.fetchExecute(callbacks
, static_cast<void *>(&counter
), 1);
202 assert(counter
== 3);
207 test_return_t
mget_test(memcached_st
*memc
)
211 memcached_return_t mc_rc
;
213 vector
< vector
<char> *> values
;
215 keys
.push_back("fudge");
216 keys
.push_back("son");
217 keys
.push_back("food");
221 populate_vector(val1
, "fudge");
222 populate_vector(val2
, "son");
223 populate_vector(val3
, "food");
225 values
.push_back(&val1
);
226 values
.push_back(&val2
);
227 values
.push_back(&val3
);
230 vector
<char> return_value
;
232 /* We need to empty the server before we continue the test */
239 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
241 assert(return_value
.size() != 0);
242 return_value
.clear();
244 assert(mc_rc
== MEMCACHED_END
);
246 rc
= mc
.setAll(keys
, values
, 50, 9);
252 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
254 assert(return_key
.length() == return_value
.size());
255 assert(!memcmp(&return_value
[0], return_key
.c_str(), return_value
.size()));
261 test_return_t
basic_behavior(memcached_st
*memc
)
266 rc
= mc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, value
);
268 uint64_t behavior
= mc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
);
269 assert(behavior
== value
);
276 reinterpret_cast<test_callback_fn
>(basic_test
) },
277 { "basic_master_key", 0,
278 reinterpret_cast<test_callback_fn
>(basic_master_key_test
) },
279 { "increment_test", 0,
280 reinterpret_cast<test_callback_fn
>(increment_test
) },
282 reinterpret_cast<test_callback_fn
>(mget_test
) },
283 { "mget_result_function", 1,
284 reinterpret_cast<test_callback_fn
>(mget_result_function
) },
285 { "basic_behavior", 0,
286 reinterpret_cast<test_callback_fn
>(basic_behavior
) },
290 collection_st collection
[] ={
291 {"block", 0, 0, tests
},
295 #define SERVERS_TO_CREATE 5
297 #include "libmemcached_world.h"
299 void get_world(world_st
*world
)
301 world
->collections
= collection
;
302 world
->collection_startup
= reinterpret_cast<test_callback_fn
>(world_collection_startup
);
303 world
->flush
= reinterpret_cast<test_callback_fn
>(world_flush
);
304 world
->pre_run
= reinterpret_cast<test_callback_fn
>(world_pre_run
);
305 world
->create
= reinterpret_cast<test_callback_create_fn
>(world_create
);
306 world
->post_run
= reinterpret_cast<test_callback_fn
>(world_post_run
);
307 world
->on_error
= reinterpret_cast<test_callback_error_fn
>(world_on_error
);
308 world
->destroy
= reinterpret_cast<test_callback_fn
>(world_destroy
);
309 world
->runner
= &defualt_libmemcached_runner
;