4 #include "libmemcached/memcached.hpp"
11 #include <sys/types.h>
23 using namespace memcache
;
26 test_return
basic_test(memcached_st
*memc
);
27 test_return
increment_test(memcached_st
*memc
);
28 test_return
basic_master_key_test(memcached_st
*memc
);
29 test_return
mget_result_function(memcached_st
*memc
);
30 test_return
mget_test(memcached_st
*memc
);
31 memcached_return
callback_counter(memcached_st
*,
32 memcached_result_st
*,
34 void *world_create(void);
35 void world_destroy(void *p
);
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
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
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
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
callback_counter(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
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_function 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
mget_test(memcached_st
*memc
)
212 memcached_return 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()));
263 { "basic", 0, basic_test
},
264 { "basic_master_key", 0, basic_master_key_test
},
265 { "increment_test", 0, increment_test
},
266 { "mget", 1, mget_test
},
267 { "mget_result_function", 1, mget_result_function
},
271 collection_st collection
[] ={
272 {"block", 0, 0, tests
},
276 #define SERVERS_TO_CREATE 1
278 extern "C" void *world_create(void)
280 server_startup_st
*construct
;
282 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
283 memset(construct
, 0, sizeof(server_startup_st
));
285 construct
->count
= SERVERS_TO_CREATE
;
286 server_startup(construct
);
291 void world_destroy(void *p
)
293 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
294 memcached_server_st
*servers
=
295 static_cast<memcached_server_st
*>(construct
->servers
);
296 memcached_server_list_free(servers
);
298 server_shutdown(construct
);
302 void get_world(world_st
*world
)
304 world
->collections
= collection
;
305 world
->create
= world_create
;
306 world
->destroy
= world_destroy
;