| 1 | /* Copyright (c) 2020, 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 <atomic> |
| 27 | #include <thread> // NOLINT(build/c++11) |
| 28 | |
| 29 | #include "plugin/x/src/ngs/socket_events.h" |
| 30 | |
| 31 | namespace ngs { |
| 32 | namespace test { |
| 33 | |
| 34 | class Socket_events_task_suite : public ::testing::Test { |
| 35 | public: |
| 36 | Socket_events m_sut; |
| 37 | }; |
| 38 | |
| 39 | TEST_F(Socket_events_task_suite, loop_doesnt_block_when_no_events) { |
| 40 | m_sut.loop(); |
| 41 | } |
| 42 | |
| 43 | TEST_F(Socket_events_task_suite, execute_loop_until_no_events) { |
| 44 | std::atomic<uint64_t> execution_count{4}; |
| 45 | m_sut.add_timer(10, [&execution_count]() { return --execution_count; }); |
| 46 | m_sut.loop(); |
| 47 | ASSERT_EQ(0, execution_count); |
| 48 | } |
| 49 | |
| 50 | TEST_F(Socket_events_task_suite, |
| 51 | break_loop_is_queued_and_ignores_active_events) { |
| 52 | std::atomic<uint64_t> execution_count{0}; |
| 53 | |
| 54 | m_sut.break_loop(); |
| 55 | m_sut.add_timer(10, [&execution_count]() { |
| 56 | ++execution_count; |
| 57 | return true; |
| 58 | }); |
| 59 | m_sut.loop(); |
| 60 | ASSERT_EQ(0, execution_count); |
| 61 | } |
| 62 | |
| 63 | TEST_F(Socket_events_task_suite, break_loop_from_thread) { |
| 64 | std::atomic<uint64_t> execution_count{0}; |
| 65 | |
| 66 | std::thread break_thread{[this, &execution_count]() { |
| 67 | while (execution_count.load() < 10) { |
| 68 | std::this_thread::yield(); |
| 69 | } |
| 70 | m_sut.break_loop(); |
| 71 | }}; |
| 72 | |
| 73 | m_sut.add_timer(10, [&execution_count]() { |
| 74 | ++execution_count; |
| 75 | return true; |
| 76 | }); |
| 77 | m_sut.loop(); |
| 78 | ASSERT_LT(0, execution_count.load()); |
| 79 | break_thread.join(); |
| 80 | } |
| 81 | |
| 82 | TEST_F(Socket_events_task_suite, break_loop_from_thread_always_active) { |
| 83 | std::atomic<uint64_t> execution_count{0}; |
| 84 | |
| 85 | std::thread break_thread{[this, &execution_count]() { |
| 86 | while (execution_count.load() < 10) { |
| 87 | std::this_thread::yield(); |
| 88 | } |
| 89 | m_sut.break_loop(); |
| 90 | }}; |
| 91 | |
| 92 | m_sut.add_timer(0, [&execution_count]() { |
| 93 | ++execution_count; |
| 94 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 95 | return true; |
| 96 | }); |
| 97 | m_sut.loop(); |
| 98 | ASSERT_LT(0, execution_count.load()); |
| 99 | break_thread.join(); |
| 100 | } |
| 101 | |
| 102 | } // namespace test |
| 103 | } // namespace ngs |