1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3 #include "test/fixtures/callbacks.hpp"
5 #include "libmemcached/instance.hpp"
6 #include "libmemcachedutil/common.h"
8 TEST_CASE("memcached_util") {
9 SECTION("version_check") {
10 auto test
= MemcachedCluster::mixed();
11 auto memc
= &test
.memc
;
13 REQUIRE_SUCCESS(memcached_version(memc
));
14 REQUIRE(libmemcached_util_version_check(memc
, 0, 0, 0));
15 REQUIRE_FALSE(libmemcached_util_version_check(memc
, 255, 255, 255));
17 auto instance
= memcached_server_instance_by_position(memc
, 0);
18 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
));
19 if (instance
->micro_version
) {
20 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
- 1));
21 if (instance
->micro_version
< 255) {
22 REQUIRE_FALSE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
+ 1));
25 if (instance
->minor_version
) {
26 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
- 1, 255));
27 if (instance
->minor_version
< 255) {
28 REQUIRE_FALSE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
+ 1, 0));
31 if (instance
->major_version
) {
32 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
- 1, 255, 255));
33 if (instance
->major_version
< 255) {
34 REQUIRE_FALSE(libmemcached_util_version_check(memc
, instance
->major_version
+ 1, 0, 0));
39 // see sasl.cpp for getpid2 test
41 auto test
= MemcachedCluster::network();
42 memcached_return_t rc
;
44 for (auto &server
: test
.cluster
.getServers()) {
45 REQUIRE(server
.getPid() == libmemcached_util_getpid("localhost", get
<int>(server
.getSocketOrPort()), &rc
));
49 REQUIRE(-1 == libmemcached_util_getpid("localhost", 1, &rc
));
50 REQUIRE(memcached_fatal(rc
));
53 // see sasl.cpp for ping2 test
55 auto test
= MemcachedCluster::network();
56 auto memc
= &test
.memc
;
57 memcached_server_fn fptr
[] = {&ping_callback
};
59 REQUIRE_SUCCESS(memcached_server_cursor(memc
, fptr
, nullptr, 1));
63 auto test
= MemcachedCluster::network();
65 for (auto &server
: test
.cluster
.getServers()) {
66 memcached_return_t rc
;
67 REQUIRE(libmemcached_util_flush("localhost", get
<int>(server
.getSocketOrPort()), &rc
));
73 SECTION("deprecated") {
74 auto conf
= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com --POOL-MIN=10 --POOL-MAX=32";
75 auto pool
= memcached_pool(S(conf
));
78 memcached_return_t rc
;
79 auto memc
= memcached_pool_pop(pool
, false, &rc
);
81 REQUIRE(MEMCACHED_SUCCESS
== rc
);
83 REQUIRE(MEMCACHED_SUCCESS
== memcached_pool_push(pool
, memc
));
84 REQUIRE(nullptr == memcached_pool_destroy(pool
));
88 auto test
= MemcachedCluster::mixed();
89 auto memc
= &test
.memc
;
90 memcached_return_t rc
;
92 constexpr auto POOL_MIN
= 5;
93 constexpr auto POOL_MAX
= 10;
95 auto pool
= memcached_pool_create(memc
, POOL_MIN
, POOL_MAX
);
98 array
<memcached_st
*, POOL_MAX
> hold
{};
99 for (auto &h
: hold
) {
100 h
= memcached_pool_fetch(pool
, nullptr, &rc
);
105 SECTION("depleted") {
106 REQUIRE(nullptr == memcached_pool_fetch(pool
, nullptr, &rc
));
107 REQUIRE_RC(MEMCACHED_NOTFOUND
, rc
);
111 for (auto h
: hold
) {
112 auto s
= to_string(reinterpret_cast<uintptr_t>(h
));
113 REQUIRE_SUCCESS(memcached_set(h
, s
.c_str(), s
.length(), s
.c_str(), s
.length(), 0, 0));
115 for (auto h
: hold
) {
116 auto s
= to_string(reinterpret_cast<uintptr_t>(h
));
117 Malloced
val(memcached_get(h
, s
.c_str(), s
.length(), nullptr, nullptr, &rc
));
120 REQUIRE(h
== reinterpret_cast<memcached_st
*>(stoul(*val
)));
123 REQUIRE_SUCCESS(memcached_set(hold
[0], S(__func__
), "0", 1, 0, 0));
125 for (auto h
: hold
) {
127 REQUIRE_SUCCESS(memcached_increment(h
, S(__func__
), 1, &val
));
132 SECTION("behaviors") {
134 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, &val
));
135 REQUIRE_FALSE(val
== 9999);
136 REQUIRE_SUCCESS(memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999));
137 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, &val
));
138 REQUIRE(val
== 9999);
140 for (auto &h
: hold
) {
141 REQUIRE_FALSE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
));
142 REQUIRE_SUCCESS(memcached_pool_release(pool
, h
));
143 h
= memcached_pool_fetch(pool
, nullptr, &rc
);
146 REQUIRE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
));
147 REQUIRE_FALSE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
));
148 REQUIRE_SUCCESS(memcached_pool_release(pool
, h
));
151 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
, &val
));
152 REQUIRE_FALSE(val
== 9999);
153 REQUIRE_SUCCESS(memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
, 9999));
154 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
, &val
));
155 REQUIRE(val
== 9999);
157 for (auto &h
: hold
) {
158 h
= memcached_pool_fetch(pool
, nullptr, &rc
);
161 REQUIRE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
));
165 for (auto h
: hold
) {
166 REQUIRE_SUCCESS(memcached_pool_release(pool
, h
));
167 auto again
= memcached_pool_fetch(pool
, nullptr, &rc
);
170 REQUIRE_SUCCESS(memcached_pool_release(pool
, again
));
173 REQUIRE(memc
== memcached_pool_destroy(pool
));