Debian Codegraph @

unittest/gunit/xplugin/xpl/broker_input_queue_task_t.cc

raw symbols SCIP
1 /* Copyright (c) 2018, 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 #include <chrono> // NOLINT(build/c++11)
27 #include <thread> // NOLINT(build/c++11)
28
29 #include "plugin/x/src/mq/broker_task.h"
30 #include "plugin/x/src/mq/notice_input_queue.h"
31 #include "unittest/gunit/xplugin/xpl/mock/client.h"
32 #include "unittest/gunit/xplugin/xpl/mock/notice_output_queue.h"
33 #include "unittest/gunit/xplugin/xpl/mock/session.h"
34
35 namespace xpl {
36 namespace test {
37
38 using ::testing::_;
39 using ::testing::InSequence;
40 using ::testing::Return;
41 using ::testing::ReturnRef;
42 using ::testing::StrictMock;
43 using ::testing::Test;
44
45 using Notice_type = ngs::Notice_type;
46 using Notice_descriptor = ngs::Notice_descriptor;
47
48 const auto kId1 = Notice_type::k_group_replication_quorum_loss;
49 const auto kId2 = Notice_type::k_group_replication_view_changed;
50
51 class Broker_input_queue_testsuite : public Test {
52 public:
53 using Task_context = iface::Server_task::Task_context;
54
55 public:
56 void SetUp() override {
57 m_client_list.add(mock_client);
58 sut_task->prepare(&m_sut_context);
59
60 EXPECT_CALL(*mock_client, session()).WillRepeatedly(Return(&mock_session));
61 EXPECT_CALL(mock_session, get_notice_output_queue())
62 .WillRepeatedly(ReturnRef(mock_notice_out_queue));
63 }
64
65 void assert_do_loop() { sut_task->loop(); }
66
67 std::shared_ptr<mock::Client> mock_client{new mock::Client()};
68 mock::Session mock_session;
69 StrictMock<mock::Notice_output_queue> mock_notice_out_queue;
70
71 ngs::Client_list m_client_list;
72 Task_context m_sut_context{Task_context::On_connection(), nullptr,
73 &m_client_list};
74 Notice_input_queue m_sut_queue;
75 std::unique_ptr<iface::Server_task> sut_task{
76 m_sut_queue.create_broker_task()};
77 };
78
79 MATCHER_P(EqNoticeType, expected, "") { return expected == arg->m_notice_type; }
80
81 TEST_F(Broker_input_queue_testsuite, does_nothing) {}
82
83 TEST_F(Broker_input_queue_testsuite, client_doesnt_have_session) {
84 m_sut_queue.emplace(kId1, "payload");
85
86 EXPECT_CALL(*mock_client, session()).WillOnce(Return(nullptr));
87
88 sut_task->loop();
89 }
90
91 TEST_F(Broker_input_queue_testsuite, queues_all_until_looped2) {
92 m_sut_queue.emplace(kId1, "payload");
93 m_sut_queue.emplace(kId2, "payload");
94
95 EXPECT_CALL(mock_notice_out_queue, emplace(_)).Times(2);
96
97 sut_task->loop();
98 }
99
100 TEST_F(Broker_input_queue_testsuite, queues_all_until_looped4) {
101 m_sut_queue.emplace(kId1, "payload");
102 m_sut_queue.emplace(kId2, "payload");
103 m_sut_queue.emplace(kId2, "payload");
104
105 EXPECT_CALL(mock_notice_out_queue, emplace(_)).Times(3);
106
107 sut_task->loop();
108 }
109
110 TEST_F(Broker_input_queue_testsuite, publish_sequence_is_same_as_queue) {
111 m_sut_queue.emplace(kId1, "payload");
112 m_sut_queue.emplace(kId2, "payload");
113 m_sut_queue.emplace(kId2, "payload");
114 m_sut_queue.emplace(kId1, "payload");
115 m_sut_queue.emplace(kId2, "payload");
116
117 InSequence s;
118 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId1)))
119 .RetiresOnSaturation();
120 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId2)))
121 .RetiresOnSaturation();
122 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId2)))
123 .RetiresOnSaturation();
124 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId1)))
125 .RetiresOnSaturation();
126 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId2)))
127 .RetiresOnSaturation();
128
129 sut_task->loop();
130 }
131
132 TEST_F(Broker_input_queue_testsuite, queues_one_by_one) {
133 m_sut_queue.emplace(kId1, "payload");
134 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId1)));
135 assert_do_loop();
136
137 m_sut_queue.emplace(kId2, "payload");
138 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId2)));
139 assert_do_loop();
140
141 m_sut_queue.emplace(kId1, "payload");
142 EXPECT_CALL(mock_notice_out_queue, emplace(EqNoticeType(kId1)));
143 assert_do_loop();
144 }
145
146 TEST_F(Broker_input_queue_testsuite, stop_blocks_until_task_ended) {
147 bool stop_ended = false;
148
149 sut_task->pre_loop();
150 std::thread call_stop([&stop_ended, this]() {
151 sut_task->stop(iface::Server_task::Stop_cause::k_normal_shutdown);
152 stop_ended = true;
153 });
154
155 std::this_thread::sleep_for(std::chrono::milliseconds(300));
156 ASSERT_FALSE(stop_ended);
157
158 assert_do_loop();
159 std::this_thread::sleep_for(std::chrono::milliseconds(300));
160 ASSERT_FALSE(stop_ended);
161
162 sut_task->post_loop();
163 call_stop.join();
164 ASSERT_TRUE(stop_ended);
165 }
166
167 } // namespace test
168 } // namespace xpl