4 #include <libmemcached/memcached.hpp>
10 #include <sys/types.h>
15 #include <libtest/server.h>
17 #include <libtest/test.h>
23 using namespace memcache
;
26 test_return_t
basic_test(memcached_st
*memc
);
27 test_return_t
increment_test(memcached_st
*memc
);
28 test_return_t
basic_master_key_test(memcached_st
*memc
);
29 test_return_t
mget_result_function(memcached_st
*memc
);
30 test_return_t
basic_behavior(memcached_st
*memc
);
31 test_return_t
mget_test(memcached_st
*memc
);
32 memcached_return_t
callback_counter(const memcached_st
*,
33 memcached_result_st
*,
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_t
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 test_true(foo
.set("mine", value
, 0, 0));
62 test_true(foo
.get("mine", test_value
));
64 test_memcmp(&test_value
[0], &value
[0], test_value
.size());
65 test_false(foo
.set("", value
, 0, 0));
70 test_return_t
increment_test(memcached_st
*original
)
72 Memcache
mcach(original
);
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 test_true(mcach
.set(key
, inc_val
, 0, 0));
85 test_true(mcach
.get(key
, ret_value
));
86 test_false(ret_value
.empty());
87 copy_vec_to_string(ret_value
, ret_string
);
89 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
90 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
91 test_compare(int_inc_value
, int_ret_value
);
93 test_true(mcach
.increment(key
, 1, &int_ret_value
));
94 test_compare(2, int_ret_value
);
96 test_true(mcach
.increment(key
, 1, &int_ret_value
));
97 test_compare(3, int_ret_value
);
99 test_true(mcach
.increment(key
, 5, &int_ret_value
));
100 test_compare(8, int_ret_value
);
105 test_return_t
basic_master_key_test(memcached_st
*original
)
107 Memcache
foo(original
);
108 const string
value_set("Data for server A");
110 vector
<char> test_value
;
111 const string
master_key_a("server-a");
112 const string
master_key_b("server-b");
113 const string
key("xyz");
115 populate_vector(value
, value_set
);
117 foo
.setByKey(master_key_a
, key
, value
, 0, 0);
118 foo
.getByKey(master_key_a
, key
, test_value
);
120 test_true((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
124 foo
.getByKey(master_key_b
, key
, test_value
);
125 test_true((memcmp(&value
[0], &test_value
[0], value
.size()) == 0));
130 /* Count the results */
131 memcached_return_t
callback_counter(const memcached_st
*,
132 memcached_result_st
*,
135 unsigned int *counter
= static_cast<unsigned int *>(context
);
137 *counter
= *counter
+ 1;
139 return MEMCACHED_SUCCESS
;
142 test_return_t
mget_test(memcached_st
*original
)
144 Memcache
memc(original
);
145 memcached_return_t mc_rc
;
147 vector
< vector
<char> *> values
;
149 keys
.push_back("fudge");
150 keys
.push_back("son");
151 keys
.push_back("food");
155 populate_vector(val1
, "fudge");
156 populate_vector(val2
, "son");
157 populate_vector(val3
, "food");
159 values
.push_back(&val1
);
160 values
.push_back(&val2
);
161 values
.push_back(&val3
);
164 vector
<char> return_value
;
166 /* We need to empty the server before we continue the test */
167 test_true(memc
.flush(0));
169 test_true(memc
.mget(keys
));
171 while ((mc_rc
= memc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
173 test_true(return_value
.size());
174 return_value
.clear();
176 test_compare(mc_rc
, MEMCACHED_END
);
178 test_true(memc
.setAll(keys
, values
, 50, 9));
180 test_true(memc
.mget(keys
));
182 while ((mc_rc
= memc
.fetch(return_key
, return_value
)) != MEMCACHED_END
)
184 test_compare(return_key
.length(), return_value
.size());
185 test_memcmp(&return_value
[0], return_key
.c_str(), return_value
.size());
191 test_return_t
basic_behavior(memcached_st
*original
)
193 Memcache
memc(original
);
195 test_true(memc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, value
));
196 uint64_t behavior
= memc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
);
197 test_compare(behavior
, value
);
204 reinterpret_cast<test_callback_fn
>(basic_test
) },
205 { "basic_master_key", 0,
206 reinterpret_cast<test_callback_fn
>(basic_master_key_test
) },
207 { "increment_test", 0,
208 reinterpret_cast<test_callback_fn
>(increment_test
) },
210 reinterpret_cast<test_callback_fn
>(mget_test
) },
211 { "basic_behavior", 0,
212 reinterpret_cast<test_callback_fn
>(basic_behavior
) },
216 collection_st collection
[] ={
217 {"block", 0, 0, tests
},
221 #define SERVERS_TO_CREATE 5
223 #include "libmemcached_world.h"
225 void get_world(world_st
*world
)
227 world
->collections
= collection
;
229 world
->create
= reinterpret_cast<test_callback_create_fn
>(world_create
);
230 world
->destroy
= reinterpret_cast<test_callback_fn
>(world_destroy
);
232 world
->test
.startup
= reinterpret_cast<test_callback_fn
>(world_test_startup
);
233 world
->test
.flush
= reinterpret_cast<test_callback_fn
>(world_flush
);
234 world
->test
.pre_run
= reinterpret_cast<test_callback_fn
>(world_pre_run
);
235 world
->test
.post_run
= reinterpret_cast<test_callback_fn
>(world_post_run
);
236 world
->test
.on_error
= reinterpret_cast<test_callback_error_fn
>(world_on_error
);
238 world
->collection
.startup
= reinterpret_cast<test_callback_fn
>(world_container_startup
);
239 world
->collection
.shutdown
= reinterpret_cast<test_callback_fn
>(world_container_shutdown
);
241 world
->runner
= &defualt_libmemcached_runner
;