Skip to content

Commit

Permalink
MsgSelf: copilot compare
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Jul 13, 2023
1 parent 0ab980c commit 9e2c84f
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 83 deletions.
21 changes: 10 additions & 11 deletions src/msg_self/MsgSelf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace RLib
{
// ***********************************************************************************************
MsgSelf::MsgSelf(const ToMainFN& aToMainFN, const UniLogName& aUniLogName)
MsgSelf::MsgSelf(const PingMainFN& aPingMainFN, const UniLogName& aUniLogName)
: UniLog(aUniLogName)
, toMainFN_(aToMainFN)
, pingMainFN_(aPingMainFN)
{}

// ***********************************************************************************************
Expand Down Expand Up @@ -39,40 +39,39 @@ void MsgSelf::handleAllMsg(const shared_ptr<bool> aValidMsgSelf)
// ***********************************************************************************************
bool MsgSelf::handleOneMsg()
{
for (auto&& priority = EMsgPri_MAX-1; priority >=EMsgPri_MIN ; priority--)
for (auto msgPri = EMsgPri_MAX-1; msgPri >=EMsgPri_MIN ; msgPri--)
{
auto&& oneQueue = msgQueues_[priority];
auto&& oneQueue = msgQueues_[msgPri];
if (oneQueue.empty())
continue;

auto&& cb = oneQueue.front();
cb(); // not support cb=nullptr
oneQueue.front()(); // run 1st MsgCB; newMsg() prevent nullptr in msgQueues_
oneQueue.pop();
--nMsg_;

if (not isLowPri(EMsgPriority(priority)))
if (not isLowPri(EMsgPriority(msgPri)))
return true;
if (hasMsg())
toMainFN_([this, isValid = isValid_]() mutable { handleAllMsg(isValid); });
pingMainFN_([this, isValid = isValid_]() mutable { handleAllMsg(isValid); });
return false; // 1 low msg per loopReq so queue with eg IM CB, syscom, etc. (lower)
}
return false;
}

// ***********************************************************************************************
void MsgSelf::newMsg(const MsgCB& aMsgCB, const EMsgPriority aPriority)
void MsgSelf::newMsg(const MsgCB& aMsgCB, const EMsgPriority aMsgPri)
{
if (aMsgCB == nullptr)
{
WRN("(MsgSelf) failed!!! aMsgCB=nullptr doesn't make sense.");
return;
}

msgQueues_[aPriority].push(aMsgCB);
msgQueues_[aMsgPri].push(aMsgCB);
++nMsg_;
if (nMsg_ > 1)
return;

toMainFN_([this, isValid = isValid_]() mutable { handleAllMsg(isValid); });
pingMainFN_([this, isValid = isValid_]() mutable { handleAllMsg(isValid); });
}
} // namespace
16 changes: 9 additions & 7 deletions src/msg_self/MsgSelf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
*/
// ***********************************************************************************************
// - Issue/why: not callback directly but wait current function return to main() then callback
// . avoid loop callbacks, but exe after all executing/in-stack func returned
// * avoid logic surprise when immediate callback [MUST-HAVE!]
// . avoid deep/loop callbacks, but exe after all executing/in-stack func returned
// * priority FIFO msg (eg "abort" is higher priority event)
// * priority FIFO msg (eg "abort" is higher priority)
// . single thread (in main thread)
// * support diff cb mechanism (async, IM, syscom, etc)
// . can withdraw on-road MsgCB (eg HdlrDomino.rmHdlr())
// . destruct MsgSelf shall not call MsgCB in msgQueues_, & no mem-leak
// - how:
// . newMsg(): send msgHdlr into msgQueues_ (all info are in msgHdlr so func<void()> is enough)
// . handleAllMsg(): call all msgHdlr in msgQueues_, priority then FIFO
// - core: msgQueues_
// - core: msgQueues_[priority][FIFO]
// . msgQueues_ is a 2D array, 1st dim is priority, 2nd dim is FIFO
// - which way? speed UT code
// . async task may slow if async busy no but direct-CB instead simple
// . IM may ensure speed no but direct-CB instead Im, ActiveBtsL, StatusMo
Expand Down Expand Up @@ -53,14 +54,14 @@ using MsgCB = function<void()>;
using WeakMsgCB = weak_ptr<MsgCB>;
using SharedMsgCB = shared_ptr<MsgCB>;

