p9y
[m6w6/libmemcached] / test / lib / common.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include <chrono>
19 #include <cstring>
20 #include <iostream>
21 #include <map>
22 #include <optional>
23 #include <string>
24 #include <sstream>
25 #include <stdexcept>
26 #include <thread>
27 #include <variant>
28 #include <vector>
29
30 #include "test/conf.h"
31 #include "test/lib/catch.hpp"
32 #include "test/lib/env.hpp"
33 #include "test/lib/random.hpp"
34
35 #include "libmemcached/memcached.h"
36
37 using namespace std;
38 using socket_or_port_t = variant<string, int>;
39
40 /**
41 * Useful macros for testing
42 */
43 #define S(s) (s), strlen(s)
44 #define LOOPED_SECTION(tests) \
45 for (auto &[name, test] : tests) DYNAMIC_SECTION("test " << name)
46 #define REQUIRE_SUCCESS(rc) \
47 do { \
48 INFO("expected: SUCCESS"); \
49 REQUIRE_THAT(rc, test.returns.success()); \
50 } while (0)
51 #define REQUIRE_RC(rc, call) \
52 do { \
53 INFO("expected: " << memcached_strerror(nullptr, rc)); \
54 REQUIRE_THAT(call, test.returns(rc)); \
55 } while (0)
56
57 inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned,
58 memcached_return_t &rc) {
59 keys_returned = 0;
60
61 memcached_result_st *result = nullptr;
62 while ((result = memcached_fetch_result(memc, result, &rc))) {
63 REQUIRE(MEMCACHED_SUCCESS == rc);
64 keys_returned += 1;
65 }
66 memcached_result_free(result);
67 return MEMCACHED_SUCCESS;
68 }
69
70 inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned) {
71 memcached_return_t rc;
72 fetch_all_results(memc, keys_returned, rc);
73 return rc;
74 }
75
76 #include <cstdlib>
77 #include <unistd.h>
78
79 class Tempfile {
80 public:
81 explicit Tempfile(const char templ_[] = "memc.test.XXXXXX") {
82 *copy(S(templ_) + templ_, fn) = '\0';
83 fd = mkstemp(fn);
84 }
85 ~Tempfile() {
86 close(fd);
87 unlink(fn);
88 }
89 [[nodiscard]] int getFd() const { return fd; }
90 [[nodiscard]] const char *getFn() const { return fn; }
91 bool put(const char *buf, size_t len) const {
92 return static_cast<ssize_t>(len) == write(fd, buf, len);
93 }
94 string get() const {
95 string all;
96 char buf[200];
97 ssize_t len;
98
99 lseek(fd, 0, SEEK_SET);
100 while (0 < (len = read(fd, buf, sizeof(buf)))) {
101 all.append(buf, len);
102 }
103 return all;
104 }
105
106 private:
107 char fn[80];
108 int fd;
109 };
110
111 class MemcachedPtr {
112 public:
113 memcached_st *memc;
114
115 explicit MemcachedPtr(memcached_st *memc_) {
116 memc = memc_;
117 }
118 MemcachedPtr()
119 : MemcachedPtr(memcached_create(nullptr)) {}
120 ~MemcachedPtr() {
121 memcached_free(memc);
122 }
123 memcached_st *operator*() const {
124 return memc;
125 }
126 auto operator->() const {
127 return memc;
128 }
129 };
130
131 template<class T>
132 class Malloced {
133 T *ptr;
134
135 public:
136 explicit Malloced(T *ptr_)
137 : ptr{ptr_} {}
138 Malloced &operator=(T *ptr_) {
139 if (ptr)
140 free(ptr);
141 ptr = ptr_;
142 return *this;
143 }
144 ~Malloced() {
145 if (ptr)
146 free(ptr);
147 }
148 auto operator*() const { return ptr; }
149 auto operator->() const { return ptr; }
150 };