Skip to content

Commit

Permalink
opt(flow):1.7.16, 优化Action::onStop()函数,去除不必要的逻辑;优化CompositeAction,解决父…
Browse files Browse the repository at this point in the history
…动作提前结束,没有停子动作问题
  • Loading branch information
hevake committed Mar 7, 2024
1 parent b40e73f commit ea9d632
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 52 deletions.
58 changes: 28 additions & 30 deletions modules/flow/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,49 @@ bool Action::stop() {
state_ != State::kPause)
return true;

auto last_state = state_;

LogDbg("stop action %d:%s[%s]", id_, type_.c_str(), label_.c_str());

state_ = State::kStoped;

if (timer_ev_ != nullptr)
timer_ev_->disable();

is_base_func_invoked_ = false;

onStop();

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

if (last_state == state_) {
onFinal();

return true;
}

bool Action::finish(bool is_succ) {
if (state_ != State::kFinished && state_ != State::kStoped) {
LogDbg("action %d:%s[%s] finished, is_succ: %s", id_, type_.c_str(), label_.c_str(),
(is_succ? "succ" : "fail"));

state_ = State::kFinished;

if (timer_ev_ != nullptr)
timer_ev_->disable();

state_ = State::kStoped;
is_base_func_invoked_ = false;

onFinished(is_succ);

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

onFinal();
return true;

} else {
LogWarn("not allow %d:%s[%s]", id_, type_.c_str(), label_.c_str());
return false;
}
return true;
}

void Action::reset() {
Expand Down Expand Up @@ -235,31 +258,6 @@ void Action::resetTimeout() {
}
}

bool Action::finish(bool is_succ) {
if (state_ != State::kFinished && state_ != State::kStoped) {
LogDbg("action %d:%s[%s] finished, is_succ: %s", id_, type_.c_str(), label_.c_str(),
(is_succ? "succ" : "fail"));
state_ = State::kFinished;

if (timer_ev_ != nullptr)
timer_ev_->disable();

is_base_func_invoked_ = false;

onFinished(is_succ);

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

onFinal();
return true;

} else {
LogWarn("not allow %d:%s[%s]", id_, type_.c_str(), label_.c_str());
return false;
}
}

void Action::onStart() { is_base_func_invoked_ = true; }

void Action::onPause() { is_base_func_invoked_ = true; }
Expand Down
8 changes: 8 additions & 0 deletions modules/flow/actions/composite_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ void CompositeAction::onReset() {
AssembleAction::onReset();
}

void CompositeAction::onFinished(bool is_succ) {
//! 有可能不是child_自然结束产生的finish
TBOX_ASSERT(child_ != nullptr);
child_->stop();

AssembleAction::onFinished(is_succ);
}

void CompositeAction::onChildFinished(bool is_succ) {
if (state() == State::kRunning)
finish(is_succ);
Expand Down
1 change: 1 addition & 0 deletions modules/flow/actions/composite_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CompositeAction : public AssembleAction {
virtual void onPause() override;
virtual void onResume() override;
virtual void onReset() override;
virtual void onFinished(bool is_succ) override;

void onChildFinished(bool is_succ);

Expand Down
67 changes: 46 additions & 21 deletions modules/flow/actions/composite_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,33 @@
namespace tbox {
namespace flow {

class TimeCountAction : public CompositeAction {
public:
TimeCountAction(event::Loop &loop) : CompositeAction(loop, "TimeCount") {
auto loop_action = new LoopAction(loop);
auto seq_action = new SequenceAction(loop);
seq_action->addChild(new SleepAction(loop, std::chrono::milliseconds(100)));
seq_action->addChild(new FunctionAction(loop, [this] { ++count_; return true; }));
loop_action->setChild(seq_action);
setChild(loop_action);
}

virtual void onReset() {
count_ = 0;
CompositeAction::onReset();
}
TEST(CompositeAction, Basic) {
class TimeCountTestAction : public CompositeAction {
public:
TimeCountTestAction(event::Loop &loop) : CompositeAction(loop, "TimeCount") {
auto loop_action = new LoopAction(loop);
auto seq_action = new SequenceAction(loop);
seq_action->addChild(new SleepAction(loop, std::chrono::milliseconds(100)));
seq_action->addChild(new FunctionAction(loop, [this] { ++count_; return true; }));
loop_action->setChild(seq_action);
setChild(loop_action);
}

int count() const { return count_; }
virtual void onReset() {
count_ = 0;
CompositeAction::onReset();
}

private:
int count_ = 0;
};
int count() const { return count_; }

private:
int count_ = 0;
};

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

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

Expand All @@ -70,6 +69,32 @@ TEST(CompositeAction, Basic) {
EXPECT_GE(action.count(), 10);
}

//! 测试父动作提前结束动作的情况,观察有没有stop子动作
TEST(CompositeAction, ParentFinishBeforeChild) {

class TimeoutTestAction : public CompositeAction {
public:
TimeoutTestAction(event::Loop &loop, Action *child)
: CompositeAction(loop, "Timeout") {
setChild(child);
setTimeout(std::chrono::milliseconds(10));
}
};

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

auto sleep_action = new SleepAction(*loop, std::chrono::seconds(1));
TimeoutTestAction action(*loop, sleep_action);
action.start();

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

EXPECT_EQ(action.state(), Action::State::kFinished);
EXPECT_EQ(sleep_action->state(), Action::State::kStoped);
}

TEST(CompositeAction, NotSetChild) {
auto loop = event::Loop::New();
SetScopeExitAction([loop] { delete loop; });
Expand Down
2 changes: 1 addition & 1 deletion version.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TBOX版本号
TBOX_VERSION_MAJOR := 1
TBOX_VERSION_MINOR := 7
TBOX_VERSION_REVISION := 15
TBOX_VERSION_REVISION := 16

0 comments on commit ea9d632

Please sign in to comment.