1 #include "test/lib/common.hpp"
3 #include "test/lib/MemcachedCluster.hpp"
5 static memcached_return_t
server_display_function(const memcached_st
*,
6 const memcached_instance_st
* server
,
10 auto bigger
= reinterpret_cast<size_t *>(context
);
11 REQUIRE(*bigger
<= memcached_server_port(server
));
12 *bigger
= memcached_server_port(server
);
14 return MEMCACHED_SUCCESS
;
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");
22 memcached_server_free(s
);
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
32 memcached_server_list_free(sl
);
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
39 memcached_server_list_free(sl
);
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
47 REQUIRE(MEMCACHED_SUCCESS
== rc
);
48 memcached_server_list_free(sl
);
52 SECTION("no configured servers") {
55 REQUIRE(MEMCACHED_NO_SERVERS
== memcached_increment(*memc
, S("key"), 1, nullptr));
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
};
65 REQUIRE_SUCCESS(memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1));
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
));
72 memcached_server_cursor(local_memc
, callbacks
, &bigger
, 1);
75 SECTION("variation 2") {
76 const memcached_instance_st
*instance
;
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
));
82 REQUIRE_SUCCESS(memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0));
84 instance
= memcached_server_instance_by_position(local_memc
, 0);
85 REQUIRE(in_port_t(43042) == memcached_server_port(instance
));
87 instance
= memcached_server_instance_by_position(local_memc
, 1);
88 REQUIRE(in_port_t(43043) == memcached_server_port(instance
));
90 memcached_server_cursor(local_memc
, callbacks
, &bigger
, 1);
94 SECTION("add null/empty") {
97 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add(*memc
, nullptr, 0));
98 REQUIRE(1 == memcached_server_count(*memc
));
100 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add(*memc
, "", 0));
101 REQUIRE(2 == memcached_server_count(*memc
));
104 SECTION("add many") {
107 for (auto i
= 0; i
< 100; ++i
) {
108 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add(*memc
, "memc", 11211 + i
));
110 REQUIRE(100 == memcached_server_count(*memc
));
113 SECTION("add many weighted") {
116 for (auto i
= 0; i
< 100; ++i
) {
117 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add_with_weight(*memc
, "memc", 11211 +i
, i
% 10));
119 REQUIRE(100 == memcached_server_count(*memc
));
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
));