Debian Codegraph @

unittest/gunit/xplugin/xcl/protocol_notices_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2017, 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, version 2.0, 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 #include "unittest/gunit/xplugin/xcl/message_helpers.h"
27 #include "unittest/gunit/xplugin/xcl/protocol_t.h"
28
29 namespace xcl {
30 namespace test {
31
32 using ::testing::Eq;
33 using ::testing::StrEq;
34
35 class Xcl_protocol_impl_tests_notices : public Xcl_protocol_impl_tests {
36 public:
37 using Notice = ::Mysqlx::Notice::Frame;
38 using Notice_type = ::Mysqlx::Notice::Frame::Type;
39
40 public:
41 void expect_notice_handler(
42 mock::Message_handlers *mock, const Notice_type expected_type,
43 const std::string &expected_payload,
44 const Handler_result consume = Handler_result::Continue) {
45 const bool expect_global = true;
46
47 EXPECT_CALL(*mock,
48 notice_handler(m_sut.get(), expect_global, expected_type,
49 StrEq(expected_payload),
50 static_cast<uint32_t>(expected_payload.size())))
51 .WillOnce(Return(consume));
52 }
53
54 void expect_notice_handler_empty_payload(
55 mock::Message_handlers *mock, const Notice_type expected_type,
56 const Handler_result consume = Handler_result::Continue) {
57 const bool expect_global = true;
58 const uint32_t expected_payload_size = 0;
59
60 EXPECT_CALL(*mock, notice_handler(m_sut.get(), expect_global, expected_type,
61 nullptr, expected_payload_size))
62 .WillOnce(Return(consume));
63 }
64
65 template <typename Message_type>
66 void expect_recv_message_handler(
67 const Message_from_str<Message_type> &message,
68 const Handler_result should_consumed = Handler_result::Continue) {
69 const Message_type &m = message.get();
70 expect_recv_message_handler(m, should_consumed);
71 }
72
73 template <typename Message_type>
74 void expect_recv_message_handler(
75 const Message_type &message,
76 const Handler_result should_consumed = Handler_result::Continue) {
77 EXPECT_CALL(m_mock_message_handler,
78 received_message_handler(m_sut.get(),
79 Server_message<Message_type>::get_id(),
80 Cmp_msg(message)))
81 .WillOnce(Return(should_consumed));
82 }
83
84 StrictMock<mock::Message_handlers> m_mock_message_handler;
85 StrictMock<mock::Message_handlers> m_mock_notice_handlers[2];
86 const Message_from_str<Notice> m_msg_notice1{"type: 2 "};
87 const Message_from_str<Notice> m_msg_notice2{
88 "type: 3 "
89 "payload: \"0123456789\""};
90 const std::string m_notice1_payload = "";
91 const std::string m_notice2_payload = "0123456789";
92 };
93
94 TEST_F(Xcl_protocol_impl_tests_notices, recv_notice_when_no_handler_installed) {
95 XProtocol::Server_message_type_id out_id;
96 XError out_error;
97
98 expect_read_message(m_msg_notice1.get());
99 auto message = m_sut->recv_single_message(&out_id, &out_error);
100
101 EXPECT_THAT(*message, Cmp_msg(m_msg_notice1.get()));
102 }
103
104 TEST_F(Xcl_protocol_impl_tests_notices, recv_notice_handler) {
105 XProtocol::Server_message_type_id out_id;
106 XError out_error;
107
108 expect_read_message(m_msg_notice1.get());
109 expect_notice_handler_empty_payload(
110 &m_mock_notice_handlers[0],
111 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED);
112
113 m_sut->add_notice_handler(
114 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
115
116 auto message = m_sut->recv_single_message(&out_id, &out_error);
117
118 EXPECT_THAT(*message, Cmp_msg(m_msg_notice1.get()));
119 }
120
121 TEST_F(Xcl_protocol_impl_tests_notices,
122 recv_msg_handler_called_before_notice_handler) {
123 XProtocol::Server_message_type_id out_id;
124 XError out_error;
125 InSequence sequence;
126
127 expect_read_message(m_msg_notice1.get());
128 expect_recv_message_handler(m_msg_notice1.get());
129 expect_notice_handler_empty_payload(
130 &m_mock_notice_handlers[0],
131 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED);
132
133 m_sut->add_received_message_handler(
134 m_mock_message_handler.get_mock_lambda_received_message_handler());
135 m_sut->add_notice_handler(
136 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
137
138 auto message = m_sut->recv_single_message(&out_id, &out_error);
139
140 EXPECT_THAT(*message, Cmp_msg(m_msg_notice1.get()));
141 }
142
143 TEST_F(Xcl_protocol_impl_tests_notices,
144 recv_msg_handler_consumed_notice_recv_next_msg) {
145 XProtocol::Server_message_type_id out_id;
146 XError out_error;
147 ::Mysqlx::Ok msg_ok;
148 InSequence sequence;
149
150 expect_read_message(m_msg_notice1.get());
151 expect_recv_message_handler(m_msg_notice1.get(), Handler_result::Consumed);
152 expect_read_message_without_payload(msg_ok);
153 expect_recv_message_handler(msg_ok);
154
155 m_sut->add_received_message_handler(
156 m_mock_message_handler.get_mock_lambda_received_message_handler());
157 m_sut->add_notice_handler(
158 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
159
160 auto message = m_sut->recv_single_message(&out_id, &out_error);
161
162 EXPECT_THAT(*message, Cmp_msg(msg_ok));
163 }
164
165 TEST_F(Xcl_protocol_impl_tests_notices,
166 recv_other_msg_notice_handler_not_called) {
167 XProtocol::Server_message_type_id out_id;
168 XError out_error;
169 auto msg_error = Server_message<::Mysqlx::Error>::make_required();
170
171 expect_read_message(msg_error);
172
173 m_sut->add_notice_handler(
174 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
175
176 auto message = m_sut->recv_single_message(&out_id, &out_error);
177
178 EXPECT_THAT(*message, Cmp_msg(msg_error));
179 }
180
181 TEST_F(Xcl_protocol_impl_tests_notices, recv_multiple_notice_handlers) {
182 XProtocol::Server_message_type_id out_id;
183 XError out_error;
184 InSequence sequence;
185
186 expect_read_message(m_msg_notice1.get());
187 expect_notice_handler_empty_payload(
188 &m_mock_notice_handlers[1],
189 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED);
190 expect_notice_handler_empty_payload(
191 &m_mock_notice_handlers[0],
192 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED);
193
194 m_sut->add_notice_handler(
195 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
196 m_sut->add_notice_handler(
197 m_mock_notice_handlers[1].get_mock_lambda_notice_handler());
198
199 auto message = m_sut->recv_single_message(&out_id, &out_error);
200
201 EXPECT_THAT(*message, Cmp_msg(m_msg_notice1.get()));
202 }
203
204 TEST_F(Xcl_protocol_impl_tests_notices,
205 recv_pushed_multiple_notice_handler_and_poped_some_of_them) {
206 XProtocol::Server_message_type_id out_id;
207 XError out_error;
208
209 expect_read_message(m_msg_notice1.get());
210
211 // The handlers are held in "stack" container,
212 // thus after calling "pop" should leave
213 // in the container first handler
214 expect_notice_handler_empty_payload(
215 &m_mock_notice_handlers[0],
216 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED);
217
218 m_sut->add_notice_handler(
219 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
220 const auto id = m_sut->add_notice_handler(
221 m_mock_notice_handlers[1].get_mock_lambda_notice_handler());
222 m_sut->remove_notice_handler(id);
223
224 auto message = m_sut->recv_single_message(&out_id, &out_error);
225
226 EXPECT_THAT(*message, Cmp_msg(m_msg_notice1.get()));
227 }
228
229 TEST_F(Xcl_protocol_impl_tests_notices,
230 recv_multiple_notice_handlers_consume1) {
231 XProtocol::Server_message_type_id out_id;
232 XError out_error;
233 InSequence sequence;
234
235 expect_read_message(m_msg_notice1.get());
236 // First handler returns true, and consumes the notice.
237 // Because of that second handler isn't called
238 // for this notice, and `recv_single_message` must receive
239 // next message.
240 expect_notice_handler_empty_payload(
241 &m_mock_notice_handlers[1],
242 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED,
243 Handler_result::Consumed);
244
245 expect_read_message(m_msg_notice2.get());
246 expect_notice_handler(&m_mock_notice_handlers[1],
247 Notice_type::Frame_Type_SESSION_STATE_CHANGED,
248 m_notice2_payload);
249 expect_notice_handler(&m_mock_notice_handlers[0],
250 Notice_type::Frame_Type_SESSION_STATE_CHANGED,
251 m_notice2_payload);
252
253 m_sut->add_notice_handler(
254 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
255 m_sut->add_notice_handler(
256 m_mock_notice_handlers[1].get_mock_lambda_notice_handler());
257
258 auto message = m_sut->recv_single_message(&out_id, &out_error);
259
260 EXPECT_THAT(*message, Cmp_msg(m_msg_notice2.get()));
261 }
262
263 TEST_F(Xcl_protocol_impl_tests_notices,
264 recv_multiple_notice_handlers_consume2) {
265 XProtocol::Server_message_type_id out_id;
266 XError out_error;
267 InSequence sequence;
268
269 expect_read_message(m_msg_notice1.get());
270 // Second handler returns true, and consumes the notice.
271 // Because of that second handler isn't called
272 // for this notice, and `recv_single_message` must receive
273 // next message.
274 expect_notice_handler_empty_payload(
275 &m_mock_notice_handlers[1],
276 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED);
277 expect_notice_handler_empty_payload(
278 &m_mock_notice_handlers[0],
279 Notice_type::Frame_Type_SESSION_VARIABLE_CHANGED,
280 Handler_result::Consumed);
281
282 expect_read_message(m_msg_notice2.get());
283 expect_notice_handler(&m_mock_notice_handlers[1],
284 Notice_type::Frame_Type_SESSION_STATE_CHANGED,
285 m_notice2_payload);
286 expect_notice_handler(&m_mock_notice_handlers[0],
287 Notice_type::Frame_Type_SESSION_STATE_CHANGED,
288 m_notice2_payload);
289
290 m_sut->add_notice_handler(
291 m_mock_notice_handlers[0].get_mock_lambda_notice_handler());
292 m_sut->add_notice_handler(
293 m_mock_notice_handlers[1].get_mock_lambda_notice_handler());
294
295 auto message = m_sut->recv_single_message(&out_id, &out_error);
296
297 EXPECT_THAT(*message, Cmp_msg(m_msg_notice2.get()));
298 }
299
300 } // namespace test
301 } // namespace xcl