MySQL 9.7.0
Source Code Documentation
mock_resolver.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_MOCK_HOST_RESOLVER_SRC_MOCK_RESOLVER_H_
27#define ROUTER_SRC_MOCK_HOST_RESOLVER_SRC_MOCK_RESOLVER_H_
28
29#include <string>
30#include <system_error>
31#include <vector>
32
37
38using Hostname = std::string;
39
40template <typename ValueType>
42
43class Entry {
44 public:
45 Entry() {}
46 Entry(const Entry &other) { *this = other; }
47
48 Entry &operator=(const Entry &other) {
50 error_ = other.error_;
51 log_ = other.log_;
52 addresses_ = other.addresses_;
53 called_ = other.called_.load();
54 wait_.set(other.wait_.get());
55
56 return *this;
57 }
58
59 bool fail_not_found_{false};
60 bool error_{false};
61 bool log_{false};
62 std::vector<net::ip::address> addresses_;
63 std::atomic<uint64_t> called_{0};
65};
66
68
70
72 public:
73 void operator()(uint64_t &value) const {
74 if (value > 0) {
75 must_wait_ = value;
76 value = 0;
77 return;
78 }
79 }
80
81 explicit operator bool() const { return 0 != must_wait_; }
82 std::chrono::seconds wait() const { return std::chrono::seconds{must_wait_}; }
83
84 private:
85 mutable uint64_t must_wait_{0};
86};
87
89 public:
90 MockResolver(ResolveActions actions) : actions_{actions} {}
91
92 static std::string action(const Entry &entry) {
93 if (entry.fail_not_found_) return "not-found";
94 if (entry.error_) return "error";
95 return "no-of-addresses-" + std::to_string(entry.addresses_.size());
96 }
97
98 ResolveHostResult resolve_host(const std::string &hostname,
99 [[maybe_unused]] CachePolicy cache_policy =
100 CachePolicy::UseIfPresent) override {
101 auto it = actions_.find(hostname);
102 if (it == actions_.end()) {
103 log_error("MockResolver - unexpected resolve of '%s'", hostname.c_str());
104 return stdx::unexpected(std::make_error_code(std::errc::not_supported));
105 }
106
107 auto &entry = it->second;
108 ++(entry.called_);
109
110 ChangeShouldWait should_wait;
111 entry.wait_.change(should_wait);
112
113 if (should_wait) {
114 log_info("must_wait_for other request for '%s'", hostname.c_str());
115 std::this_thread::sleep_for(should_wait.wait());
116 }
117
118 if (entry.log_) {
119 log_info("MockResolver - mocked resolve of host '%s', returning %s",
120 hostname.c_str(), action(entry).c_str());
121 }
122
123 if (entry.error_) {
124 // Any non not-found error.
125 return stdx::unexpected(
126 std::make_error_code(std::errc::invalid_argument));
127 }
128
129 if (entry.fail_not_found_) {
132 }
133
135 result.addresses = entry.addresses_;
136 return result;
137 }
138
139 private:
141};
142
143#endif // ROUTER_SRC_MOCK_HOST_RESOLVER_SRC_MOCK_RESOLVER_H_
Definition: mock_resolver.h:71
std::chrono::seconds wait() const
Definition: mock_resolver.h:82
void operator()(uint64_t &value) const
Definition: mock_resolver.h:73
Definition: mock_resolver.h:43
Entry(const Entry &other)
Definition: mock_resolver.h:46
bool error_
Definition: mock_resolver.h:60
WaitableVariable< uint64_t > wait_
Definition: mock_resolver.h:64
bool log_
Definition: mock_resolver.h:61
Entry()
Definition: mock_resolver.h:45
std::vector< net::ip::address > addresses_
Definition: mock_resolver.h:62
bool fail_not_found_
Definition: mock_resolver.h:59
std::atomic< uint64_t > called_
Definition: mock_resolver.h:63
Entry & operator=(const Entry &other)
Definition: mock_resolver.h:48
Definition: mock_resolver.h:88
ResolveActions actions_
Definition: mock_resolver.h:140
ResolveHostResult resolve_host(const std::string &hostname, CachePolicy cache_policy=CachePolicy::UseIfPresent) override
Definition: mock_resolver.h:98
static std::string action(const Entry &entry)
Definition: mock_resolver.h:92
MockResolver(ResolveActions actions)
Definition: mock_resolver.h:90
Definition: wait_variable.h:37
Definition: expected.h:286
std::string Hostname
Definition: host_cache.cc:45
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:51
Logging interface for using and extending the logging subsystem.
#define IMPORT_LOG_FUNCTIONS()
convenience macro to avoid common boilerplate
Definition: logging.h:331
std::map< Hostname, Entry > ResolveActions
Definition: mock_resolver.h:69
std::chrono::seconds seconds
Definition: authorize_manager.cc:70
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
@ NotFound
Deterministic negative outcome (NXDOMAIN, NODATA).
CachePolicy
Definition: common.h:40
HARNESS_EXPORT std::error_code make_error_code(ErrcResolveResult) noexcept
Definition: error_code.cc:56
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:97
Define std::hash<Gtid>.
Definition: gtid.h:355
unexpected(E) -> unexpected< E >
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2898
struct result result
Definition: result.h:34
Definition: completion_hash.h:35
Definition: result.h:30
#define log_info(msg,...)
Definition: tm_log.h:79
#define log_error(msg,...)
Definition: tm_log.h:87