Debian Codegraph @

unittest/gunit/xplugin/xpl/sync_variable_t.cc

raw symbols SCIP
1 /* Copyright (c) 2015, 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
26 #include <cstddef>
27 #include <thread> // NOLINT(build/c++11)
28
29 #include "my_sys.h" // NOLINT(build/include_subdir)
30 #include "my_systime.h" // my_sleep() NOLINT(build/include_subdir)
31
32 #include "plugin/x/src/helper/multithread/sync_variable.h"
33 #include "plugin/x/src/ngs/thread.h"
34
35 namespace xpl {
36
37 namespace test {
38
39 const int EXPECTED_VALUE_FIRST = 10;
40 const int EXPECTED_VALUE_SECOND = 20;
41 const int EXPECTED_VALUE_THRID = 30;
42 const int EXPECTED_VALUE_SET = 40;
43 const int EXPECTED_VALUE_SET_EXPECT = 50;
44
45 class Xpl_sync_variable : public ::testing::Test {
46 public:
47 Xpl_sync_variable()
48 : m_sut(EXPECTED_VALUE_FIRST, PSI_NOT_INSTRUMENTED, PSI_NOT_INSTRUMENTED),
49 m_thread_ended(false) {}
50
51 static void *start_routine_set(void *data) {
52 Xpl_sync_variable *self = static_cast<Xpl_sync_variable *>(data);
53 self->set_value();
54
55 return nullptr;
56 }
57
58 static void *start_routine_set_and_expect(void *data) {
59 Xpl_sync_variable *self = static_cast<Xpl_sync_variable *>(data);
60 self->set_value();
61 self->m_sut.wait_for(EXPECTED_VALUE_SET_EXPECT);
62
63 return nullptr;
64 }
65
66 void set_value() {
67 my_sleep(1);
68 m_thread_ended = true;
69 m_sut.set(EXPECTED_VALUE_SET);
70 }
71
72 Sync_variable<int> m_sut;
73
74 volatile bool m_thread_ended;
75 };
76
77 TEST_F(Xpl_sync_variable, is_returnConstructorInitializedValue) {
78 ASSERT_TRUE(m_sut.is(EXPECTED_VALUE_FIRST));
79 }
80
81 TEST_F(Xpl_sync_variable, is_returnChangedValue) {
82 m_sut.set(EXPECTED_VALUE_SECOND);
83
84 ASSERT_TRUE(m_sut.is(EXPECTED_VALUE_SECOND));
85 }
86
87 TEST_F(Xpl_sync_variable, is_returnChangedValue_afterSetWasCalled) {
88 m_sut.set(EXPECTED_VALUE_SECOND);
89
90 ASSERT_TRUE(m_sut.is(EXPECTED_VALUE_SECOND));
91 }
92
93 TEST_F(Xpl_sync_variable, is_exchangeSuccesses_whenCurrentValueMatches) {
94 ASSERT_TRUE(m_sut.exchange(EXPECTED_VALUE_FIRST, EXPECTED_VALUE_SECOND));
95 ASSERT_TRUE(m_sut.is(EXPECTED_VALUE_SECOND));
96 }
97
98 TEST_F(Xpl_sync_variable, is_exchangeFails_whenCurrentValueDoesntMatches) {
99 ASSERT_FALSE(m_sut.exchange(EXPECTED_VALUE_THRID, EXPECTED_VALUE_SECOND));
100 ASSERT_FALSE(m_sut.is(EXPECTED_VALUE_SECOND));
101 ASSERT_TRUE(m_sut.is(EXPECTED_VALUE_FIRST));
102 }
103
104 TEST_F(Xpl_sync_variable, wait_returnsRightAway_whenCurrentValueMatches) {
105 m_sut.wait_for(EXPECTED_VALUE_FIRST);
106 }
107
108 TEST_F(Xpl_sync_variable,
109 wait_returnsRightAway_whenCurrentValueInArrayMatches) {
110 int VALUES[] = {EXPECTED_VALUE_SECOND, EXPECTED_VALUE_FIRST};
111 m_sut.wait_for(VALUES);
112 }
113
114 TEST_F(Xpl_sync_variable, wait_returnsRightAway_whenNewValueMatches) {
115 m_sut.set(EXPECTED_VALUE_SECOND);
116 m_sut.wait_for(EXPECTED_VALUE_SECOND);
117 }
118
119 TEST_F(Xpl_sync_variable, set_returnsOldValue) {
120 ASSERT_EQ(EXPECTED_VALUE_FIRST,
121 m_sut.set_and_return_old(EXPECTED_VALUE_SET_EXPECT));
122 ASSERT_EQ(EXPECTED_VALUE_SET_EXPECT,
123 m_sut.set_and_return_old(EXPECTED_VALUE_SECOND));
124 ASSERT_EQ(EXPECTED_VALUE_SECOND,
125 m_sut.set_and_return_old(EXPECTED_VALUE_FIRST));
126 }
127
128 TEST_F(Xpl_sync_variable,
129 wait_returnsRightAway_whenNewCurrentValueInArrayMatches) {
130 int VALUES[] = {EXPECTED_VALUE_SECOND, EXPECTED_VALUE_FIRST};
131 m_sut.set(EXPECTED_VALUE_SECOND);
132 m_sut.wait_for(VALUES);
133 }
134
135 TEST_F(Xpl_sync_variable,
136 wait_returnsDelayed_whenThreadChangesValueAndItsExpected) {
137 std::thread t(&Xpl_sync_variable::start_routine_set, this);
138 m_sut.wait_for(EXPECTED_VALUE_SET);
139 t.join();
140
141 ASSERT_TRUE(m_thread_ended); // Verify that the exit was triggerd by thread
142 }
143
144 TEST_F(
145 Xpl_sync_variable,
146 wait_returnsDelayed_whenThreadChangesValueAndItsInArrayOfExpectedValues) {
147 std::thread t(&Xpl_sync_variable::start_routine_set_and_expect, this);
148 int VALUES[] = {EXPECTED_VALUE_SET};
149 m_sut.wait_for_and_set(VALUES, EXPECTED_VALUE_SET_EXPECT);
150 t.join();
151
152 ASSERT_TRUE(m_thread_ended); // Verify that the exit was triggerd by thread
153 ASSERT_TRUE(m_sut.is(EXPECTED_VALUE_SET_EXPECT));
154 }
155
156 } // namespace test
157 } // namespace xpl