Skip to content

Commit

Permalink
opt(flow):1.8.0, 修改Action的setFinishCallback()接口,添加Reason,Trace
Browse files Browse the repository at this point in the history
  • Loading branch information
hevake committed Mar 17, 2024
1 parent 675d055 commit 03cfafb
Show file tree
Hide file tree
Showing 25 changed files with 289 additions and 92 deletions.
1 change: 1 addition & 0 deletions modules/flow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(TBOX_LIBRARY_NAME tbox_flow)
set(TBOX_FLOW_HEADERS
state_machine.h
action.h
action_reason.h
event.h
action_executor.h
event_subscriber.h
Expand Down
1 change: 1 addition & 0 deletions modules/flow/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LIB_VERSION_Z = 3
HEAD_FILES = \
state_machine.h \
action.h \
action_reason.h \
event.h \
action_executor.h \
event_subscriber.h \
Expand Down
4 changes: 4 additions & 0 deletions modules/flow/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ void Action::onFinished(bool is_succ, const Reason &why, const Trace &trace) {
is_base_func_invoked_ = true;
}

void Action::onTimeout() {
finish(false, Reason(ACTION_REASON_ACTION_TIMEOUT, "ActionTimeout"));
}

void Action::cancelDispatchedCallback() {
if (finish_cb_run_id_ != 0) {
loop_.cancel(finish_cb_run_id_);
Expand Down
4 changes: 3 additions & 1 deletion modules/flow/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <tbox/base/json_fwd.h>
#include <tbox/event/loop.h>

#include "action_reason.h"

namespace tbox {
namespace flow {

Expand Down Expand Up @@ -141,7 +143,7 @@ class Action {
virtual void onReset();
virtual void onFinished(bool is_succ, const Reason &why, const Trace &trace);
virtual void onFinal() { }
virtual void onTimeout() { finish(false); }
virtual void onTimeout();

void cancelDispatchedCallback();

Expand Down
Empty file removed modules/flow/action_reason.cpp
Empty file.
41 changes: 31 additions & 10 deletions modules/flow/action_reason.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#ifndef TBOX_FLOW_ACTION_REASON_H_20240316
#define TBOX_FLOW_ACTION_REASON_H_20240316
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2024 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/

namespace tbox {
namespace flow {
#ifndef TBOX_FLOW_ACTION_REASON_H_20240317
#define TBOX_FLOW_ACTION_REASON_H_20240317

enum ActionReason {
kSuccess,
};
#define ACTION_REASON_NONE 0
#define ACTION_REASON_ACTION_TIMEOUT 1 //!< "ActionTimeout"
#define ACTION_REASON_FUNCTION_ACTION 2 //!< "FunctionAction"
#define ACTION_REASON_SLEEP_ACTION 3 //!< "SleepAction"
#define ACTION_REASON_SUCC_ACTION 4 //!< "SuccAction"
#define ACTION_REASON_FAIL_ACTION 5 //!< "FailAction"
#define ACTION_REASON_START_CHILD_FAIL 6 //!< "StartChildFail"
#define ACTION_REASON_REPEAT_NO_TIMES 7 //!< "RepeatNoTimes"

}
}
//! 保存 1000 以内供 Action 自用,使用者自定义的 Reason 需 >= 1000

#endif //TBOX_FLOW_ACTION_REASON_H_20240316
#endif //TBOX_FLOW_ACTION_REASON_H_20240317
30 changes: 25 additions & 5 deletions modules/flow/action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ TEST(Action, StartFinish) {
public:
explicit TestAction(event::Loop &loop) : Action(loop, "Test") { }
virtual bool isReady() const override { return true; }
virtual void onStart() override { finish(true); }
virtual void onStart() override { finish(true, 100); }
};

TestAction action(*loop);

bool is_callback = false;
action.setFinishCallback(
[&, loop] (bool is_succ, const Action::Reason &, const Action::Trace &) {
[&, loop] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_TRUE(is_succ);

EXPECT_EQ(r.code, 100);
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].type, "Test");
is_callback = true;
}
);
Expand All @@ -61,13 +65,24 @@ TEST(Action, StartBlock) {
public:
explicit TestAction(event::Loop &loop) : Action(loop, "Test") { }
virtual bool isReady() const override { return true; }
virtual void onStart() override { Action::onStart(); block(0); }
virtual void onStart() override {
Action::onStart();
block(Action::Reason(100, "block_on_start"));
}
};

bool is_block = false;

TestAction action(*loop);
action.setBlockCallback([&](const Action::Reason &, const Action::Trace &) { is_block = true; });
action.setBlockCallback(
[&](const Action::Reason &r, const Action::Trace &t) {
EXPECT_EQ(r.code, 100);
EXPECT_EQ(r.message, "block_on_start");
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].type, "Test");
is_block = true;
}
);
action.start();

loop->exitLoop(std::chrono::milliseconds(10));
Expand Down Expand Up @@ -98,8 +113,13 @@ TEST(Action, Timeout) {
std::chrono::steady_clock::time_point ts_timeout;

action.setFinishCallback(
[&, loop] (bool is_succ, const Action::Reason &, const Action::Trace &) {
[&, loop] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_FALSE(is_succ);
EXPECT_EQ(r.code, ACTION_REASON_ACTION_TIMEOUT);
EXPECT_EQ(r.message, "ActionTimeout");
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].type, "Test");

is_callback = true;
ts_timeout = std::chrono::steady_clock::now();
loop->exitLoop();
Expand Down
69 changes: 56 additions & 13 deletions modules/flow/actions/composite_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "loop_action.h"
#include "function_action.h"
#include "sequence_action.h"
#include "succ_fail_action.h"

namespace tbox {
namespace flow {
Expand Down Expand Up @@ -60,6 +61,7 @@ TEST(CompositeAction, Basic) {

TimeCountTestAction action(*loop);
EXPECT_TRUE(action.isReady());

action.start();

loop->exitLoop(std::chrono::milliseconds(1010));
Expand Down Expand Up @@ -122,19 +124,23 @@ TEST(CompositeAction, ChildBlock) {

ParentAction action(*loop);

action.setBlockCallback([&] (const Action::Reason &why) {
is_blocked = true;
EXPECT_EQ(why.code, 1);
EXPECT_FALSE(is_finished);
EXPECT_EQ(action.state(), Action::State::kPause);
action.resume();
});
action.setFinishCallback([&] (bool succ) {
is_finished = true;
EXPECT_TRUE(succ);
EXPECT_TRUE(is_blocked);
EXPECT_EQ(action.state(), Action::State::kFinished);
});
action.setBlockCallback(
[&] (const Action::Reason &why, const Action::Trace &) {
is_blocked = true;
EXPECT_EQ(why.code, 1);
EXPECT_FALSE(is_finished);
EXPECT_EQ(action.state(), Action::State::kPause);
action.resume();
}
);
action.setFinishCallback(
[&] (bool succ, const Action::Reason &, const Action::Trace &) {
is_finished = true;
EXPECT_TRUE(succ);
EXPECT_TRUE(is_blocked);
EXPECT_EQ(action.state(), Action::State::kFinished);
}
);

action.start();

Expand All @@ -154,5 +160,42 @@ TEST(CompositeAction, NotSetChild) {
EXPECT_FALSE(action.isReady());
}

TEST(CompositeAction, ReasonAndTrace) {
class TestAction : public CompositeAction {
public:
explicit TestAction(event::Loop &loop)
: CompositeAction(loop, "Test") {
setChild(new FailAction(loop));
}
};

auto loop = event::Loop::New();
SetScopeExitAction([loop] { delete loop; });

TestAction action(*loop);

bool is_finished = false;
action.setFinishCallback(
[&](bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_FALSE(is_succ);
EXPECT_EQ(r.code, ACTION_REASON_FAIL_ACTION);
EXPECT_EQ(r.message, "FailAction");

ASSERT_EQ(t.size(), 2);
EXPECT_EQ(t[0].type, "Fail");
EXPECT_EQ(t[1].type, "Test");

is_finished = true;
}
);

action.start();

loop->exitLoop(std::chrono::milliseconds(10));
loop->runLoop();

EXPECT_TRUE(is_finished);
}

}
}
2 changes: 1 addition & 1 deletion modules/flow/actions/function_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FunctionAction::FunctionAction(event::Loop &loop, Func &&func)

void FunctionAction::onStart() {
Action::onStart();
finish(func_());
finish(func_(), Reason(ACTION_REASON_FUNCTION_ACTION, "FunctionAction"));
}

}
Expand Down
14 changes: 12 additions & 2 deletions modules/flow/actions/function_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ TEST(NonDelayAction, True) {
FunctionAction action(*loop, [] { return true; });
bool is_callback = false;
action.setFinishCallback(
[&is_callback, loop] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_TRUE(is_succ);
EXPECT_EQ(r.code, ACTION_REASON_FUNCTION_ACTION);
EXPECT_EQ(r.message, "FunctionAction");
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].id, action.id());

is_callback = true;
loop->exitLoop();
}
Expand All @@ -53,8 +58,13 @@ TEST(NonDelayAction, False) {
FunctionAction action(*loop, [] { return false; });
bool is_callback = false;
action.setFinishCallback(
[&is_callback, loop] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
EXPECT_FALSE(is_succ);
EXPECT_EQ(r.code, ACTION_REASON_FUNCTION_ACTION);
EXPECT_EQ(r.message, "FunctionAction");
ASSERT_EQ(t.size(), 1);
EXPECT_EQ(t[0].id, action.id());

is_callback = true;
loop->exitLoop();
}
Expand Down
12 changes: 6 additions & 6 deletions modules/flow/actions/if_else_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST(IfElseAction, CondSucc) {
EXPECT_TRUE(if_else_action.isReady());

if_else_action.setFinishCallback(
[&] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &, const Action::Trace &) {
EXPECT_TRUE(is_succ);
if_else_action_run = true;
loop->exitLoop();
Expand Down Expand Up @@ -87,7 +87,7 @@ TEST(IfElseAction, CondFail) {
EXPECT_TRUE(if_else_action.isReady());

if_else_action.setFinishCallback(
[&] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &, const Action::Trace &) {
EXPECT_TRUE(is_succ);
if_else_action_run = true;
loop->exitLoop();
Expand Down Expand Up @@ -120,7 +120,7 @@ TEST(IfElseAction, CondSuccNoIfAction) {
EXPECT_TRUE(if_else_action.isReady());

if_else_action.setFinishCallback(
[&] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &, const Action::Trace &) {
EXPECT_TRUE(is_succ);
if_else_action_run = true;
loop->exitLoop();
Expand Down Expand Up @@ -153,7 +153,7 @@ TEST(IfElseAction, CondFailNoElseAction) {
EXPECT_TRUE(if_else_action.isReady());

if_else_action.setFinishCallback(
[&] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &, const Action::Trace &) {
EXPECT_TRUE(is_succ);
if_else_action_run = true;
loop->exitLoop();
Expand Down Expand Up @@ -198,15 +198,15 @@ TEST(IfElseAction, BlockOnIf) {
EXPECT_TRUE(if_else_action.setChildAs(succ_action, "succ"));
EXPECT_TRUE(if_else_action.isReady());

if_else_action.setBlockCallback([&] (const Action::Reason &why) {
if_else_action.setBlockCallback([&] (const Action::Reason &why, const Action::Trace &) {
is_blocked = true;
EXPECT_EQ(why.code, 1);
EXPECT_EQ(if_else_action.state(), Action::State::kPause);
if_else_action.resume();
});

if_else_action.setFinishCallback(
[&] (bool is_succ) {
[&] (bool is_succ, const Action::Reason &, const Action::Trace &) {
EXPECT_TRUE(is_succ);
if_else_action_run = true;
loop->exitLoop();
Expand Down
4 changes: 2 additions & 2 deletions modules/flow/actions/loop_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST(LoopAction, FunctionActionForever) {

EXPECT_TRUE(loop_action.setChild(function_action));
EXPECT_TRUE(loop_action.isReady());
loop_action.setFinishCallback([&] (bool) { is_finished = true; });
loop_action.setFinishCallback([&] (bool, const Action::Reason&, const Action::Trace&) { is_finished = true; });

loop_action.start();
loop->exitLoop(std::chrono::milliseconds(1000));
Expand Down Expand Up @@ -94,7 +94,7 @@ TEST(LoopAction, SleepActionForever) {

EXPECT_TRUE(loop_action.setChild(seq_action));
EXPECT_TRUE(loop_action.isReady());
loop_action.setFinishCallback([&] (bool) { is_finished = true; });
loop_action.setFinishCallback([&] (bool, const Action::Reason&, const Action::Trace&) { is_finished = true; });

loop_action.start();
loop->exitLoop(std::chrono::milliseconds(1010));
Expand Down
4 changes: 2 additions & 2 deletions modules/flow/actions/loop_if_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST(LoopIfAction, LoopRemainTimes) {
EXPECT_TRUE(loop_if_action.setChildAs(cond_action, "if"));
EXPECT_TRUE(loop_if_action.setChildAs(exec_action, "exec"));
EXPECT_TRUE(loop_if_action.isReady());
loop_if_action.setFinishCallback([&] (bool) { is_finished = true; });
loop_if_action.setFinishCallback([&] (bool, const Action::Reason&, const Action::Trace&) { is_finished = true; });
loop_if_action.start();

loop->exitLoop(std::chrono::milliseconds(10));
Expand Down Expand Up @@ -84,7 +84,7 @@ TEST(LoopIfAction, MultiAction) {
EXPECT_TRUE(loop_if_action.setChildAs(cond_action, "if"));
EXPECT_TRUE(loop_if_action.setChildAs(exec_action, "exec"));
EXPECT_TRUE(loop_if_action.isReady());
loop_if_action.setFinishCallback([&] (bool) { is_finished = true; });
loop_if_action.setFinishCallback([&] (bool, const Action::Reason&, const Action::Trace&) { is_finished = true; });
loop_if_action.start();

loop->exitLoop(std::chrono::milliseconds(200));
Expand Down
4 changes: 2 additions & 2 deletions modules/flow/actions/parallel_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST(ParallelAction, TwoSleepAction) {
EXPECT_TRUE(para_action->isReady());

para_action->setFinishCallback(
[loop](bool is_succ) {
[loop](bool is_succ, const Action::Reason&, const Action::Trace&) {
EXPECT_TRUE(is_succ);
loop->exitLoop();
}
Expand Down Expand Up @@ -79,7 +79,7 @@ TEST(ParallelAction, SleepFunctionAction) {
EXPECT_TRUE(para_action->isReady());

para_action->setFinishCallback(
[loop](bool is_succ) {
[loop](bool is_succ, const Action::Reason&, const Action::Trace&) {
EXPECT_TRUE(is_succ);
loop->exitLoop();
}
Expand Down
Loading

0 comments on commit 03cfafb

Please sign in to comment.