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 void *world_create(void);
30 void world_destroy(void *p
);
33 test_return
basic_test(memcached_st
*memc
)
36 const string
value_set("This is some data");
40 foo
.set("mine", value_set
, 0, 0);
41 value
= foo
.get("mine", &value_length
);
43 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
48 test_return
increment_test(memcached_st
*memc
)
50 Memcached
mcach(memc
);
52 const string
key("inctest");
53 const string
inc_value("1");
55 uint64_t int_inc_value
;
56 uint64_t int_ret_value
;
59 mcach
.set(key
, inc_value
, 0, 0);
60 ret_value
= mcach
.get(key
, &value_length
);
61 printf("\nretvalue %s\n",ret_value
.c_str());
62 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
63 int_ret_value
= uint64_t(atol(ret_value
.c_str()));
64 assert(int_ret_value
== int_inc_value
);
66 rc
= mcach
.increment(key
, 1, &int_ret_value
);
68 assert(int_ret_value
== 2);
70 rc
= mcach
.increment(key
, 1, &int_ret_value
);
72 assert(int_ret_value
== 3);
74 rc
= mcach
.increment(key
, 5, &int_ret_value
);
76 assert(int_ret_value
== 8);
81 test_return
basic_master_key_test(memcached_st
*memc
)
84 const string
value_set("Data for server A");
85 const string
master_key_a("server-a");
86 const string
master_key_b("server-b");
87 const string
key("xyz");
91 foo
.set_by_key(master_key_a
, key
, value_set
, 0, 0);
92 value
= foo
.get_by_key(master_key_a
, key
, &value_length
);
94 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
96 value
= foo
.get_by_key(master_key_b
, key
, &value_length
);
97 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
102 /* Count the results */
103 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
104 memcached_result_st
*result
__attribute__((unused
)),
107 unsigned int *counter
= static_cast<unsigned int *>(context
);
109 *counter
= *counter
+ 1;
111 return MEMCACHED_SUCCESS
;
114 test_return
mget_result_function(memcached_st
*memc
)
118 string
key1("fudge");
123 keys
.push_back(key1
);
124 keys
.push_back(key2
);
125 keys
.push_back(key3
);
126 unsigned int counter
;
127 memcached_execute_function callbacks
[1];
129 /* We need to empty the server before we continue the test */
131 rc
= mc
.set_all(keys
, keys
, 50, 9);
137 callbacks
[0]= &callback_counter
;
139 rc
= mc
.fetch_execute(callbacks
, static_cast<void *>(&counter
), 1);
141 assert(counter
== 3);
146 test_return
mget_test(memcached_st
*memc
)
150 memcached_return mc_rc
;
153 keys
.push_back("fudge");
154 keys
.push_back("son");
155 keys
.push_back("food");
159 size_t return_key_length
;
161 size_t return_value_length
;
163 /* We need to empty the server before we continue the test */
170 while (mc
.fetch(return_key
, return_value
, &return_key_length
,
171 &return_value_length
, &flags
, &mc_rc
))
173 assert(return_value
.length() != 0);
175 assert(return_value_length
== 0);
176 assert(mc_rc
== MEMCACHED_END
);
178 rc
= mc
.set_all(keys
, keys
, 50, 9);
184 while ((mc
.fetch(return_key
, return_value
, &return_key_length
,
185 &return_value_length
, &flags
, &mc_rc
)))
187 assert(return_value
.length() != 0);
188 assert(mc_rc
== MEMCACHED_SUCCESS
);
189 assert(return_key_length
== return_value_length
);
190 assert(!memcmp(return_value
.c_str(), return_key
.c_str(), return_value_length
));
197 { "basic", 0, basic_test
},
198 { "basic_master_key", 0, basic_master_key_test
},
199 { "increment_test", 0, increment_test
},
200 { "mget", 1, mget_test
},
201 { "mget_result_function", 1, mget_result_function
},
205 collection_st collection
[] ={
206 {"block", 0, 0, tests
},
210 #define SERVERS_TO_CREATE 1
212 extern "C" void *world_create(void)
214 server_startup_st
*construct
;
216 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
217 memset(construct
, 0, sizeof(server_startup_st
));
219 construct
->count
= SERVERS_TO_CREATE
;
220 server_startup(construct
);
225 void world_destroy(void *p
)
227 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
228 memcached_server_st
*servers
=
229 static_cast<memcached_server_st
*>(construct
->servers
);
230 memcached_server_list_free(servers
);
232 server_shutdown(construct
);
236 void get_world(world_st
*world
)
238 world
->collections
= collection
;
239 world
->create
= world_create
;
240 world
->destroy
= world_destroy
;