using FromMainFN = function<void()>;
using ToMainFN = function<void(const FromMainFN&)>;
using PongMainFN = function<void()>; // tap to main()
using PingMainFN = function<void(const PongMainFN&)>; // tap from main()

// ***********************************************************************************************
class MsgSelf : public UniLog
{
public:
MsgSelf(const ToMainFN&, const UniLogName& = ULN_DEFAULT);
MsgSelf(const PingMainFN&, const UniLogName& = ULN_DEFAULT);
~MsgSelf();
const shared_ptr<bool> getValid() const { return isValid_; }

Expand All @@ -78,7 +79,7 @@ class MsgSelf : public UniLog
queue<MsgCB> msgQueues_[EMsgPri_MAX];

shared_ptr<bool> isValid_ = make_shared<bool>(true); // MsgSelf is still valid?
ToMainFN toMainFN_;
PingMainFN pingMainFN_;
size_t nMsg_ = 0;
};
} // namespace
Expand All @@ -101,4 +102,5 @@ class MsgSelf : public UniLog
// . support both MsgCB & WeakMsgCB(by lambda)
// 2022-12-02 CSZ - simple & natural
// 2022-12-31 CSZ - not support MsgCB=nullptr
// 2023-07-13 CSZ - copilot compare
// ***********************************************************************************************
34 changes: 17 additions & 17 deletions ut/domino/FreeHdlrDominoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct FreeHdlrDominoTest : public UtInitObjAnywhere

// -------------------------------------------------------------------------------------------
shared_ptr<MsgSelf> msgSelf_ = make_shared<MsgSelf>(
[this](const FromMainFN& aFromMainFN){ fromMainFN_ = aFromMainFN; }, uniLogName());
FromMainFN fromMainFN_;
[this](const PongMainFN& aPongMainFN){ pongMainFN_ = aPongMainFN; }, uniLogName());
PongMainFN pongMainFN_;

MsgCB h1_ = [this](){ hdlrIDs_.insert(1); };
MsgCB h2_ = [this](){ hdlrIDs_.insert(2); };
Expand Down Expand Up @@ -60,12 +60,12 @@ TYPED_TEST_P(FreeHdlrDominoTest, GOLD_afterCallback_autoRmHdlr)
EXPECT_FALSE(PARA_DOM->isRepeatHdlr(e1)); // req: default false (most hdlr used once)

PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({1}), this->hdlrIDs_); // cb

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_() ;
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1}), this->hdlrIDs_); // req: no more cb since auto-rm
}
TYPED_TEST_P(FreeHdlrDominoTest, afterCallback_autoRmHdlr_aliasMultiHdlr)
Expand All @@ -74,12 +74,12 @@ TYPED_TEST_P(FreeHdlrDominoTest, afterCallback_autoRmHdlr_aliasMultiHdlr)
EXPECT_FALSE(PARA_DOM->isRepeatHdlr(aliasE1));

PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({3}), this->hdlrIDs_); // cb

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_() ;
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({3}), this->hdlrIDs_); // req: no more cb since auto-rm
}
TYPED_TEST_P(FreeMultiHdlrDominoTest, afterCallback_autoRmHdlr_multiHdlr)
Expand All @@ -91,24 +91,24 @@ TYPED_TEST_P(FreeMultiHdlrDominoTest, afterCallback_autoRmHdlr_multiHdlr)
EXPECT_FALSE(PARA_DOM->isRepeatHdlr(aliasE1));

PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({1, 2, 3}), this->hdlrIDs_); // cb

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_() ;
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3}), this->hdlrIDs_); // req: no more cb since auto-rm

// re-add hdlr
PARA_DOM->multiHdlrByAliasEv("alias e1", this->h6_, "e1");
PARA_DOM->multiHdlrOnSameEv("e1", this->h5_, "h2_");
PARA_DOM->setHdlr("e1", this->h4_); // reverse order to inc coverage
if (this->msgSelf_->hasMsg()) this->fromMainFN_() ;
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3, 4, 5, 6}), this->hdlrIDs_); // req: re-add ok

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_() ;
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3, 4, 5, 6}), this->hdlrIDs_); // req: no more cb since auto-rm
}
TYPED_TEST_P(FreeMultiHdlrDominoTest, afterCallback_notRmHdlr)
Expand All @@ -122,12 +122,12 @@ TYPED_TEST_P(FreeMultiHdlrDominoTest, afterCallback_notRmHdlr)
EXPECT_TRUE(PARA_DOM->isRepeatHdlr(aliasE1));

PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_(); // 1st cb
if (this->msgSelf_->hasMsg()) this->pongMainFN_(); // 1st cb
EXPECT_EQ(multiset<int>({1, 2, 3}), this->hdlrIDs_); // req: not auto-rm

PARA_DOM->setState({{"e1", false}, {"alias e1", false}}); // SameEv simpler than AliasEv
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_(); // 2nd cb
if (this->msgSelf_->hasMsg()) this->pongMainFN_(); // 2nd cb
EXPECT_EQ(multiset<int>({1, 2, 3, 1, 2, 3}), this->hdlrIDs_); // req: not auto-rm

// re-add hdlr
Expand All @@ -137,7 +137,7 @@ TYPED_TEST_P(FreeMultiHdlrDominoTest, afterCallback_notRmHdlr)

PARA_DOM->setState({{"e1", false}, {"alias e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->fromMainFN_() ;
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3, 1, 2, 3, 1, 2, 3}), this->hdlrIDs_); // req: re-add nok
}
TYPED_TEST_P(FreeMultiHdlrDominoTest, BugFix_disorderAutoRm_ok)
Expand All @@ -148,7 +148,7 @@ TYPED_TEST_P(FreeMultiHdlrDominoTest, BugFix_disorderAutoRm_ok)
PARA_DOM->multiHdlrOnSameEv("e1", this->h2_, "h2_"); // h2_ on road
PARA_DOM->multiHdlrOnSameEv("e1", this->h4_, "h4_"); // h4_ on road

if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({1, 2, 3, 4}), this->hdlrIDs_);
}
TYPED_TEST_P(FreeHdlrDominoTest, multiCallbackOnRoad_noCrash_noMultiCall)
Expand All @@ -159,7 +159,7 @@ TYPED_TEST_P(FreeHdlrDominoTest, multiCallbackOnRoad_noCrash_noMultiCall)
PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}}); // 2nd on road

if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({1}), this->hdlrIDs_); // req: no more cb since auto-rm
}
TYPED_TEST_P(FreeMultiHdlrDominoTest, BugFix_multiCallbackOnRoad_noCrash_noMultiCall)
Expand All @@ -172,7 +172,7 @@ TYPED_TEST_P(FreeMultiHdlrDominoTest, BugFix_multiCallbackOnRoad_noCrash_noMulti

PARA_DOM->setHdlr("e1", this->h1_); // h1_ on road

if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({1, 2}), this->hdlrIDs_); // req: no more cb since auto-rm
}

Expand All @@ -193,7 +193,7 @@ TYPED_TEST_P(FreeHdlrDominoTest, BugFix_noCrash_whenRmDom)
PARA_DOM->setState({{"e1", true}});
ASSERT_TRUE(this->msgSelf_->hasMsg());
ObjAnywhere::set<TypeParam>(nullptr, *this); // req: no mem leak when rm MsgSelf with h7 in msg queue
this->fromMainFN_();
this->pongMainFN_();
}

