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 test_return
basic_test(memcached_st
*memc
)
39 const string
value_set("This is some data");
43 foo
.set("mine", value_set
, 0, 0);
44 value
= foo
.get("mine", &value_length
);
46 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
51 test_return
increment_test(memcached_st
*memc
)
53 Memcached
mcach(memc
);
55 const string
key("inctest");
56 const string
inc_value("1");
58 uint64_t int_inc_value
;
59 uint64_t int_ret_value
;
62 mcach
.set(key
, inc_value
, 0, 0);
63 ret_value
= mcach
.get(key
, &value_length
);
64 printf("\nretvalue %s\n",ret_value
.c_str());
65 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
66 int_ret_value
= uint64_t(atol(ret_value
.c_str()));
67 assert(int_ret_value
== int_inc_value
);
69 rc
= mcach
.increment(key
, 1, &int_ret_value
);
71 assert(int_ret_value
== 2);
73 rc
= mcach
.increment(key
, 1, &int_ret_value
);
75 assert(int_ret_value
== 3);
77 rc
= mcach
.increment(key
, 5, &int_ret_value
);
79 assert(int_ret_value
== 8);
84 test_return
basic_master_key_test(memcached_st
*memc
)
87 const string
value_set("Data for server A");
88 const string
master_key_a("server-a");
89 const string
master_key_b("server-b");
90 const string
key("xyz");
94 foo
.set_by_key(master_key_a
, key
, value_set
, 0, 0);
95 value
= foo
.get_by_key(master_key_a
, key
, &value_length
);
97 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
99 value
= foo
.get_by_key(master_key_b
, key
, &value_length
);
100 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
105 /* Count the results */
106 memcached_return
callback_counter(memcached_st
*,
107 memcached_result_st
*,
110 unsigned int *counter
= static_cast<unsigned int *>(context
);
112 *counter
= *counter
+ 1;
114 return MEMCACHED_SUCCESS
;
117 test_return
mget_result_function(memcached_st
*memc
)
121 string
key1("fudge");
126 keys
.push_back(key1
);
127 keys
.push_back(key2
);
128 keys
.push_back(key3
);
129 unsigned int counter
;
130 memcached_execute_function callbacks
[1];
132 /* We need to empty the server before we continue the test */
134 rc
= mc
.set_all(keys
, keys
, 50, 9);
140 callbacks
[0]= &callback_counter
;
142 rc
= mc
.fetch_execute(callbacks
, static_cast<void *>(&counter
), 1);
144 assert(counter
== 3);
149 test_return
mget_test(memcached_st
*memc
)
153 memcached_return mc_rc
;
156 keys
.push_back("fudge");
157 keys
.push_back("son");
158 keys
.push_back("food");
162 size_t return_key_length
;
164 size_t return_value_length
;
166 /* We need to empty the server before we continue the test */
173 while (mc
.fetch(return_key
, return_value
, &return_key_length
,
174 &return_value_length
, &flags
, &mc_rc
))
176 assert(return_value
.length() != 0);
178 assert(return_value_length
== 0);
179 assert(mc_rc
== MEMCACHED_END
);
181 rc
= mc
.set_all(keys
, keys
, 50, 9);
187 while ((mc
.fetch(return_key
, return_value
, &return_key_length
,
188 &return_value_length
, &flags
, &mc_rc
)))
190 assert(return_value
.length() != 0);
191 assert(mc_rc
== MEMCACHED_SUCCESS
);
192 assert(return_key_length
== return_value_length
);
193 assert(!memcmp(return_value
.c_str(), return_key
.c_str(), return_value_length
));
200 { "basic", 0, basic_test
},
201 { "basic_master_key", 0, basic_master_key_test
},
202 { "increment_test", 0, increment_test
},
203 { "mget", 1, mget_test
},
204 { "mget_result_function", 1, mget_result_function
},
208 collection_st collection
[] ={
209 {"block", 0, 0, tests
},
213 #define SERVERS_TO_CREATE 1
215 extern "C" void *world_create(void)
217 server_startup_st
*construct
;
219 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
220 memset(construct
, 0, sizeof(server_startup_st
));
222 construct
->count
= SERVERS_TO_CREATE
;
223 server_startup(construct
);
228 void world_destroy(void *p
)
230 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
231 memcached_server_st
*servers
=
232 static_cast<memcached_server_st
*>(construct
->servers
);
233 memcached_server_list_free(servers
);
235 server_shutdown(construct
);
239 void get_world(world_st
*world
)
241 world
->collections
= collection
;
242 world
->create
= world_create
;
243 world
->destroy
= world_destroy
;