bin: consolidate clients
[awesomized/libmemcached] / test / tests / memcached / haldenbrand.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3
4 /* Test case provided by Cal Haldenbrand */
5
6 #define HALDENBRAND_KEY_COUNT 3000U // * 1024576
7 #define HALDENBRAND_FLAG_KEY 99 // * 1024576
8
9 #include <cstdlib>
10
11 TEST_CASE("memcached_haldenbrand_nblock_tcp_ndelay") {
12 pair<string, MemcachedCluster> tests[] = {
13 {"network", MemcachedCluster::network()},
14 };
15
16 for (auto &[name, test] : tests) {
17 REQUIRE_SUCCESS(memcached_behavior_set(&test.memc, MEMCACHED_BEHAVIOR_NO_BLOCK, true));
18 REQUIRE_SUCCESS(memcached_behavior_set(&test.memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true));
19 }
20
21 LOOPED_SECTION(tests) {
22 auto memc = &test.memc;
23
24 /* We just keep looking at the same values over and over */
25 srandom(10);
26
27 /* add key */
28 unsigned long long total = 0;
29 for (uint32_t x = 0; total < 20 * 1024576; x++) {
30 uint32_t size = (uint32_t) (rand() % (5 * 1024)) + 400;
31 char randomstuff[6 * 1024];
32 memset(randomstuff, 0, 6 * 1024);
33 REQUIRE(size < 6 * 1024); /* Being safe here */
34
35 for (uint32_t j = 0; j < size; j++) {
36 randomstuff[j] = (signed char) ((rand() % 26) + 97);
37 }
38
39 total += size;
40 char key[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1];
41 int key_length = snprintf(key, sizeof(key), "%u", x);
42 REQUIRE_SUCCESS(memcached_set(memc, key, key_length,
43 randomstuff, strlen(randomstuff),
44 time_t(0), HALDENBRAND_FLAG_KEY
45 ));
46 }
47 REQUIRE(total > HALDENBRAND_KEY_COUNT);
48
49 size_t total_value_length = 0;
50 for (uint32_t x = 0, errors = 0; total_value_length < 24576; x++) {
51 uint32_t flags = 0;
52 size_t val_len = 0;
53
54 char key[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1];
55 int key_length = snprintf(key, sizeof(key), "%u", x);
56
57 memcached_return_t rc;
58 char *getval = memcached_get(memc, key, key_length, &val_len, &flags, &rc);
59 if (memcached_failed(rc)) {
60 if (rc == MEMCACHED_NOTFOUND) {
61 errors++;
62 } else {
63 REQUIRE(rc);
64 }
65
66 continue;
67 }
68 REQUIRE(uint32_t(HALDENBRAND_FLAG_KEY) == flags);
69 REQUIRE(getval);
70
71 total_value_length += val_len;
72 errors = 0;
73 ::free(getval);
74 }
75
76
77 std::vector<size_t> key_lengths;
78 key_lengths.resize(HALDENBRAND_KEY_COUNT);
79 std::vector<char *> keys;
80 keys.resize(key_lengths.size());
81 for (uint32_t x = 0; x < key_lengths.size(); x++) {
82 char key[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1];
83 int key_length = snprintf(key, sizeof(key), "%u", x);
84 REQUIRE(key_length > 0);
85 REQUIRE(key_length < MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1);
86 keys[x] = strdup(key);
87 key_lengths[x] = key_length;
88 }
89
90 REQUIRE_SUCCESS(memcached_mget(memc, &keys[0], &key_lengths[0], key_lengths.size()));
91
92 unsigned int keys_returned;
93 REQUIRE(memcached_success(fetch_all_results(memc, keys_returned)));
94 REQUIRE(HALDENBRAND_KEY_COUNT == keys_returned);
95
96 for (auto key : keys) {
97 free(key);
98 }
99 }
100 }