4 #include "libmemcached/memcached.hpp"
11 #include <sys/types.h>
22 using namespace memcache
;
25 test_return
basic_test(memcached_st
*memc
);
26 test_return
increment_test(memcached_st
*memc
);
27 test_return
basic_master_key_test(memcached_st
*memc
);
28 test_return
mget_result_function(memcached_st
*memc
);
29 test_return
mget_test(memcached_st
*memc
);
30 memcached_return
callback_counter(memcached_st
*,
31 memcached_result_st
*,
33 void *world_create(void);
34 void world_destroy(void *p
);
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
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 test_value
= foo
.get("mine", test_value
);
64 assert((memcmp(&test_value
[0], &value
[0], test_value
.size()) == 0));
69 test_return
increment_test(memcached_st
*memc
)
73 const string
key("blah");
74 const string
inc_value("1");
75 std::vector
<char> inc_val
;
76 vector
<char> ret_value
;
78 uint64_t int_inc_value
;
79 uint64_t int_ret_value
;
81 populate_vector(inc_val
, inc_value
);
83 rc
= mcach
.set(key
, inc_val
, 0, 0);
88 ret_value
= mcach
.get(key
, ret_value
);
89 if (ret_value
.empty())
93 copy_vec_to_string(ret_value
, ret_string
);
95 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
96 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
97 assert(int_ret_value
== int_inc_value
);
99 rc
= mcach
.increment(key
, 1, &int_ret_value
);
101 assert(int_ret_value
== 2);
103 rc
= mcach
.increment(key
, 1, &int_ret_value
);
105 assert(int_ret_value
== 3);
107 rc
= mcach
.increment(key
, 5, &int_ret_value
);
109 assert(int_ret_value
== 8);
114 test_return
basic_master_key_test(memcached_st
*memc
)
117 const string
value_set("Data for server A");
119 vector
<char> test_value
;
120 const string
master_key_a("server-a");
121 const string
master_key_b("server-b");
122 const string
key("xyz");
124 populate_vector(value
, value_set
);
126 foo
.setByKey(master_key_a
, key
, value
, 0, 0);
127 test_value
= foo
.getByKey(master_key_a
, key
, test_value
);
129 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
133 test_value
= foo
.getByKey(master_key_b
, key
, test_value
);
134 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
139 /* Count the results */
140 memcached_return
callback_counter(memcached_st
*,
141 memcached_result_st
*,
144 unsigned int *counter
= static_cast<unsigned int *>(context
);
146 *counter
= *counter
+ 1;
148 return MEMCACHED_SUCCESS
;
151 test_return
mget_result_function(memcached_st
*memc
)
155 string
key1("fudge");
159 vector
< vector
<char> *> values
;
163 populate_vector(val1
, key1
);
164 populate_vector(val2
, key2
);
165 populate_vector(val3
, key3
);
167 keys
.push_back(key1
);
168 keys
.push_back(key2
);
169 keys
.push_back(key3
);
171 values
.push_back(&val1
);
172 values
.push_back(&val2
);
173 values
.push_back(&val3
);
174 unsigned int counter
;
175 memcached_execute_function callbacks
[1];
177 /* We need to empty the server before we continue the test */
179 rc
= mc
.setAll(keys
, values
, 50, 9);
185 callbacks
[0]= &callback_counter
;
187 rc
= mc
.fetchExecute(callbacks
, static_cast<void *>(&counter
), 1);
189 assert(counter
== 3);
194 test_return
mget_test(memcached_st
*memc
)
198 memcached_return mc_rc
;
200 vector
< vector
<char> *> values
;
202 keys
.push_back("fudge");
203 keys
.push_back("son");
204 keys
.push_back("food");
208 populate_vector(val1
, "fudge");
209 populate_vector(val2
, "son");
210 populate_vector(val3
, "food");
212 values
.push_back(&val1
);
213 values
.push_back(&val2
);
214 values
.push_back(&val3
);
218 vector
<char> return_value
;
220 /* We need to empty the server before we continue the test */
227 while (mc
.fetch(return_key
, return_value
,
230 assert(return_value
.size() != 0);
231 return_value
.clear();
233 assert(mc_rc
== MEMCACHED_END
);
235 rc
= mc
.setAll(keys
, values
, 50, 9);
241 while ((mc
.fetch(return_key
, return_value
,
244 assert(mc_rc
== MEMCACHED_SUCCESS
);
245 assert(return_key
.length() == return_value
.size());
246 assert(!memcmp(&return_value
[0], return_key
.c_str(), return_value
.size()));
253 { "basic", 0, basic_test
},
254 { "basic_master_key", 0, basic_master_key_test
},
255 { "increment_test", 0, increment_test
},
256 { "mget", 1, mget_test
},
257 { "mget_result_function", 1, mget_result_function
},
261 collection_st collection
[] ={
262 {"block", 0, 0, tests
},
266 #define SERVERS_TO_CREATE 1
268 extern "C" void *world_create(void)
270 server_startup_st
*construct
;
272 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
273 memset(construct
, 0, sizeof(server_startup_st
));
275 construct
->count
= SERVERS_TO_CREATE
;
276 server_startup(construct
);
281 void world_destroy(void *p
)
283 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
284 memcached_server_st
*servers
=
285 static_cast<memcached_server_st
*>(construct
->servers
);
286 memcached_server_list_free(servers
);
288 server_shutdown(construct
);
292 void get_world(world_st
*world
)
294 world
->collections
= collection
;
295 world
->create
= world_create
;
296 world
->destroy
= world_destroy
;