Updating for 1.0.2 release
[awesomized/libmemcached] / tests / virtual_buckets.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Client and Server
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <config.h>
39 #include <libtest/test.hpp>
40
41 using namespace libtest;
42
43 #include <tests/virtual_buckets.h>
44
45 #include <libmemcached/memcached.h>
46
47 #include <cstring>
48
49 struct libtest_string_t {
50 const char *c_str;
51 size_t size;
52 };
53
54 static inline libtest_string_t libtest_string(const char *arg, size_t arg_size)
55 {
56 libtest_string_t local= { arg, arg_size };
57 return local;
58 }
59
60 #define make_libtest_string(X) libtest_string((X), static_cast<size_t>(sizeof(X) - 1))
61
62 static libtest_string_t libtest_string_t_null= { 0, 0};
63
64 static bool libtest_string_is_null(const libtest_string_t &string)
65 {
66 if (string.c_str == 0 and string.size == 0)
67 return true;
68
69 return false;
70 }
71
72 struct expect_t {
73 libtest_string_t key;
74 uint32_t server_id;
75 uint32_t bucket_id;
76 };
77
78 expect_t basic_keys[]= {
79 { make_libtest_string("hello"), 0, 0 },
80 { make_libtest_string("doctor"), 0, 0 },
81 { make_libtest_string("name"), 1, 3 },
82 { make_libtest_string("continue"), 1, 3 },
83 { make_libtest_string("yesterday"), 0, 0 },
84 { make_libtest_string("tomorrow"), 1, 1 },
85 { make_libtest_string("another key"), 2, 2 },
86 { libtest_string_t_null, 0, 0 }
87 };
88
89 test_return_t virtual_back_map(memcached_st *)
90 {
91 memcached_return_t rc;
92 memcached_server_st *server_pool;
93 memcached_st *memc;
94
95 memc= memcached_create(NULL);
96 test_true(memc);
97
98 uint32_t server_map[] = { 0, 1, 2, 1 };
99 rc= memcached_bucket_set(memc, server_map, NULL, 4, 2);
100 test_true(rc == MEMCACHED_SUCCESS);
101
102 memcached_server_distribution_t dt;
103 dt= memcached_behavior_get_distribution(memc);
104 test_true(dt == MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET);
105
106 memcached_behavior_set_key_hash(memc, MEMCACHED_HASH_CRC);
107 test_true(rc == MEMCACHED_SUCCESS);
108
109 memcached_hash_t hash_type= memcached_behavior_get_key_hash(memc);
110 test_true(hash_type == MEMCACHED_HASH_CRC);
111
112 server_pool = memcached_servers_parse("localhost:11211, localhost1:11210, localhost2:11211");
113 test_true(server_pool);
114 memcached_server_push(memc, server_pool);
115
116 /* verify that the server list was parsed okay. */
117 test_true(memcached_server_count(memc) == 3);
118 test_true(strcmp(server_pool[0].hostname, "localhost") == 0);
119 test_true(server_pool[0].port == 11211);
120
121 test_true(strcmp(server_pool[1].hostname, "localhost1") == 0);
122 test_true(server_pool[1].port == 11210);
123
124 test_true(strcmp(server_pool[2].hostname, "localhost2") == 0);
125 test_true(server_pool[2].port == 11211);
126
127 dt= memcached_behavior_get_distribution(memc);
128 hash_type= memcached_behavior_get_key_hash(memc);
129 test_true(dt == MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET);
130 test_true(hash_type == MEMCACHED_HASH_CRC);
131
132 /* verify the standard ketama set. */
133 for (expect_t *ptr= basic_keys; not libtest_string_is_null(ptr->key); ptr++)
134 {
135 uint32_t server_idx = memcached_generate_hash(memc, ptr->key.c_str, ptr->key.size);
136
137 char buffer[1024];
138 snprintf(buffer, sizeof(buffer), "%.*s:%lu Got/Expected %u == %u", (int)ptr->key.size, ptr->key.c_str, (unsigned long)ptr->key.size, server_idx, ptr->server_id);
139 test_true_got(server_idx == ptr->server_id, buffer);
140 }
141
142 memcached_server_list_free(server_pool);
143 memcached_free(memc);
144
145 return TEST_SUCCESS;
146 }