|
| 1 | +/* |
| 2 | + * .============. |
| 3 | + * // M A K E / \ |
| 4 | + * // C++ DEV / \ |
| 5 | + * // E A S Y / \/ \ |
| 6 | + * ++ ----------. \/\ . |
| 7 | + * \\ \ \ /\ / |
| 8 | + * \\ \ \ / |
| 9 | + * \\ \ \ / |
| 10 | + * -============' |
| 11 | + * |
| 12 | + * Copyright (c) 2018 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 | + |
| 24 | +#include "random_select_action.h" |
| 25 | +#include "function_action.h" |
| 26 | +#include "succ_fail_action.h" |
| 27 | +#include "repeat_action.h" |
| 28 | + |
| 29 | +namespace tbox { |
| 30 | +namespace flow { |
| 31 | + |
| 32 | +TEST(RandomSelectAction, IsReady) { |
| 33 | + auto loop = event::Loop::New(); |
| 34 | + SetScopeExitAction([loop] { delete loop; }); |
| 35 | + |
| 36 | + RandomSelectAction random_select_action(*loop); |
| 37 | + EXPECT_FALSE(random_select_action.isReady()); |
| 38 | + |
| 39 | + random_select_action.addChild(new SuccAction(*loop)); |
| 40 | + EXPECT_TRUE(random_select_action.isReady()); |
| 41 | + |
| 42 | + random_select_action.addChild(new FailAction(*loop)); |
| 43 | + EXPECT_TRUE(random_select_action.isReady()); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(RandomSelectAction, OneChild) { |
| 47 | + auto loop = event::Loop::New(); |
| 48 | + SetScopeExitAction([loop] { delete loop; }); |
| 49 | + |
| 50 | + RandomSelectAction random_select_action(*loop); |
| 51 | + random_select_action.addChild(new SuccAction(*loop)); |
| 52 | + |
| 53 | + loop->exitLoop(std::chrono::milliseconds(10)); |
| 54 | + |
| 55 | + bool is_finished = false; |
| 56 | + random_select_action.setFinishCallback( |
| 57 | + [&] (bool is_succ, const Action::Reason &, const Action::Trace &) { |
| 58 | + is_finished = true; |
| 59 | + EXPECT_TRUE(is_succ); |
| 60 | + } |
| 61 | + ); |
| 62 | + random_select_action.start(); |
| 63 | + |
| 64 | + loop->runLoop(); |
| 65 | + EXPECT_TRUE(is_finished); |
| 66 | +} |
| 67 | + |
| 68 | +//! 在 RandomSelectAction 下添加 10 个 FunctionAction。 |
| 69 | +//! 用 RepeatAction 反复执行 100 次,观察是否每个 FunctionAction 都被执行过 |
| 70 | +TEST(RandomSelectAction, TenChildren) { |
| 71 | + auto loop = event::Loop::New(); |
| 72 | + SetScopeExitAction([loop] { delete loop; }); |
| 73 | + |
| 74 | + std::set<int> run_mark; |
| 75 | + RepeatAction repeat_action(*loop, 100); |
| 76 | + |
| 77 | + auto random_select_action = new RandomSelectAction(*loop); |
| 78 | + repeat_action.setChild(random_select_action); |
| 79 | + |
| 80 | + for (int i = 0; i < 10; ++i) { |
| 81 | + random_select_action->addChild( |
| 82 | + new FunctionAction(*loop, [&, i] { run_mark.insert(i); return true; }) |
| 83 | + ); |
| 84 | + } |
| 85 | + repeat_action.start(); |
| 86 | + |
| 87 | + loop->exitLoop(std::chrono::milliseconds(10)); |
| 88 | + loop->runLoop(); |
| 89 | + |
| 90 | + //! 检查是否每个FunctionAction都被执行到 |
| 91 | + for (int i = 0; i < 10; ++i) { |
| 92 | + EXPECT_EQ(run_mark.count(i), 1); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +} |
| 97 | +} |
0 commit comments