2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Sample test application.
15 #include "libmemcached/common.h"
21 #include <sys/types.h>
26 #include "../clients/generator.h"
27 #include "../clients/execute.h"
30 #define INT64_MAX LONG_MAX
33 #define INT32_MAX INT_MAX
39 /* Number of items generated for tests */
40 #define GLOBAL_COUNT 100000
42 /* Number of times to run the test loop */
43 #define TEST_COUNTER 500000
44 static uint32_t global_count
;
46 static pairs_st
*global_pairs
;
47 static char *global_keys
[GLOBAL_COUNT
];
48 static size_t global_keys_length
[GLOBAL_COUNT
];
50 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
52 pairs_free(global_pairs
);
57 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
59 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
60 global_count
= GLOBAL_COUNT
;
62 for (size_t x
= 0; x
< global_count
; x
++)
64 global_keys
[x
]= global_pairs
[x
].key
;
65 global_keys_length
[x
]= global_pairs
[x
].key_length
;
71 static test_return_t
drizzle(memcached_st
*memc
)
73 memcached_return_t rc
;
75 size_t return_value_length
;
79 for (size_t x
= 0; x
< TEST_COUNTER
; x
++)
84 test_bit
= (uint32_t)(random() % GLOBAL_COUNT
);
85 which
= (uint8_t)(random() % 2);
89 return_value
= memcached_get(memc
, global_keys
[test_bit
], global_keys_length
[test_bit
],
90 &return_value_length
, &flags
, &rc
);
91 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
95 else if (rc
== MEMCACHED_NOTFOUND
)
101 WATCHPOINT_ERROR(rc
);
102 WATCHPOINT_ASSERT(rc
);
107 rc
= memcached_set(memc
, global_pairs
[test_bit
].key
,
108 global_pairs
[test_bit
].key_length
,
109 global_pairs
[test_bit
].value
,
110 global_pairs
[test_bit
].value_length
,
112 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
114 WATCHPOINT_ERROR(rc
);
115 WATCHPOINT_ASSERT(0);
120 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
126 static test_return_t
pre_nonblock(memcached_st
*memc
)
128 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
134 Set the value, then quit to make sure it is flushed.
135 Come back in and test that add fails.
137 static test_return_t
add_test(memcached_st
*memc
)
139 memcached_return_t rc
;
140 const char *key
= "foo";
141 const char *value
= "when we sanitize";
142 unsigned long long setting_value
;
144 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
146 rc
= memcached_set(memc
, key
, strlen(key
),
147 value
, strlen(value
),
148 (time_t)0, (uint32_t)0);
149 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
150 memcached_quit(memc
);
151 rc
= memcached_add(memc
, key
, strlen(key
),
152 value
, strlen(value
),
153 (time_t)0, (uint32_t)0);
155 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
158 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
162 test_true(rc
== MEMCACHED_NOTSTORED
);
169 * repeating add_tests many times
170 * may show a problem in timing
172 static test_return_t
many_adds(memcached_st
*memc
)
174 for (size_t x
= 0; x
< TEST_COUNTER
; x
++)
181 test_st smash_tests
[] ={
182 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
183 {"drizzle", 1, (test_callback_fn
)drizzle
},
184 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
185 {"many_adds", 1, (test_callback_fn
)many_adds
},
190 collection_st collection
[] ={
191 {"smash", 0, 0, smash_tests
},
192 {"smash_nonblock", (test_callback_fn
)pre_nonblock
, 0, smash_tests
},
197 #define SERVERS_TO_CREATE 5
199 #include "libmemcached_world.h"
201 void get_world(world_st
*world
)
203 world
->collections
= collection
;
205 world
->create
= (test_callback_create_fn
)world_create
;
206 world
->destroy
= (test_callback_fn
)world_destroy
;
208 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
209 world
->test
.flush
= (test_callback_fn
)world_flush
;
210 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
211 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
212 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
214 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
215 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
217 world
->runner
= &defualt_libmemcached_runner
;