4 #include <libmemcached/memcached.hpp>
10 #include <sys/types.h>
15 #include <libtest/server.h>
17 #include <libtest/test.hpp>
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 test_true(foo
.setByKey(master_key_a
, key
, value
, 0, 0));
118 test_true(foo
.getByKey(master_key_a
, key
, test_value
));
120 test_compare(value
.size(), test_value
.size());
121 test_memcmp(&value
[0], &test_value
[0], value
.size());
125 test_false(foo
.getByKey(master_key_b
, key
, test_value
));
126 test_compare(0, test_value
.size());
131 /* Count the results */
132 memcached_return_t
callback_counter(const memcached_st
*,
133 memcached_result_st
*,
136 unsigned int *counter
= static_cast<unsigned int *>(context
);
138 *counter
= *counter
+1;
140 return MEMCACHED_SUCCESS
;
143 test_return_t
mget_test(memcached_st
*original
)
145 Memcache
memc(original
);
146 memcached_return_t mc_rc
;
148 vector
< vector
<char> *> values
;
150 keys
.push_back("fudge");
151 keys
.push_back("son");
152 keys
.push_back("food");
156 populate_vector(val1
, "fudge");
157 populate_vector(val2
, "son");
158 populate_vector(val3
, "food");
160 values
.push_back(&val1
);
161 values
.push_back(&val2
);
162 values
.push_back(&val3
);
165 vector
<char> return_value
;
167 /* We need to empty the server before we continue the test */
168 test_true(memc
.flush());
170 test_true(memc
.mget(keys
));
172 test_compare(MEMCACHED_NOTFOUND
,
173 memc
.fetch(return_key
, return_value
));
175 test_true(memc
.setAll(keys
, values
, 50, 9));
177 test_true(memc
.mget(keys
));
179 while (memcached_success(mc_rc
= memc
.fetch(return_key
, return_value
)))
181 test_compare(return_key
.length(), return_value
.size());
182 test_memcmp(&return_value
[0], return_key
.c_str(), return_value
.size());
185 test_compare(values
.size(), count
);
190 test_return_t
basic_behavior(memcached_st
*original
)
192 Memcache
memc(original
);
194 test_true(memc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, value
));
195 uint64_t behavior
= memc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
);
196 test_compare(behavior
, value
);
203 reinterpret_cast<test_callback_fn
*>(basic_test
) },
204 { "basic_master_key", 0,
205 reinterpret_cast<test_callback_fn
*>(basic_master_key_test
) },
206 { "increment_test", 0,
207 reinterpret_cast<test_callback_fn
*>(increment_test
) },
209 reinterpret_cast<test_callback_fn
*>(mget_test
) },
210 { "basic_behavior", 0,
211 reinterpret_cast<test_callback_fn
*>(basic_behavior
) },
215 collection_st collection
[] ={
216 {"block", 0, 0, tests
},
220 #define SERVERS_TO_CREATE 5
222 #include "libmemcached_world.h"
224 void get_world(Framework
*world
)
226 world
->collections
= collection
;
228 world
->_create
= reinterpret_cast<test_callback_create_fn
*>(world_create
);
229 world
->_destroy
= reinterpret_cast<test_callback_fn
*>(world_destroy
);
231 world
->item
._startup
= reinterpret_cast<test_callback_fn
*>(world_test_startup
);
232 world
->item
._flush
= reinterpret_cast<test_callback_fn
*>(world_flush
);
233 world
->item
.set_pre(reinterpret_cast<test_callback_fn
*>(world_pre_run
));
234 world
->item
.set_post(reinterpret_cast<test_callback_fn
*>(world_post_run
));
235 world
->_on_error
= reinterpret_cast<test_callback_error_fn
*>(world_on_error
);
237 world
->collection_startup
= reinterpret_cast<test_callback_fn
*>(world_container_startup
);
238 world
->collection_shutdown
= reinterpret_cast<test_callback_fn
*>(world_container_shutdown
);
240 world
->runner
= &defualt_libmemcached_runner
;