Update for test system.
[awesomized/libmemcached] / tests / atomsmasher.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /*
13 Sample test application.
14 */
15 #include "libmemcached/common.h"
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include "server.h"
27 #include "../clients/generator.h"
28 #include "../clients/execute.h"
29
30 #ifndef INT64_MAX
31 #define INT64_MAX LONG_MAX
32 #endif
33 #ifndef INT32_MAX
34 #define INT32_MAX INT_MAX
35 #endif
36
37
38 #include "test.h"
39
40 /* Number of items generated for tests */
41 #define GLOBAL_COUNT 100000
42
43 /* Number of times to run the test loop */
44 #define TEST_COUNTER 500000
45 static uint32_t global_count;
46
47 static pairs_st *global_pairs;
48 static char *global_keys[GLOBAL_COUNT];
49 static size_t global_keys_length[GLOBAL_COUNT];
50
51 static test_return_t cleanup_pairs(memcached_st *memc __attribute__((unused)))
52 {
53 pairs_free(global_pairs);
54
55 return 0;
56 }
57
58 static test_return_t generate_pairs(memcached_st *memc __attribute__((unused)))
59 {
60 unsigned long long x;
61 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
62 global_count= GLOBAL_COUNT;
63
64 for (x= 0; x < global_count; x++)
65 {
66 global_keys[x]= global_pairs[x].key;
67 global_keys_length[x]= global_pairs[x].key_length;
68 }
69
70 return 0;
71 }
72
73 static test_return_t drizzle(memcached_st *memc)
74 {
75 unsigned int x;
76 memcached_return_t rc;
77 char *return_value;
78 size_t return_value_length;
79 uint32_t flags;
80
81 infinite:
82 for (x= 0; x < TEST_COUNTER; x++)
83 {
84 uint32_t test_bit;
85 uint8_t which;
86
87 test_bit= (uint32_t)(random() % GLOBAL_COUNT);
88 which= (uint8_t)(random() % 2);
89
90 if (which == 0)
91 {
92 return_value= memcached_get(memc, global_keys[test_bit], global_keys_length[test_bit],
93 &return_value_length, &flags, &rc);
94 if (rc == MEMCACHED_SUCCESS && return_value)
95 free(return_value);
96 else if (rc == MEMCACHED_NOTFOUND)
97 continue;
98 else
99 {
100 WATCHPOINT_ERROR(rc);
101 WATCHPOINT_ASSERT(rc);
102 }
103 }
104 else
105 {
106 rc= memcached_set(memc, global_pairs[test_bit].key,
107 global_pairs[test_bit].key_length,
108 global_pairs[test_bit].value,
109 global_pairs[test_bit].value_length,
110 0, 0);
111 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
112 {
113 WATCHPOINT_ERROR(rc);
114 WATCHPOINT_ASSERT(0);
115 }
116 }
117 }
118
119 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
120 goto infinite;
121
122 return 0;
123 }
124
125 static memcached_return_t pre_nonblock(memcached_st *memc)
126 {
127 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
128
129 return MEMCACHED_SUCCESS;
130 }
131
132 static memcached_return_t pre_md5(memcached_st *memc)
133 {
134 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
135
136 return MEMCACHED_SUCCESS;
137 }
138
139 static memcached_return_t pre_hsieh(memcached_st *memc)
140 {
141 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
142
143 return MEMCACHED_SUCCESS;
144 }
145
146 static memcached_return_t enable_consistent(memcached_st *memc)
147 {
148 memcached_server_distribution_t value= MEMCACHED_DISTRIBUTION_CONSISTENT;
149 memcached_hash_t hash;
150 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
151 pre_hsieh(memc);
152
153 value= (memcached_server_distribution_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
154 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
155
156 hash= (memcached_hash_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
157 assert(hash == MEMCACHED_HASH_HSIEH);
158
159
160 return MEMCACHED_SUCCESS;
161 }
162
163 /*
164 Set the value, then quit to make sure it is flushed.
165 Come back in and test that add fails.
166 */
167 static test_return_t add_test(memcached_st *memc)
168 {
169 memcached_return_t rc;
170 const char *key= "foo";
171 const char *value= "when we sanitize";
172 unsigned long long setting_value;
173
174 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
175
176 rc= memcached_set(memc, key, strlen(key),
177 value, strlen(value),
178 (time_t)0, (uint32_t)0);
179 test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
180 memcached_quit(memc);
181 rc= memcached_add(memc, key, strlen(key),
182 value, strlen(value),
183 (time_t)0, (uint32_t)0);
184
185 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
186 if (setting_value)
187 {
188 test_truth(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
189 }
190 else
191 {
192 test_truth(rc == MEMCACHED_NOTSTORED);
193 }
194
195 return 0;
196 }
197
198 /*
199 * repeating add_tests many times
200 * may show a problem in timing
201 */
202 static test_return_t many_adds(memcached_st *memc)
203 {
204 unsigned int i;
205 for (i = 0; i < TEST_COUNTER; i++)
206 {
207 add_test(memc);
208 }
209 return 0;
210 }
211
212 test_st smash_tests[] ={
213 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
214 {"drizzle", 1, (test_callback_fn)drizzle },
215 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
216 {"many_adds", 1, (test_callback_fn)many_adds },
217 {0, 0, 0}
218 };
219
220
221 collection_st collection[] ={
222 {"smash", 0, 0, smash_tests},
223 {"smash_hsieh", (test_callback_fn)pre_hsieh, 0, smash_tests},
224 {"smash_hsieh_consistent", (test_callback_fn)enable_consistent, 0, smash_tests},
225 {"smash_md5", (test_callback_fn)pre_md5, 0, smash_tests},
226 {"smash_nonblock", (test_callback_fn)pre_nonblock, 0, smash_tests},
227 {0, 0, 0, 0}
228 };
229
230
231 #define SERVERS_TO_CREATE 5
232
233 #include "libmemcached_world.h"
234
235 void get_world(world_st *world)
236 {
237 world->collections= collection;
238 world->collection_startup= (test_callback_fn)world_collection_startup;
239 world->flush= (test_callback_fn)world_flush;
240 world->pre_run= (test_callback_fn)world_pre_run;
241 world->create= (test_callback_create_fn)world_create;
242 world->post_run= (test_callback_fn)world_post_run;
243 world->on_error= (test_callback_error_fn)world_on_error;
244 world->destroy= (test_callback_fn)world_destroy;
245 world->runner= &defualt_libmemcached_runner;
246 }