| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | #include "Common/WorkQueueThread.h" |
| 7 | |
| 8 | TEST(WorkQueueThread, Simple) |
| 9 | { |
| 10 | Common::WorkQueueThreadSP<int> worker; |
| 11 | |
| 12 | constexpr int BIG_VAL = 1000; |
| 13 | |
| 14 | int x = 0; |
| 15 | const auto func = [&](int value) { x = value; }; |
| 16 | |
| 17 | worker.Push(1); |
| 18 | worker.WaitForCompletion(); |
| 19 | // Still zero because it's not running. |
| 20 | EXPECT_EQ(x, 0); |
| 21 | |
| 22 | // Does nothing if not running. |
| 23 | worker.Shutdown(); |
| 24 | |
| 25 | worker.Reset("test worker", func); |
| 26 | worker.WaitForCompletion(); |
| 27 | // Items pushed before Reset are processed. |
| 28 | EXPECT_EQ(x, 1); |
| 29 | |
| 30 | worker.Shutdown(); |
| 31 | worker.Push(0); |
| 32 | worker.WaitForCompletion(); |
| 33 | // Still 1 because it's no longer running. |
| 34 | EXPECT_EQ(x, 1); |
| 35 | |
| 36 | worker.Cancel(); |
| 37 | worker.Reset("test worker", func); |
| 38 | worker.WaitForCompletion(); |
| 39 | // Still 1 because the work was canceled. |
| 40 | EXPECT_EQ(x, 1); |
| 41 | |
| 42 | for (int i = 0; i != BIG_VAL; ++i) |
| 43 | worker.Push(i); |
| 44 | worker.Cancel(); |
| 45 | // Could be any one of the pushed values. |
| 46 | EXPECT_LT(x, BIG_VAL); |
| 47 | GTEST_LOG_(INFO) << "Canceled work after item " << x; |
| 48 | |
| 49 | worker.Push(2); |
| 50 | worker.WaitForCompletion(); |
| 51 | // Still running after cancellation. |
| 52 | EXPECT_EQ(x, 2); |
| 53 | } |