#define ID_STATE
Expand Down
16 changes: 8 additions & 8 deletions ut/domino/HdlrDominoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct HdlrDominoTest : public UtInitObjAnywhere
MOCK_METHOD(void, hdlr2, ());

// -------------------------------------------------------------------------------------------
FromMainFN fromMainFN_;
PongMainFN pongMainFN_;
MsgCB hdlr0_ = [this](){ this->hdlr0(); };
MsgCB hdlr1_ = [this](){ this->hdlr1(); };
MsgCB hdlr2_ = [this](){ this->hdlr2(); };
Expand Down Expand Up @@ -205,7 +205,7 @@ TYPED_TEST_P(HdlrDominoTest, rmHdlrOnRoad_noCallback)
{
// not auto-cb but manually
auto msgSelf = make_shared<MsgSelf>(
[this](const FromMainFN& aFromMainFN){ this->fromMainFN_ = aFromMainFN; }, this->uniLogName());
[this](const PongMainFN& aPongMainFN){ this->pongMainFN_ = aPongMainFN; }, this->uniLogName());
PARA_DOM->setMsgSelf(msgSelf);

PARA_DOM->multiHdlrByAliasEv("e0", this->hdlr0_, "e");
Expand All @@ -216,13 +216,13 @@ TYPED_TEST_P(HdlrDominoTest, rmHdlrOnRoad_noCallback)

EXPECT_CALL(*this, hdlr0()).Times(0);
EXPECT_CALL(*this, hdlr1());
this->fromMainFN_(); // manual trigger on road cb
this->pongMainFN_(); // manual trigger on road cb
}
TYPED_TEST_P(NofreeHdlrDominoTest, rmHdlrOnRoad_thenReAdd_noCallbackUntilReTrigger)
{
// not auto-cb but manually
auto msgSelf = make_shared<MsgSelf>(
[this](const FromMainFN& aFromMainFN){ this->fromMainFN_ = aFromMainFN; }, this->uniLogName());
[this](const PongMainFN& aPongMainFN){ this->pongMainFN_ = aPongMainFN; }, this->uniLogName());
PARA_DOM->setMsgSelf(msgSelf);

PARA_DOM->setHdlr("event", this->hdlr0_);
Expand All @@ -239,18 +239,18 @@ TYPED_TEST_P(NofreeHdlrDominoTest, rmHdlrOnRoad_thenReAdd_noCallbackUntilReTrigg
PARA_DOM->setHdlr("event", this->hdlr0_); // re-add hdlr

EXPECT_CALL(*this, hdlr0()).Times(0); // req: no cb since rm-ed
this->fromMainFN_();
this->pongMainFN_();

PARA_DOM->setState({{"event", true}});
ASSERT_TRUE(msgSelf->hasMsg());
EXPECT_CALL(*this, hdlr0()); // req: new cb
this->fromMainFN_();
this->pongMainFN_();
}
TYPED_TEST_P(NofreeHdlrDominoTest, hdlrOnRoad_thenRmDom_noCrash_noLeak)
{
// not auto-cb but manually
auto msgSelf = make_shared<MsgSelf>(
[this](const FromMainFN& aFromMainFN){ this->fromMainFN_ = aFromMainFN; }, this->uniLogName());
[this](const PongMainFN& aPongMainFN){ this->pongMainFN_ = aPongMainFN; }, this->uniLogName());
PARA_DOM->setMsgSelf(msgSelf);

PARA_DOM->setHdlr("event", this->hdlr0_);
Expand All @@ -259,7 +259,7 @@ TYPED_TEST_P(NofreeHdlrDominoTest, hdlrOnRoad_thenRmDom_noCrash_noLeak)

ObjAnywhere::set<TypeParam>(nullptr, *this); // rm dom
EXPECT_CALL(*this, hdlr0()).Times(0); // req: no cb
this->fromMainFN_();
this->pongMainFN_();
}

