4 #include "libmemcached/memcached.h"
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 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 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 foo
.getByKey(master_key_a
, key
, test_value
);
129 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
133 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
);
217 vector
<char> return_value
;
219 /* We need to empty the server before we continue the test */
226 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
228 assert(return_value
.size() != 0);
229 return_value
.clear();
231 assert(mc_rc
== MEMCACHED_END
);
233 rc
= mc
.setAll(keys
, values
, 50, 9);
239 while ((mc_rc
= mc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
241 assert(return_key
.length() == return_value
.size());
242 assert(!memcmp(&return_value
[0], return_key
.c_str(), return_value
.size()));
249 { "basic", 0, basic_test
},
250 { "basic_master_key", 0, basic_master_key_test
},
251 { "increment_test", 0, increment_test
},
252 { "mget", 1, mget_test
},
253 { "mget_result_function", 1, mget_result_function
},
257 collection_st collection
[] ={
258 {"block", 0, 0, tests
},
262 #define SERVERS_TO_CREATE 1
264 extern "C" void *world_create(void)
266 server_startup_st
*construct
;
268 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
269 memset(construct
, 0, sizeof(server_startup_st
));
271 construct
->count
= SERVERS_TO_CREATE
;
272 server_startup(construct
);
277 void world_destroy(void *p
)
279 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
280 memcached_server_st
*servers
=
281 static_cast<memcached_server_st
*>(construct
->servers
);
282 memcached_server_list_free(servers
);
284 server_shutdown(construct
);
288 void get_world(world_st
*world
)
290 world
->collections
= collection
;
291 world
->create
= world_create
;
292 world
->destroy
= world_destroy
;