| 1 | /* Copyright (c) 2021, 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 <gtest/gtest.h> |
| 25 | #include <string> |
| 26 | |
| 27 | #include "libchangestreams/include/mysql/cs/reader/state.h" |
| 28 | |
| 29 | namespace cs::reader::unittests { |
| 30 | |
| 31 | const std::string DEFAULT_UUID1 = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; |
| 32 | const std::string DEFAULT_UUID2 = "aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb"; |
| 33 | |
| 34 | class ReaderStateTest : public ::testing::Test { |
| 35 | protected: |
| 36 | binary_log::gtids::Uuid valid_uuid1; |
| 37 | binary_log::gtids::Uuid valid_uuid2; |
| 38 | cs::reader::State state1; |
| 39 | |
| 40 | binary_log::gtids::Gtid gtid1_1{valid_uuid1, 0}; |
| 41 | binary_log::gtids::Gtid gtid1_2{valid_uuid1, 0}; |
| 42 | binary_log::gtids::Gtid gtid2_1{valid_uuid1, 0}; |
| 43 | binary_log::gtids::Gtid gtid1_1_copy{valid_uuid1, 0}; |
| 44 | |
| 45 | ReaderStateTest() = default; |
| 46 | |
| 47 | void SetUp() override { |
| 48 | valid_uuid1.parse(DEFAULT_UUID1.c_str(), DEFAULT_UUID1.size()); |
| 49 | valid_uuid2.parse(DEFAULT_UUID2.c_str(), DEFAULT_UUID2.size()); |
| 50 | |
| 51 | gtid1_1 = {valid_uuid1, 1}; |
| 52 | gtid1_2 = {valid_uuid1, 2}; |
| 53 | gtid2_1 = {valid_uuid2, 1}; |
| 54 | gtid1_1_copy = {valid_uuid1, 1}; |
| 55 | } |
| 56 | |
| 57 | void TearDown() override {} |
| 58 | }; |
| 59 | |
| 60 | TEST_F(ReaderStateTest, StateReset) { |
| 61 | state1.add_gtid(gtid1_2); |
| 62 | state1.add_gtid(gtid2_1); |
| 63 | state1.add_gtid(gtid1_1_copy); |
| 64 | |
| 65 | ASSERT_TRUE(state1.get_gtids().contains(gtid1_1)); |
| 66 | ASSERT_TRUE(state1.get_gtids().contains(gtid1_2)); |
| 67 | ASSERT_TRUE(state1.get_gtids().contains(gtid2_1)); |
| 68 | |
| 69 | // clear original state |
| 70 | state1.reset(); |
| 71 | |
| 72 | // assert that the original state is empty |
| 73 | ASSERT_EQ(state1.get_gtids().count(), 0); |
| 74 | |
| 75 | ASSERT_FALSE(state1.get_gtids().contains(gtid1_1)); |
| 76 | ASSERT_FALSE(state1.get_gtids().contains(gtid1_2)); |
| 77 | ASSERT_FALSE(state1.get_gtids().contains(gtid2_1)); |
| 78 | |
| 79 | state1.add_gtid(gtid1_1); |
| 80 | ASSERT_TRUE(state1.get_gtids().contains(gtid1_1)); |
| 81 | |
| 82 | state1.reset(); |
| 83 | } |
| 84 | |
| 85 | TEST_F(ReaderStateTest, StateAddGtidSet) { |
| 86 | binary_log::gtids::Gtid_set set; |
| 87 | cs::reader::State state; |
| 88 | |
| 89 | set.add(gtid1_1); |
| 90 | set.add(gtid1_2); |
| 91 | set.add(gtid2_1); |
| 92 | |
| 93 | state.add_gtid_set(set); |
| 94 | |
| 95 | // assert that the size of the gtids in the state is 3 |
| 96 | ASSERT_EQ(state.get_gtids().count(), 3); |
| 97 | |
| 98 | // assert that the gtids added are those expected |
| 99 | ASSERT_TRUE(state.get_gtids().contains(gtid1_1)); |
| 100 | ASSERT_TRUE(state.get_gtids().contains(gtid1_2)); |
| 101 | ASSERT_TRUE(state.get_gtids().contains(gtid2_1)); |
| 102 | } |
| 103 | |
| 104 | TEST_F(ReaderStateTest, StateCopyAssignmentOperator) { |
| 105 | state1.add_gtid(gtid1_1); |
| 106 | state1.add_gtid(gtid1_2); |
| 107 | state1.add_gtid(gtid2_1); |
| 108 | state1.add_gtid(gtid1_1_copy); |
| 109 | |
| 110 | // assignment operator |
| 111 | cs::reader::State state2 = state1; |
| 112 | |
| 113 | // check that original and copy states match |
| 114 | ASSERT_EQ(state1, state2); |
| 115 | |
| 116 | ASSERT_TRUE(state2.get_gtids().contains(gtid1_1)); |
| 117 | ASSERT_TRUE(state2.get_gtids().contains(gtid1_2)); |
| 118 | ASSERT_TRUE(state2.get_gtids().contains(gtid2_1)); |
| 119 | |
| 120 | // clear original state |
| 121 | state1.reset(); |
| 122 | |
| 123 | // assert original and copy are not equal anymore |
| 124 | ASSERT_FALSE(state1 == state2); |
| 125 | } |
| 126 | |
| 127 | } // namespace cs::reader::unittests |