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