2 Sample test application.
5 #include <libmemcached/memcached.h>
10 #include <sys/types.h>
15 #include "../libmemcached/common.h"
16 #include "../clients/generator.h"
17 #include "../clients/execute.h"
20 #define INT64_MAX LONG_MAX
23 #define INT32_MAX INT_MAX
29 /* Number of items generated for tests */
30 #define GLOBAL_COUNT 100000
32 /* Number of times to run the test loop */
33 #define TEST_COUNTER 500000
34 static uint32_t global_count
;
36 static pairs_st
*global_pairs
;
37 static char *global_keys
[GLOBAL_COUNT
];
38 static size_t global_keys_length
[GLOBAL_COUNT
];
40 uint8_t cleanup_pairs(memcached_st
*memc
)
42 pairs_free(global_pairs
);
47 uint8_t generate_pairs(memcached_st
*memc
)
50 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
51 global_count
= GLOBAL_COUNT
;
53 for (x
= 0; x
< global_count
; x
++)
55 global_keys
[x
]= global_pairs
[x
].key
;
56 global_keys_length
[x
]= global_pairs
[x
].key_length
;
62 uint8_t drizzle(memcached_st
*memc
)
67 size_t return_value_length
;
71 for (x
= 0; x
< TEST_COUNTER
; x
++)
76 test_bit
= random() % GLOBAL_COUNT
;
81 return_value
= memcached_get(memc
, global_keys
[test_bit
], global_keys_length
[test_bit
],
82 &return_value_length
, &flags
, &rc
);
83 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
85 else if (rc
== MEMCACHED_NOTFOUND
)
90 WATCHPOINT_ASSERT(rc
);
95 rc
= memcached_set(memc
, global_pairs
[test_bit
].key
,
96 global_pairs
[test_bit
].key_length
,
97 global_pairs
[test_bit
].value
,
98 global_pairs
[test_bit
].value_length
,
100 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
102 WATCHPOINT_ERROR(rc
);
103 WATCHPOINT_ASSERT(0);
108 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
114 memcached_return
pre_nonblock(memcached_st
*memc
)
116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
118 return MEMCACHED_SUCCESS
;
121 memcached_return
pre_md5(memcached_st
*memc
)
123 memcached_hash value
= MEMCACHED_HASH_MD5
;
124 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
126 return MEMCACHED_SUCCESS
;
129 memcached_return
pre_hsieh(memcached_st
*memc
)
131 memcached_hash value
= MEMCACHED_HASH_HSIEH
;
132 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
134 return MEMCACHED_SUCCESS
;
137 memcached_return
enable_consistent(memcached_st
*memc
)
139 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
141 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, &value
);
144 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
145 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
147 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
148 assert(hash
== MEMCACHED_HASH_HSIEH
);
151 return MEMCACHED_SUCCESS
;
155 Set the value, then quit to make sure it is flushed.
156 Come back in and test that add fails.
158 uint8_t add_test(memcached_st
*memc
)
162 char *value
= "when we sanitize";
163 unsigned long long setting_value
;
165 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
167 rc
= memcached_set(memc
, key
, strlen(key
),
168 value
, strlen(value
),
169 (time_t)0, (uint32_t)0);
170 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
171 memcached_quit(memc
);
172 rc
= memcached_add(memc
, key
, strlen(key
),
173 value
, strlen(value
),
174 (time_t)0, (uint32_t)0);
176 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
178 assert(rc
== MEMCACHED_NOTSTORED
|| MEMCACHED_STORED
);
180 assert(rc
== MEMCACHED_NOTSTORED
);
186 * repeating add_tests many times
187 * may show a problem in timing
189 uint8_t many_adds(memcached_st
*memc
)
192 for (i
= 0; i
< TEST_COUNTER
; i
++){
198 test_st smash_tests
[] ={
199 {"generate_pairs", 1, generate_pairs
},
200 {"drizzle", 1, drizzle
},
201 {"cleanup", 1, cleanup_pairs
},
202 {"many_adds", 1, many_adds
},
207 collection_st collection
[] ={
208 {"smash", 0, 0, smash_tests
},
209 {"smash_hsieh", pre_hsieh
, 0, smash_tests
},
210 {"smash_hsieh_consistent", enable_consistent
, 0, smash_tests
},
211 {"smash_md5", pre_md5
, 0, smash_tests
},
212 {"smash_nonblock", pre_nonblock
, 0, smash_tests
},
216 #define SERVERS_TO_CREATE 5
218 void *world_create(void)
220 server_startup_st
*construct
;
222 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
223 memset(construct
, 0, sizeof(server_startup_st
));
224 construct
->count
= SERVERS_TO_CREATE
;
226 server_startup(construct
);
231 void world_destroy(void *p
)
233 server_startup_st
*construct
= (server_startup_st
*)p
;
234 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
235 memcached_server_list_free(servers
);
237 server_shutdown(construct
);
241 void get_world(world_st
*world
)
243 world
->collections
= collection
;
244 world
->create
= world_create
;
245 world
->destroy
= world_destroy
;