| 1 | /* Copyright (c) 2009, 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 "sql/mdl.h" |
| 27 | #include "unittest/gunit/thread_utils.h" |
| 28 | |
| 29 | using thread::Notification; |
| 30 | using thread::Thread; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | const int counter_start_value = 42; |
| 35 | |
| 36 | class NotificationThread : public Thread { |
| 37 | public: |
| 38 | NotificationThread(Notification *start_notification, |
| 39 | Notification *end_notfication, int *counter) |
| 40 | : m_start_notification(start_notification), |
| 41 | m_end_notification(end_notfication), |
| 42 | m_counter(counter) {} |
| 43 | |
| 44 | void run() override { |
| 45 | // Verify counter, increment it, notify the main thread. |
| 46 | EXPECT_EQ(counter_start_value, *m_counter); |
| 47 | (*m_counter) += 1; |
| 48 | m_start_notification->notify(); |
| 49 | |
| 50 | // Wait for notification from other thread. |
| 51 | m_end_notification->wait_for_notification(); |
| 52 | EXPECT_EQ(counter_start_value, *m_counter); |
| 53 | |
| 54 | // Set counter again before returning from thread. |
| 55 | (*m_counter) += 1; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | Notification *m_start_notification; |
| 60 | Notification *m_end_notification; |
| 61 | int *m_counter; |
| 62 | |
| 63 | NotificationThread(const NotificationThread &); // Not copyable. |
| 64 | void operator=(const NotificationThread &); // Not assignable. |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | A basic, single-threaded test of Notification. |
| 69 | */ |
| 70 | TEST(Notification, Notify) { |
| 71 | Notification notification; |
| 72 | EXPECT_FALSE(notification.has_been_notified()); |
| 73 | notification.notify(); |
| 74 | EXPECT_TRUE(notification.has_been_notified()); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | Starts a thread, and verifies that the notification/synchronization |
| 79 | mechanism works. |
| 80 | */ |
| 81 | TEST(NotificationThread, StartAndWait) { |
| 82 | Notification start_notification; |
| 83 | Notification end_notfication; |
| 84 | int counter = counter_start_value; |
| 85 | NotificationThread notification_thread(&start_notification, &end_notfication, |
| 86 | &counter); |
| 87 | notification_thread.start(); |
| 88 | |
| 89 | // Wait for the other thread to increment counter, and notify us. |
| 90 | start_notification.wait_for_notification(); |
| 91 | EXPECT_EQ(counter_start_value + 1, counter); |
| 92 | EXPECT_TRUE(start_notification.has_been_notified()); |
| 93 | |
| 94 | // Reset counter, and notify other thread. |
| 95 | counter = counter_start_value; |
| 96 | end_notfication.notify(); |
| 97 | notification_thread.join(); |
| 98 | |
| 99 | // We should see the final results of the thread we have joined. |
| 100 | EXPECT_EQ(counter_start_value + 1, counter); |
| 101 | } |
| 102 | |
| 103 | } // namespace |