| 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 | #include <algorithm> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "plugin/x/src/helper/multithread/cond.h" |
| 29 | #include "plugin/x/src/helper/multithread/mutex.h" |
| 30 | #include "plugin/x/src/ngs/scheduler.h" |
| 31 | |
| 32 | namespace xpl { |
| 33 | |
| 34 | namespace test { |
| 35 | |
| 36 | template <typename T> |
| 37 | struct Result_collector { |
| 38 | void task(const T &value) { |
| 39 | { |
| 40 | MUTEX_LOCK(lock, m_result_mutex); |
| 41 | |
| 42 | m_result.push_back(value); |
| 43 | } |
| 44 | |
| 45 | m_check_task_count_cond.signal(m_check_task_count_mutex); |
| 46 | } |
| 47 | |
| 48 | ngs::Scheduler_dynamic::Task *new_task(const T &value) { |
| 49 | return new ngs::Scheduler_dynamic::Task( |
| 50 | std::bind(&Result_collector::task, this, value)); |
| 51 | } |
| 52 | |
| 53 | void wait(size_t task_count) { |
| 54 | MUTEX_LOCK(lock, m_check_task_count_mutex); |
| 55 | |
| 56 | while (m_result.size() != task_count) |
| 57 | m_check_task_count_cond.wait(m_check_task_count_mutex); |
| 58 | } |
| 59 | |
| 60 | Mutex m_check_task_count_mutex{PSI_NOT_INSTRUMENTED}; |
| 61 | Cond m_check_task_count_cond{PSI_NOT_INSTRUMENTED}; |
| 62 | Mutex m_result_mutex{PSI_NOT_INSTRUMENTED}; |
| 63 | std::vector<T> m_result; |
| 64 | }; |
| 65 | |
| 66 | TEST(xpl_scheduler_dynamic, DISABLED_run_1000_tasks) { |
| 67 | const unsigned int TASK_COUNT = 1000; |
| 68 | |
| 69 | ngs::Scheduler_dynamic scheduler("name", PSI_NOT_INSTRUMENTED); |
| 70 | Result_collector<unsigned int> result_set; |
| 71 | |
| 72 | scheduler.launch(); |
| 73 | for (unsigned int idx = 0; idx < TASK_COUNT; ++idx) |
| 74 | scheduler.post(result_set.new_task(idx)); |
| 75 | result_set.wait(TASK_COUNT); |
| 76 | scheduler.stop(); |
| 77 | ASSERT_EQ(TASK_COUNT, result_set.m_result.size()); |
| 78 | |
| 79 | std::sort(result_set.m_result.begin(), result_set.m_result.end()); |
| 80 | for (unsigned int idx = 0; idx < result_set.m_result.size(); ++idx) |
| 81 | ASSERT_EQ(idx, result_set.m_result[idx]); |
| 82 | } |
| 83 | |
| 84 | } // namespace test |
| 85 | |
| 86 | } // namespace xpl |