|
| 1 | +/* |
| 2 | + * .============. |
| 3 | + * // M A K E / \ |
| 4 | + * // C++ DEV / \ |
| 5 | + * // E A S Y / \/ \ |
| 6 | + * ++ ----------. \/\ . |
| 7 | + * \\ \ \ /\ / |
| 8 | + * \\ \ \ / |
| 9 | + * \\ \ \ / |
| 10 | + * -============' |
| 11 | + * |
| 12 | + * Copyright (c) 2025 Hevake and contributors, all rights reserved. |
| 13 | + * |
| 14 | + * This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox) |
| 15 | + * Use of this source code is governed by MIT license that can be found |
| 16 | + * in the LICENSE file in the root of the source tree. All contributing |
| 17 | + * project authors may be found in the CONTRIBUTORS.md file in the root |
| 18 | + * of the source tree. |
| 19 | + */ |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <tbox/event/loop.h> |
| 22 | +#include <tbox/base/scope_exit.hpp> |
| 23 | +#include <tbox/eventx/work_thread.h> |
| 24 | + |
| 25 | +#include "execute_in_thread_action.h" |
| 26 | + |
| 27 | +namespace tbox { |
| 28 | +namespace flow { |
| 29 | + |
| 30 | +TEST(ExecuteInThreadAction, IsReady) { |
| 31 | + auto loop = event::Loop::New(); |
| 32 | + SetScopeExitAction([loop] { delete loop; }); |
| 33 | + eventx::WorkThread worker(loop); |
| 34 | + |
| 35 | + ExecuteInThreadAction action(*loop, worker); |
| 36 | + EXPECT_FALSE(action.isReady()); |
| 37 | + |
| 38 | + action.setFunc([] (Action::Reason &) { return true; }); |
| 39 | + EXPECT_TRUE(action.isReady()); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(ExecuteInThreadAction, Succ) { |
| 43 | + auto loop = event::Loop::New(); |
| 44 | + SetScopeExitAction([loop] { delete loop; }); |
| 45 | + eventx::WorkThread worker(loop); |
| 46 | + |
| 47 | + ExecuteInThreadAction action(*loop, worker); |
| 48 | + action.setFunc( |
| 49 | + [] (Action::Reason &) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + bool is_callback = false; |
| 55 | + action.setFinishCallback( |
| 56 | + [&] (bool is_succ, const Action::Reason &r, const Action::Trace &) { |
| 57 | + EXPECT_TRUE(is_succ); |
| 58 | + EXPECT_EQ(r.code, 0); |
| 59 | + |
| 60 | + is_callback = true; |
| 61 | + loop->exitLoop(); |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + EXPECT_TRUE(action.isReady()); |
| 66 | + action.start(); |
| 67 | + |
| 68 | + loop->runLoop(); |
| 69 | + EXPECT_TRUE(is_callback); |
| 70 | +} |
| 71 | + |
| 72 | +TEST(ExecuteInThreadAction, Fail) { |
| 73 | + auto loop = event::Loop::New(); |
| 74 | + SetScopeExitAction([loop] { delete loop; }); |
| 75 | + eventx::WorkThread worker(loop); |
| 76 | + |
| 77 | + ExecuteInThreadAction action(*loop, worker); |
| 78 | + action.setFunc( |
| 79 | + [] (Action::Reason &r) { |
| 80 | + r.code = 101; |
| 81 | + return false; |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + bool is_callback = false; |
| 86 | + action.setFinishCallback( |
| 87 | + [&] (bool is_succ, const Action::Reason &r, const Action::Trace &) { |
| 88 | + EXPECT_FALSE(is_succ); |
| 89 | + EXPECT_EQ(r.code, 101); |
| 90 | + |
| 91 | + is_callback = true; |
| 92 | + loop->exitLoop(); |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + EXPECT_TRUE(action.isReady()); |
| 97 | + action.start(); |
| 98 | + |
| 99 | + loop->runLoop(); |
| 100 | + EXPECT_TRUE(is_callback); |
| 101 | +} |
| 102 | + |
| 103 | +} |
| 104 | +} |
0 commit comments