2 Sample test application.
14 #include "../libmemcached/common.h"
15 #include "../clients/generator.h"
16 #include "../clients/execute.h"
19 #define INT64_MAX LONG_MAX
22 #define INT32_MAX INT_MAX
28 /* Number of items generated for tests */
29 #define GLOBAL_COUNT 100000
31 /* Number of times to run the test loop */
32 #define TEST_COUNTER 500000
33 static uint32_t global_count
;
35 static pairs_st
*global_pairs
;
36 static char *global_keys
[GLOBAL_COUNT
];
37 static size_t global_keys_length
[GLOBAL_COUNT
];
39 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
41 pairs_free(global_pairs
);
46 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
49 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
50 global_count
= GLOBAL_COUNT
;
52 for (x
= 0; x
< global_count
; x
++)
54 global_keys
[x
]= global_pairs
[x
].key
;
55 global_keys_length
[x
]= global_pairs
[x
].key_length
;
61 static test_return
drizzle(memcached_st
*memc
)
66 size_t return_value_length
;
70 for (x
= 0; x
< TEST_COUNTER
; x
++)
75 test_bit
= random() % GLOBAL_COUNT
;
80 return_value
= memcached_get(memc
, global_keys
[test_bit
], global_keys_length
[test_bit
],
81 &return_value_length
, &flags
, &rc
);
82 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
84 else if (rc
== MEMCACHED_NOTFOUND
)
89 WATCHPOINT_ASSERT(rc
);
94 rc
= memcached_set(memc
, global_pairs
[test_bit
].key
,
95 global_pairs
[test_bit
].key_length
,
96 global_pairs
[test_bit
].value
,
97 global_pairs
[test_bit
].value_length
,
99 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
101 WATCHPOINT_ERROR(rc
);
102 WATCHPOINT_ASSERT(0);
107 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
113 static memcached_return
pre_nonblock(memcached_st
*memc
)
115 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
117 return MEMCACHED_SUCCESS
;
120 static memcached_return
pre_md5(memcached_st
*memc
)
122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
124 return MEMCACHED_SUCCESS
;
127 static memcached_return
pre_hsieh(memcached_st
*memc
)
129 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
131 return MEMCACHED_SUCCESS
;
134 static memcached_return
enable_consistent(memcached_st
*memc
)
136 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
138 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
141 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
142 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
144 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
145 assert(hash
== MEMCACHED_HASH_HSIEH
);
148 return MEMCACHED_SUCCESS
;
152 Set the value, then quit to make sure it is flushed.
153 Come back in and test that add fails.
155 static test_return
add_test(memcached_st
*memc
)
159 char *value
= "when we sanitize";
160 unsigned long long setting_value
;
162 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
164 rc
= memcached_set(memc
, key
, strlen(key
),
165 value
, strlen(value
),
166 (time_t)0, (uint32_t)0);
167 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
168 memcached_quit(memc
);
169 rc
= memcached_add(memc
, key
, strlen(key
),
170 value
, strlen(value
),
171 (time_t)0, (uint32_t)0);
173 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
175 assert(rc
== MEMCACHED_NOTSTORED
|| MEMCACHED_STORED
);
177 assert(rc
== MEMCACHED_NOTSTORED
);
183 * repeating add_tests many times
184 * may show a problem in timing
186 static test_return
many_adds(memcached_st
*memc
)
189 for (i
= 0; i
< TEST_COUNTER
; i
++){
195 test_st smash_tests
[] ={
196 {"generate_pairs", 1, generate_pairs
},
197 {"drizzle", 1, drizzle
},
198 {"cleanup", 1, cleanup_pairs
},
199 {"many_adds", 1, many_adds
},
204 collection_st collection
[] ={
205 {"smash", 0, 0, smash_tests
},
206 {"smash_hsieh", pre_hsieh
, 0, smash_tests
},
207 {"smash_hsieh_consistent", enable_consistent
, 0, smash_tests
},
208 {"smash_md5", pre_md5
, 0, smash_tests
},
209 {"smash_nonblock", pre_nonblock
, 0, smash_tests
},
213 #define SERVERS_TO_CREATE 5
215 static void *world_create(void)
217 server_startup_st
*construct
;
219 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
220 memset(construct
, 0, sizeof(server_startup_st
));
221 construct
->count
= SERVERS_TO_CREATE
;
223 server_startup(construct
);
228 static void world_destroy(void *p
)
230 server_startup_st
*construct
= (server_startup_st
*)p
;
231 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
232 memcached_server_list_free(servers
);
234 server_shutdown(construct
);
238 void get_world(world_st
*world
)
240 world
->collections
= collection
;
241 world
->create
= world_create
;
242 world
->destroy
= world_destroy
;