Debian Codegraph @

unittest/gunit/xplugin/xpl/timeouts_t.cc

raw symbols SCIP
1 /* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 #include "plugin/x/src/client.h"
28 #include "plugin/x/src/ngs/protocol_encoder.h"
29 #include "plugin/x/src/operations_factory.h"
30 #include "plugin/x/src/variables/system_variables.h"
31 #include "plugin/x/src/variables/system_variables_defaults.h"
32 #include "plugin/x/src/variables/timeout_config.h"
33 #include "unittest/gunit/xplugin/xpl/mock/notice_output_queue.h"
34 #include "unittest/gunit/xplugin/xpl/mock/protocol_encoder.h"
35 #include "unittest/gunit/xplugin/xpl/mock/protocol_monitor.h"
36 #include "unittest/gunit/xplugin/xpl/mock/server.h"
37 #include "unittest/gunit/xplugin/xpl/mock/session.h"
38 #include "unittest/gunit/xplugin/xpl/mock/vio.h"
39 #include "unittest/gunit/xplugin/xpl/mock/waiting_for_io.h"
40
41 namespace xpl {
42 namespace test {
43
44 using ::testing::_;
45 using ::testing::DoAll;
46 using ::testing::Expectation;
47 using ::testing::InSequence;
48 using ::testing::Return;
49 using ::testing::ReturnPointee;
50 using ::testing::ReturnRef;
51 using ::testing::SetArrayArgument;
52 using ::testing::StrictMock;
53
54 class Timers_test_suite : public ::testing::Test {
55 public:
56 void SetUp() override {
57 config->m_timeouts.m_interactive_timeout =
58 defaults::timeout::k_interactive_timeout;
59 config->m_timeouts.m_read_timeout = defaults::timeout::k_read_timeout;
60 config->m_timeouts.m_write_timeout = defaults::timeout::k_write_timeout;
61
62 EXPECT_CALL(mock_server, get_config()).WillRepeatedly(Return(config));
63 EXPECT_CALL(mock_server, is_running()).WillRepeatedly(Return(true));
64 EXPECT_CALL(*mock_vio, get_mysql_socket())
65 .WillRepeatedly(ReturnRef(m_socket));
66 EXPECT_CALL(*mock_wait_for_io, has_to_report_idle_waiting())
67 .WillRepeatedly(Return(false));
68 EXPECT_CALL(*mock_wait_for_io, on_idle_or_before_read())
69 .WillRepeatedly(Return(true));
70 EXPECT_CALL(*mock_session, get_notice_output_queue())
71 .WillRepeatedly(ReturnRef(mock_notice_output_queue));
72 EXPECT_CALL(*mock_protocol_monitor, init(_));
73
74 sut = std::make_shared<Client>(mock_vio, &mock_server, /* id */ 1,
75 mock_protocol_monitor);
76 sut->set_session(mock_session);
77 sut->set_idle_reporting(mock_wait_for_io);
78 }
79
80 void TearDown() override { EXPECT_CALL(*mock_vio, shutdown()); }
81
82 using Strict_mock_vio = StrictMock<mock::Vio>;
83 std::shared_ptr<Strict_mock_vio> mock_vio{new Strict_mock_vio()};
84 StrictMock<mock::Server> mock_server;
85 StrictMock<mock::Protocol_monitor> *mock_protocol_monitor =
86 ngs::allocate_object<StrictMock<mock::Protocol_monitor>>();
87 StrictMock<mock::Notice_output_queue> mock_notice_output_queue;
88 StrictMock<mock::Waiting_for_io> *mock_wait_for_io{
89 new StrictMock<mock::Waiting_for_io>};
90
91 std::shared_ptr<ngs::Protocol_global_config> config{
92 new ngs::Protocol_global_config()};
93 ngs::Memory_block_pool m_pool{{0, k_minimum_page_size}};
94 const std::vector<unsigned char> k_msg{
95 1, 0, 0, 0, 1}; // 1 = size, 0, 0, 0, 1 = Msg_CapGet
96
97 StrictMock<mock::Session> *mock_session = new StrictMock<mock::Session>();
98 std::shared_ptr<Client> sut;
99 MYSQL_SOCKET m_socket{INVALID_SOCKET, nullptr};
100 };
101
102 ACTION_P2(SetSocketErrnoAndReturn, err, result) {
103 xpl::Operations_factory operations_factory;
104
105 operations_factory.create_system_interface()->set_socket_errno(err);
106
107 return result;
108 }
109
110 TEST_F(Timers_test_suite,
111 read_one_message_non_interactive_client_default_wait_timeout) {
112 // Client holds only the timeout value which must be used.
113 // It doesn't hold interactive or non-interactive timeout values.
114 // The timeout value is set from outside thus the test uses
115 // k_interactive_timeout
116 Expectation set_timeout_exp = EXPECT_CALL(
117 *mock_vio,
118 set_timeout_in_ms(iface::Vio::Direction::k_read,
119 defaults::timeout::k_interactive_timeout * 1000));
120
121 EXPECT_CALL(*mock_vio, read(_, _))
122 .After(set_timeout_exp)
123 .WillOnce(DoAll(SetArrayArgument<0>(k_msg.begin(), k_msg.end()),
124 Return(k_msg.size())));
125
126 EXPECT_CALL(*mock_vio, set_state(_)).Times(2);
127 EXPECT_CALL(*mock_protocol_monitor, on_receive(k_msg.size()));
128 EXPECT_CALL(*mock_session, handle_message(_));
129
130 sut->read_one_message_and_dispatch();
131 }
132
133 TEST_F(Timers_test_suite,
134 read_one_message_interactive_client_default_interactive_timeout) {
135 Expectation set_timeout_exp = EXPECT_CALL(
136 *mock_vio,
137 set_timeout_in_ms(iface::Vio::Direction::k_read,
138 defaults::timeout::k_interactive_timeout * 1000));
139
140 EXPECT_CALL(*mock_vio, read(_, _))
141 .After(set_timeout_exp)
142 .WillOnce(DoAll(SetArrayArgument<0>(k_msg.begin(), k_msg.end()),
143 Return(k_msg.size())));
144
145 EXPECT_CALL(*mock_vio, set_state(_)).Times(2);
146 EXPECT_CALL(*mock_session, handle_message(_));
147 EXPECT_CALL(*mock_protocol_monitor, on_receive(k_msg.size()));
148
149 sut->read_one_message_and_dispatch();
150 }
151
152 TEST_F(Timers_test_suite,
153 read_one_message_interactive_client_custom_interactive_timer) {
154 config->m_timeouts.m_interactive_timeout = 11;
155 sut->set_wait_timeout(config->m_timeouts.m_interactive_timeout);
156
157 EXPECT_CALL(*mock_vio,
158 set_timeout_in_ms(iface::Vio::Direction::k_read, 11 * 1000));
159 EXPECT_CALL(*mock_vio, get_mysql_socket()).WillOnce(ReturnRef(m_socket));
160 EXPECT_CALL(*mock_vio, read(_, _))
161 .WillOnce(DoAll(SetArrayArgument<0>(k_msg.begin(), k_msg.end()),
162 Return(k_msg.size())));
163 EXPECT_CALL(*mock_vio, set_state(_)).Times(2);
164 EXPECT_CALL(*mock_protocol_monitor, on_receive(k_msg.size()));
165 EXPECT_CALL(*mock_session, handle_message(_));
166 EXPECT_CALL(*mock_wait_for_io, has_to_report_idle_waiting())
167 .WillRepeatedly(Return(false));
168
169 sut->read_one_message_and_dispatch();
170 }
171
172 TEST_F(Timers_test_suite,
173 read_one_message_non_interactive_client_custom_wait_timer) {
174 config->m_timeouts.m_wait_timeout = 22;
175 sut->set_wait_timeout(config->m_timeouts.m_wait_timeout);
176
177 EXPECT_CALL(*mock_vio,
178 set_timeout_in_ms(iface::Vio::Direction::k_read, 22 * 1000));
179 EXPECT_CALL(*mock_vio, get_mysql_socket()).WillOnce(ReturnRef(m_socket));
180 EXPECT_CALL(*mock_vio, read(_, _))
181 .WillOnce(DoAll(SetArrayArgument<0>(k_msg.begin(), k_msg.end()),
182 Return(k_msg.size())));
183 EXPECT_CALL(*mock_vio, set_state(_)).Times(2);
184 EXPECT_CALL(*mock_session, handle_message(_));
185 EXPECT_CALL(*mock_protocol_monitor, on_receive(k_msg.size()));
186 EXPECT_CALL(*mock_wait_for_io, has_to_report_idle_waiting())
187 .WillRepeatedly(Return(false));
188
189 sut->read_one_message_and_dispatch();
190 }
191
192 TEST_F(Timers_test_suite, read_one_message_default_read_timeout) {
193 EXPECT_CALL(*mock_vio, set_timeout_in_ms(
194 iface::Vio::Direction::k_read,
195 defaults::timeout::k_interactive_timeout * 1000));
196
197 // Expected to be called twice - once for header and once for payload
198 EXPECT_CALL(*mock_vio, read(_, _))
199 .Times(2)
200 .WillOnce(DoAll(SetArrayArgument<0>(k_msg.begin(), k_msg.end() - 1),
201 Return(k_msg.size() - 1)))
202 .WillOnce(
203 DoAll(SetArrayArgument<0>(k_msg.end() - 1, k_msg.end()), Return(1)));
204 EXPECT_CALL(*mock_vio, set_state(_)).Times(2);
205 EXPECT_CALL(*mock_session, handle_message(_));
206 EXPECT_CALL(*mock_protocol_monitor, on_receive(k_msg.size())).Times(1);
207
208 auto conf = std::make_shared<ngs::Protocol_global_config>();
209 EXPECT_CALL(mock_server, get_config()).WillRepeatedly(ReturnPointee(&conf));
210
211 EXPECT_CALL(*mock_vio,
212 set_timeout_in_ms(iface::Vio::Direction::k_read,
213 defaults::timeout::k_read_timeout * 1000));
214
215 sut->read_one_message_and_dispatch();
216 }
217
218 TEST_F(Timers_test_suite, read_one_message_custom_read_timeout) {
219 config->m_timeouts.m_read_timeout = 33;
220 sut->set_read_timeout(config->m_timeouts.m_read_timeout);
221
222 EXPECT_CALL(*mock_vio, set_timeout_in_ms(
223 iface::Vio::Direction::k_read,
224 defaults::timeout::k_interactive_timeout * 1000));
225 EXPECT_CALL(*mock_vio,
226 set_timeout_in_ms(iface::Vio::Direction::k_read, 33 * 1000));
227 EXPECT_CALL(*mock_vio, get_mysql_socket()).WillOnce(ReturnRef(m_socket));
228
229 // Expected to be called twice - once for header and once for payload
230 EXPECT_CALL(*mock_vio, read(_, _))
231 .Times(2)
232 .WillOnce(DoAll(SetArrayArgument<0>(k_msg.begin(), k_msg.end() - 1),
233 Return(k_msg.size() - 1)))
234 .WillOnce(
235 DoAll(SetArrayArgument<0>(k_msg.end() - 1, k_msg.end()), Return(1)));
236 EXPECT_CALL(*mock_vio, set_state(_)).Times(2);
237 EXPECT_CALL(*mock_session, handle_message(_));
238 EXPECT_CALL(*mock_protocol_monitor, on_receive(k_msg.size())).Times(1);
239 EXPECT_CALL(*mock_wait_for_io, has_to_report_idle_waiting())
240 .WillRepeatedly(Return(false));
241
242 auto conf = std::make_shared<ngs::Protocol_global_config>();
243 EXPECT_CALL(mock_server, get_config()).WillRepeatedly(ReturnPointee(&conf));
244
245 sut->read_one_message_and_dispatch();
246 }
247
248 TEST_F(Timers_test_suite, read_one_message_failed_read) {
249 EXPECT_CALL(*mock_vio, set_timeout_in_ms(
250 iface::Vio::Direction::k_read,
251 defaults::timeout::k_interactive_timeout * 1000));
252
253 EXPECT_CALL(*mock_vio, read(_, _))
254 .WillRepeatedly(SetSocketErrnoAndReturn(SOCKET_ETIMEDOUT, -1));
255 EXPECT_CALL(*mock_vio, set_state(_)).Times(1);
256
257 EXPECT_CALL(*mock_protocol_monitor, on_receive(_)).Times(0);
258 EXPECT_CALL(*mock_session, set_proto(_));
259
260 auto encoder = ngs::allocate_object<mock::Protocol_encoder>();
261 ngs::Memory_block_pool memory_block_pool{{0, k_minimum_page_size}};
262 protocol::Encoding_pool pool(0, &memory_block_pool);
263 protocol::Encoding_buffer buffer(&pool);
264 protocol::XMessage_encoder low_level_encoder(&buffer);
265 ngs::Protocol_flusher flusher(&buffer, &low_level_encoder,
266 mock_protocol_monitor, mock_vio, [](int) {});
267 EXPECT_CALL(*encoder, get_flusher()).WillRepeatedly(Return(&flusher));
268 sut->set_encoder(encoder);
269
270 // queue up notice for future send
271 EXPECT_CALL(mock_notice_output_queue, emplace(_));
272
273 sut->read_one_message_and_dispatch();
274 }
275
276 TEST_F(Timers_test_suite, send_message_default_write_timeout) {
277 EXPECT_CALL(*mock_vio, get_fd());
278 Expectation set_timeout_exp = EXPECT_CALL(
279 *mock_vio, set_timeout_in_ms(iface::Vio::Direction::k_write,
280 defaults::timeout::k_write_timeout * 1000));
281
282 EXPECT_CALL(*mock_vio, write(_, _)).After(set_timeout_exp);
283 EXPECT_CALL(*mock_session, set_proto(_));
284
285 auto stub_error_handler = [](int) {};
286 auto encoder = ngs::allocate_object<ngs::Protocol_encoder>(
287 mock_vio, stub_error_handler, mock_protocol_monitor, &m_pool);
288 sut->set_encoder(encoder);
289 encoder->send_protobuf_message(Mysqlx::ServerMessages::OK, Mysqlx::Ok(),
290 false);
291 }
292
293 TEST_F(Timers_test_suite, send_message_custom_write_timeout) {
294 config->m_timeouts.m_write_timeout = 44;
295 sut->set_write_timeout(config->m_timeouts.m_write_timeout);
296
297 EXPECT_CALL(*mock_session, set_proto(_));
298
299 EXPECT_CALL(*mock_vio, get_fd());
300 Expectation set_timeout_exp = EXPECT_CALL(
301 *mock_vio, set_timeout_in_ms(iface::Vio::Direction::k_write, 44 * 1000));
302
303 EXPECT_CALL(*mock_vio, write(_, _)).After(set_timeout_exp);
304
305 auto stub_error_handler = [](int) {};
306 auto encoder = ngs::allocate_object<ngs::Protocol_encoder>(
307 mock_vio, stub_error_handler, mock_protocol_monitor, &m_pool);
308 sut->set_encoder(encoder);
309 encoder->send_protobuf_message(Mysqlx::ServerMessages::OK, Mysqlx::Ok(),
310 false);
311 }
312
313 TEST_F(Timers_test_suite, send_message_failed_write) {
314 EXPECT_CALL(*mock_vio, get_fd());
315 EXPECT_CALL(*mock_vio,
316 set_timeout_in_ms(iface::Vio::Direction::k_write,
317 defaults::timeout::k_write_timeout * 1000));
318
319 ON_CALL(*mock_vio, write(_, _)).WillByDefault(Return(-1));
320 EXPECT_CALL(*mock_vio, write(_, _));
321 EXPECT_CALL(*mock_session, set_proto(_));
322
323 struct CustomExpection {};
324 auto stub_error_handler = [](int) { throw CustomExpection(); };
325 auto encoder = ngs::allocate_object<ngs::Protocol_encoder>(
326 mock_vio, stub_error_handler, mock_protocol_monitor, &m_pool);
327 sut->set_encoder(encoder);
328
329 // write failed so error_handler should be used
330 EXPECT_THROW(encoder->send_protobuf_message(Mysqlx::ServerMessages::OK,
331 Mysqlx::Ok(), false),
332 CustomExpection);
333 }
334
335 } // namespace test
336 } // namespace xpl