#define FORCE_CALL
Expand Down
8 changes: 4 additions & 4 deletions ut/domino/MultiHdlrDominoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct MultiHdlrDominoTest : public UtInitObjAnywhere
MOCK_METHOD(void, hdlr2, ());

// -------------------------------------------------------------------------------------------
FromMainFN fromMainFN_;
PongMainFN pongMainFN_;
MsgCB hdlr0_ = [this](){ this->hdlr0(); };
MsgCB hdlr1_ = [this](){ this->hdlr1(); };
MsgCB hdlr2_ = [this](){ this->hdlr2(); };
Expand Down Expand Up @@ -246,7 +246,7 @@ TYPED_TEST_P(NofreeMultiHdlrDominoTest, rmHdlrOnRoad)
{
// not auto-cb but manually
auto msgSelf = make_shared<MsgSelf>(
[this](const FromMainFN& aFromMainFN){ this->fromMainFN_ = aFromMainFN; }, this->uniLogName());
[this](const PongMainFN& aPongMainFN){ this->pongMainFN_ = aPongMainFN; }, this->uniLogName());
PARA_DOM->setMsgSelf(msgSelf);

PARA_DOM->setHdlr("event", this->hdlr0_);
Expand All @@ -260,15 +260,15 @@ TYPED_TEST_P(NofreeMultiHdlrDominoTest, rmHdlrOnRoad)
EXPECT_CALL(*this, hdlr0()).Times(0);
EXPECT_CALL(*this, hdlr1());
EXPECT_CALL(*this, hdlr2()).Times(0);
if (msgSelf->hasMsg()) this->fromMainFN_(); // manual trigger on road cb
if (msgSelf->hasMsg()) this->pongMainFN_(); // manual trigger on road cb

PARA_DOM->setState({{"event", false}});
PARA_DOM->setState({{"event", true}}); // retrigger

EXPECT_CALL(*this, hdlr0()).Times(0);
EXPECT_CALL(*this, hdlr1());
EXPECT_CALL(*this, hdlr2()).Times(0);
if (msgSelf->hasMsg()) this->fromMainFN_(); // manual trigger on road cb
if (msgSelf->hasMsg()) this->pongMainFN_(); // manual trigger on road cb
}
// ***********************************************************************************************
// rm invalid
Expand Down
6 changes: 3 additions & 3 deletions ut/domino/PriDominoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct PriDominoTest : public UtInitObjAnywhere

// -------------------------------------------------------------------------------------------
shared_ptr<MsgSelf> msgSelf_ = make_shared<MsgSelf>(
[this](const FromMainFN& aFromMainFN){ fromMainFN_ = aFromMainFN; }, uniLogName());
FromMainFN fromMainFN_;
[this](const PongMainFN& aPongMainFN){ pongMainFN_ = aPongMainFN; }, uniLogName());
PongMainFN pongMainFN_;

MsgCB d1EventHdlr_ = [&](){ hdlrIDs_.push(1); };
MsgCB d2EventHdlr_ = [&](){ hdlrIDs_.push(2); };
Expand Down Expand Up @@ -85,7 +85,7 @@ TYPED_TEST_P(PriDominoTest, GOLD_setPriority_thenPriorityFifoCallback)
PARA_DOM->setState({{"e4", false}});
PARA_DOM->setState({{"e4", true}});

if (this->msgSelf_->hasMsg()) this->fromMainFN_();
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
if (this->hdlrIDs_.size() == 6) EXPECT_EQ(queue<int>({5, 4, 2, 1, 3, 4}), this->hdlrIDs_);
else EXPECT_EQ(queue<int>({5, 4, 2, 1, 3}), this->hdlrIDs_); // auto-rm-hdlr dom
}
Expand Down
Loading

0 comments on commit 9e2c84f

Please sign in to comment.