4 #include "libmemcached/memcached.hh"
11 #include <sys/types.h>
24 void *world_create(void);
25 void world_destroy(void *p
);
28 static test_return
basic_test(memcached_st
*memc
)
31 const string
value_set("This is some data");
35 foo
.set("mine", value_set
, 0, 0);
36 value
= foo
.get("mine", &value_length
);
38 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
43 static test_return
increment_test(memcached_st
*memc
)
45 Memcached
mcach(memc
);
47 const string
key("inctest");
48 const string
inc_value("1");
50 uint64_t int_inc_value
;
51 uint64_t int_ret_value
;
54 mcach
.set(key
, inc_value
, 0, 0);
55 ret_value
= mcach
.get(key
, &value_length
);
56 printf("\nretvalue %s\n",ret_value
.c_str());
57 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
58 int_ret_value
= uint64_t(atol(ret_value
.c_str()));
59 assert(int_ret_value
== int_inc_value
);
61 rc
= mcach
.increment(key
, 1, &int_ret_value
);
63 assert(int_ret_value
== 2);
65 rc
= mcach
.increment(key
, 1, &int_ret_value
);
67 assert(int_ret_value
== 3);
69 rc
= mcach
.increment(key
, 5, &int_ret_value
);
71 assert(int_ret_value
== 8);
76 static test_return
basic_master_key_test(memcached_st
*memc
)
79 const string
value_set("Data for server A");
80 const string
master_key_a("server-a");
81 const string
master_key_b("server-b");
82 const string
key("xyz");
86 foo
.set_by_key(master_key_a
, key
, value_set
, 0, 0);
87 value
= foo
.get_by_key(master_key_a
, key
, &value_length
);
89 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
91 value
= foo
.get_by_key(master_key_b
, key
, &value_length
);
92 assert((memcmp(value
.c_str(), value_set
.c_str(), value_length
) == 0));
97 /* Count the results */
98 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
99 memcached_result_st
*result
__attribute__((unused
)),
102 unsigned int *counter
= static_cast<unsigned int *>(context
);
104 *counter
= *counter
+ 1;
106 return MEMCACHED_SUCCESS
;
109 static test_return
mget_result_function(memcached_st
*memc
)
113 string
key1("fudge");
118 keys
.push_back(key1
);
119 keys
.push_back(key2
);
120 keys
.push_back(key3
);
121 unsigned int counter
;
122 memcached_execute_function callbacks
[1];
124 /* We need to empty the server before we continue the test */
126 rc
= mc
.set_all(keys
, keys
, 50, 9);
132 callbacks
[0]= &callback_counter
;
134 rc
= mc
.fetch_execute(callbacks
, static_cast<void *>(&counter
), 1);
136 assert(counter
== 3);
141 static test_return
mget_test(memcached_st
*memc
)
145 memcached_return mc_rc
;
148 keys
.push_back("fudge");
149 keys
.push_back("son");
150 keys
.push_back("food");
154 size_t return_key_length
;
156 size_t return_value_length
;
158 /* We need to empty the server before we continue the test */
165 while (mc
.fetch(return_key
, return_value
, &return_key_length
,
166 &return_value_length
, &flags
, &mc_rc
))
168 assert(return_value
.length() != 0);
170 assert(return_value_length
== 0);
171 assert(mc_rc
== MEMCACHED_END
);
173 rc
= mc
.set_all(keys
, keys
, 50, 9);
179 while ((mc
.fetch(return_key
, return_value
, &return_key_length
,
180 &return_value_length
, &flags
, &mc_rc
)))
182 assert(return_value
.length() != 0);
183 assert(mc_rc
== MEMCACHED_SUCCESS
);
184 assert(return_key_length
== return_value_length
);
185 assert(!memcmp(return_value
.c_str(), return_key
.c_str(), return_value_length
));
192 { "basic", 0, basic_test
},
193 { "basic_master_key", 0, basic_master_key_test
},
194 { "increment_test", 0, increment_test
},
195 { "mget", 1, mget_test
},
196 { "mget_result_function", 1, mget_result_function
},
200 collection_st collection
[] ={
201 {"block", 0, 0, tests
},
205 #define SERVERS_TO_CREATE 1
207 extern "C" void *world_create(void)
209 server_startup_st
*construct
;
211 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
212 memset(construct
, 0, sizeof(server_startup_st
));
214 construct
->count
= SERVERS_TO_CREATE
;
215 server_startup(construct
);
220 void world_destroy(void *p
)
222 server_startup_st
*construct
= static_cast<server_startup_st
*>(p
);
223 memcached_server_st
*servers
=
224 static_cast<memcached_server_st
*>(construct
->servers
);
225 memcached_server_list_free(servers
);
227 server_shutdown(construct
);
231 void get_world(world_st
*world
)
233 world
->collections
= collection
;
234 world
->create
= world_create
;
235 world
->destroy
= world_destroy
;