4 #include "libmemcached/memcached.hh"
11 #include <sys/types.h>
24 test_return
basic_test(memcached_st
*memc
);
25 test_return
increment_test(memcached_st
*memc
);
26 test_return
basic_master_key_test(memcached_st
*memc
);
27 test_return
mget_result_function(memcached_st
*memc
);
28 test_return
mget_test(memcached_st
*memc
);
29 memcached_return
callback_counter(memcached_st
*,
30 memcached_result_st
*,
32 void *world_create(void);
33 void world_destroy(void *p
);
36 static void populate_vector(vector
<char> &vec
, const string
&str
)
38 vec
.reserve(str
.length());
39 memcpy(&*vec
.begin(), str
.c_str(), str
.length());
42 static void copy_vec_to_string(vector
<char> &vec
, string
&str
)
45 char *tmp
= static_cast<char *>(malloc(vec
.size() * sizeof(char)));
48 memcpy(tmp
, &vec
[0], vec
.size());
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 test_value
= foo
.get("mine", test_value
);
65 assert((memcmp(&test_value
[0], &value
[0], test_value
.size()) == 0));
70 test_return
increment_test(memcached_st
*memc
)
72 Memcached
mcach(memc
);
74 const string
key("blah");
75 const string
inc_value("1");
76 std::vector
<char> inc_val
;
77 vector
<char> ret_value
;
79 uint64_t int_inc_value
;
80 uint64_t int_ret_value
;
82 populate_vector(inc_val
, inc_value
);
84 rc
= mcach
.set(key
, inc_val
, 0, 0);
89 ret_value
= mcach
.get(key
, ret_value
);
90 if (ret_value
.empty())
94 copy_vec_to_string(ret_value
, ret_string
);
96 printf("string is: %s\n", ret_string
.c_str());
98 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
99 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
100 assert(int_ret_value
== int_inc_value
);
102 rc
= mcach
.increment(key
, 1, &int_ret_value
);
104 assert(int_ret_value
== 2);
106 rc
= mcach
.increment(key
, 1, &int_ret_value
);
108 assert(int_ret_value
== 3);
110 rc
= mcach
.increment(key
, 5, &int_ret_value
);
112 assert(int_ret_value
== 8);
117 test_return
basic_master_key_test(memcached_st
*memc
)
120 const string
value_set("Data for server A");
122 vector
<char> test_value
;
123 const string
master_key_a("server-a");
124 const string
master_key_b("server-b");
125 const string
key("xyz");
127 populate_vector(value
, value_set
);
129 foo
.setByKey(master_key_a
, key
, value
, 0, 0);
130 test_value
= foo
.getByKey(master_key_a
, key
, test_value
);
132 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
136 test_value
= foo
.getByKey(master_key_b
, key
, test_value
);
137 assert((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
142 /* Count the results */
143 memcached_return
callback_counter(memcached_st
*,
144 memcached_result_st
*,
147 unsigned int *counter
= static_cast<unsigned int *>(context
);
149 *counter
= *counter
+ 1;
151 return MEMCACHED_SUCCESS
;
154 test_return
mget_result_function(memcached_st
*memc
)
158 string
key1("fudge");
162 vector
< vector
<char> > values
;
166 populate_vector(val1
, key1
);
167 populate_vector(val2
, key2
);
168 populate_vector(val3
, key3
);
170 keys
.push_back(key1
);
171 keys
.push_back(key2
);
172 keys
.push_back(key3
);
174 values
.push_back(val1
);
175 values
.push_back(val2
);
176 values
.push_back(val3
);
177 unsigned int counter
;
178 memcached_execute_function callbacks
[1];
180 /* We need to empty the server before we continue the test */
182 rc
= mc
.setAll(keys
, values
, 50, 9);
188 callbacks
[0]= &callback_counter
;
190 rc
= mc
.fetchExecute(callbacks
, static_cast<void *>(&counter
), 1);
192 assert(counter
== 3);
197 test_return
mget_test(memcached_st
*memc
)
201 memcached_return mc_rc
;
203 vector
< vector
<char> > values
;
205 keys
.push_back("fudge");
206 keys
.push_back("son");
207 keys
.push_back("food");
211 populate_vector(val1
, "fudge");
212 populate_vector(val2
, "son");
213 populate_vector(val3
, "food");
215 values
.push_back(val1
);
216 values
.push_back(val2
);
217 values
.push_back(val3
);
221 vector
<char> return_value
;
223 /* We need to empty the server before we continue the test */
230 while (mc
.fetch(return_key
, return_value
,
233 assert(return_value
.size() != 0);
234 return_value
.clear();
236 assert(mc_rc
== MEMCACHED_END
);
238 rc
= mc
.setAll(keys
, values
, 50, 9);
244 while ((mc
.fetch(return_key
, return_value
,
247 assert(mc_rc
== MEMCACHED_SUCCESS
);
248 assert(return_key
.length() == return_value
.size());
249 assert(!memcmp(&return_value
[0], return_key
.c_str(), return_value
.size()));
256 { "basic", 0, basic_test
},
257 { "basic_master_key", 0, basic_master_key_test
},
258 { "increment_test", 0, increment_test
},
259 { "mget", 1, mget_test
},
260 { "mget_result_function", 1, mget_result_function
},
264 collection_st collection
[] ={
265 {"block", 0, 0, tests
},
269 #define SERVERS_TO_CREATE 1
271 extern "C" void *world_create(void)
273 server_startup_st
*construct
;
275 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
276 memset(construct
, 0, sizeof(server_startup_st
));
278 construct
->count
= SERVERS_TO_CREATE
;
279 server_startup(construct
);
284 void world_destroy(void *p
)
286 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
287 memcached_server_st
*servers
=
288 static_cast<memcached_server_st
*>(construct
->servers
);
289 memcached_server_list_free(servers
);
291 server_shutdown(construct
);
295 void get_world(world_st
*world
)
297 world
->collections
= collection
;
298 world
->create
= world_create
;
299 world
->destroy
= world_destroy
;