testing: server_add
[awesomized/libmemcached] / test / tests / memcached / servers.cpp
1 #include "test/lib/common.hpp"
2
3 #include "test/lib/MemcachedCluster.hpp"
4
5 static memcached_return_t server_display_function(const memcached_st *,
6 const memcached_instance_st * server,
7 void *context)
8 {
9 if (context) {
10 auto bigger = reinterpret_cast<size_t *>(context);
11 REQUIRE(*bigger <= memcached_server_port(server));
12 *bigger = memcached_server_port(server);
13 }
14 return MEMCACHED_SUCCESS;
15 }
16
17 TEST_CASE("memcached_servers") {
18 SECTION("memcached_servers_parse") {
19 SECTION("does not leak memory") {
20 memcached_server_st *s = memcached_servers_parse("1.2.3.4:1234");
21 REQUIRE(s);
22 memcached_server_free(s);
23 }
24 }
25
26 SECTION("memcached_server_list") {
27 SECTION("append with weight - all zeros") {
28 memcached_server_st *sl = memcached_server_list_append_with_weight(
29 nullptr, nullptr, 0, 0, 0
30 );
31 REQUIRE(sl);
32 memcached_server_list_free(sl);
33 }
34 SECTION("append with weight - host set only") {
35 memcached_server_st *sl = memcached_server_list_append_with_weight(
36 nullptr, "localhost", 0, 0, 0
37 );
38 REQUIRE(sl);
39 memcached_server_list_free(sl);
40 }
41 SECTION("append with weight - error set only") {
42 memcached_return_t rc;
43 memcached_server_st *sl = memcached_server_list_append_with_weight(
44 nullptr, nullptr, 0, 0, &rc
45 );
46 REQUIRE(sl);
47 REQUIRE(MEMCACHED_SUCCESS == rc);
48 memcached_server_list_free(sl);
49 }
50 }
51
52 SECTION("no configured servers") {
53 MemcachedPtr memc;
54
55 REQUIRE(MEMCACHED_NO_SERVERS == memcached_increment(*memc, S("key"), 1, nullptr));
56 }
57
58 SECTION("sort") {
59 MemcachedPtr memc;
60 auto local_memc = *memc;
61 LoneReturnMatcher test{local_memc};
62 size_t bigger = 0; /* Prime the value for the test_true in server_display_function */
63 memcached_server_fn callbacks[1]{server_display_function};
64
65 REQUIRE_SUCCESS(memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1));
66
67 SECTION("variation 1") {
68 for (uint32_t x = 0; x < 7; x++) {
69 REQUIRE_SUCCESS(memcached_server_add_with_weight(local_memc, "localhost", random_port(), 0));
70 REQUIRE(x + 1 == memcached_server_count(local_memc));
71 }
72 memcached_server_cursor(local_memc, callbacks, &bigger, 1);
73 }
74
75 SECTION("variation 2") {
76 const memcached_instance_st *instance;
77
78 REQUIRE_SUCCESS(memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0));
79 instance = memcached_server_instance_by_position(local_memc, 0);
80 REQUIRE(in_port_t(43043) == memcached_server_port(instance));
81
82 REQUIRE_SUCCESS(memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0));
83
84 instance = memcached_server_instance_by_position(local_memc, 0);
85 REQUIRE(in_port_t(43042) == memcached_server_port(instance));
86
87 instance = memcached_server_instance_by_position(local_memc, 1);
88 REQUIRE(in_port_t(43043) == memcached_server_port(instance));
89
90 memcached_server_cursor(local_memc, callbacks, &bigger, 1);
91 }
92 }
93
94 SECTION("add null/empty") {
95 MemcachedPtr memc;
96
97 REQUIRE(MEMCACHED_SUCCESS == memcached_server_add(*memc, nullptr, 0));
98 REQUIRE(1 == memcached_server_count(*memc));
99
100 REQUIRE(MEMCACHED_SUCCESS == memcached_server_add(*memc, "", 0));
101 REQUIRE(2 == memcached_server_count(*memc));
102 }
103
104 SECTION("add many") {
105 MemcachedPtr memc;
106
107 for (auto i = 0; i < 100; ++i) {
108 REQUIRE(MEMCACHED_SUCCESS == memcached_server_add(*memc, "memc", 11211 + i));
109 }
110 REQUIRE(100 == memcached_server_count(*memc));
111 }
112
113 SECTION("add many weighted") {
114 MemcachedPtr memc;
115
116 for (auto i = 0; i < 100; ++i) {
117 REQUIRE(MEMCACHED_SUCCESS == memcached_server_add_with_weight(*memc, "memc", 11211 +i, i % 10));
118 }
119 REQUIRE(100 == memcached_server_count(*memc));
120 }
121
122 SECTION("reset") {
123 MemcachedPtr memc;
124
125 REQUIRE_FALSE(memcached_server_count(*memc));
126 REQUIRE(MEMCACHED_SUCCESS == memcached_server_add(*memc, "local", 12345));
127 REQUIRE(1 == memcached_server_count(*memc));
128 memcached_servers_reset(*memc);
129 REQUIRE_FALSE(memcached_server_count(*memc));
130 REQUIRE(MEMCACHED_SUCCESS == memcached_server_add(*memc, "local", 12345));
131 REQUIRE(1 == memcached_server_count(*memc));
132 memcached_servers_reset(*memc);
133 REQUIRE_FALSE(memcached_server_count(*memc));
134 }
135 }