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
*,
35 void *world_create(void);
36 void world_destroy(void *p
);
39 static void populate_vector(vector
<char> &vec
, const string
&str
)
41 vec
.reserve(str
.length());
42 vec
.assign(str
.begin(), str
.end());
45 static void copy_vec_to_string(vector
<char> &vec
, string
&str
)
50 str
.assign(vec
.begin(), vec
.end());
54 test_return_t
basic_test(memcached_st
*memc
)
57 const string
value_set("This is some data");
58 std::vector
<char> value
;
59 std::vector
<char> test_value
;
61 populate_vector(value
, value_set
);
63 foo
.set("mine", value
, 0, 0);
64 foo
.get("mine", test_value
);
66 assert((memcmp(&test_value
[0], &value
[0], test_value
.size()) == 0));
69 * Simple test of the exceptions here...this should throw an exception
70 * saying that the key is empty.
74 foo
.set("", value
, 0, 0);
84 test_return_t
increment_test(memcached_st
*memc
)
88 const string
key("blah");
89 const string
inc_value("1");
90 std::vector
<char> inc_val
;
91 vector
<char> ret_value
;
93 uint64_t int_inc_value
;
94 uint64_t int_ret_value
;
96 populate_vector(inc_val
, inc_value
);
98 rc
= mcach
.set(key
, inc_val
, 0, 0);
103 mcach
.get(key
, ret_value
);
104 if (ret_value
.empty())
108 copy_vec_to_string(ret_value
, ret_string
);
110 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
111 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
112 assert(int_ret_value
== int_inc_value
);
114 rc
= mcach
.increment(key
, 1, &int_ret_value
);
116 assert(int_ret_value
== 2);
118 rc
= mcach
.increment(key
, 1, &int_ret_value
);
120 assert(int_ret_value
== 3);
122 rc
= mcach
.increment(key
, 5, &int_ret_value
);
124 assert(int_ret_value
== 8);
129 test_return_t
basic_master_key_test(memcached_st
*memc
)
132 const string
value_set("Data for server A");
134 vector
<char> test_value
;
135 const string
master_key_a("server-a");
136 const string
master_key_b("server-b");
137 const string
key("xyz");
139 populate_vector(value
, value_set
);
141 foo
.setByKey(master_key_a
, key
, value
, 0, 0);
142 foo
.getByKey(master_key_a
, key
, test_value
);
144 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
148 foo
.getByKey(master_key_b
, key
, test_value
);
149 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
154 /* Count the results */
155 memcached_return_t
callback_counter(memcached_st
*,
156 memcached_result_st
*,
159 unsigned int *counter
= static_cast<unsigned int *>(context
);
161 *counter
= *counter
+ 1;
163 return MEMCACHED_SUCCESS
;
166 test_return_t
mget_result_function(memcached_st
*memc
)
170 string
key1("fudge");
174 vector
< vector
<char> *> values
;
178 populate_vector(val1
, key1
);
179 populate_vector(val2
, key2
);
180 populate_vector(val3
, key3
);
182 keys
.push_back(key1
);
183 keys
.push_back(key2
);
184 keys
.push_back(key3
);
186 values
.push_back(&val1
);
187 values
.push_back(&val2
);
188 values
.push_back(&val3
);
189 unsigned int counter
;
190 memcached_execute_fn callbacks
[1];
192 /* We need to empty the server before we continue the test */
194 rc
= mc
.setAll(keys
, values
, 50, 9);
200 callbacks
[0]= &callback_counter
;
202 rc
= mc
.fetchExecute(callbacks
, static_cast<void *>(&counter
), 1);
204 assert(counter
== 3);
209 test_return_t
mget_test(memcached_st
*memc
)
213 memcached_return_t mc_rc
;
215 vector
< vector
<char> *> values
;
217 keys
.push_back("fudge");
218 keys
.push_back("son");
219 keys
.push_back("food");
223 populate_vector(val1
, "fudge");
224 populate_vector(val2
, "son");
225 populate_vector(val3
, "food");
227 values
.push_back(&val1
);
228 values
.push_back(&val2
);
229 values
.push_back(&val3
);
232 vector
<char> return_value
;
234 /* We need to empty the server before we continue the test */
241 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
243 assert(return_value
.size() != 0);
244 return_value
.clear();
246 assert(mc_rc
== MEMCACHED_END
);
248 rc
= mc
.setAll(keys
, values
, 50, 9);
254 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
256 assert(return_key
.length() == return_value
.size());
257 assert(!memcmp(&return_value
[0], return_key
.c_str(), return_value
.size()));
263 test_return_t
basic_behavior(memcached_st
*memc
)
268 rc
= mc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, value
);
270 uint64_t behavior
= mc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
);
271 assert(behavior
== value
);
277 { "basic", 0, basic_test
},
278 { "basic_master_key", 0, basic_master_key_test
},
279 { "increment_test", 0, increment_test
},
280 { "mget", 1, mget_test
},
281 { "mget_result_function", 1, mget_result_function
},
282 { "basic_behavior", 0, basic_behavior
},
286 collection_st collection
[] ={
287 {"block", 0, 0, tests
},
291 #define SERVERS_TO_CREATE 1
293 extern "C" void *world_create(void)
295 server_startup_st
*construct
;
297 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
298 memset(construct
, 0, sizeof(server_startup_st
));
300 construct
->count
= SERVERS_TO_CREATE
;
301 server_startup(construct
);
306 void world_destroy(void *p
)
308 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
309 memcached_server_st
*servers
=
310 static_cast<memcached_server_st
*>(construct
->servers
);
311 memcached_server_list_free(servers
);
313 server_shutdown(construct
);
317 void get_world(world_st
*world
)
319 world
->collections
= collection
;
320 world
->create
= world_create
;
321 world
->destroy
= world_destroy
;