1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3 #include "test/fixtures/callbacks.hpp"
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");
10 memcached_server_free(s
);
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
20 memcached_server_list_free(sl
);
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
27 memcached_server_list_free(sl
);
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
35 REQUIRE(MEMCACHED_SUCCESS
== rc
);
36 memcached_server_list_free(sl
);
40 SECTION("no configured servers") {
43 REQUIRE(MEMCACHED_NO_SERVERS
== memcached_increment(*memc
, S("key"), 1, nullptr));
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
};
53 REQUIRE_SUCCESS(memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1));
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
));
60 memcached_server_cursor(local_memc
, callbacks
, &bigger
, 1);
63 SECTION("variation 2") {
64 const memcached_instance_st
*instance
;
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
));
70 REQUIRE_SUCCESS(memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0));
72 instance
= memcached_server_instance_by_position(local_memc
, 0);
73 REQUIRE(in_port_t(43042) == memcached_server_port(instance
));
75 instance
= memcached_server_instance_by_position(local_memc
, 1);
76 REQUIRE(in_port_t(43043) == memcached_server_port(instance
));
78 memcached_server_cursor(local_memc
, callbacks
, &bigger
, 1);
82 SECTION("add null/empty") {
85 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add(*memc
, nullptr, 0));
86 REQUIRE(1 == memcached_server_count(*memc
));
88 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add(*memc
, "", 0));
89 REQUIRE(2 == memcached_server_count(*memc
));
95 for (auto i
= 0; i
< 100; ++i
) {
96 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add(*memc
, "memc", 11211 + i
));
98 REQUIRE(100 == memcached_server_count(*memc
));
101 SECTION("add many weighted") {
104 for (auto i
= 0; i
< 100; ++i
) {
105 REQUIRE(MEMCACHED_SUCCESS
== memcached_server_add_with_weight(*memc
, "memc", 11211 +i
, i
% 10));
107 REQUIRE(100 == memcached_server_count(*memc
));
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
));