Skip to content

Commit

Permalink
MsgSelf: hasMsg->nMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Oct 26, 2023
1 parent 42c61e9 commit b351bb7
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/msg_self/MsgSelf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void MsgSelf::handleAllMsg(const shared_ptr<bool> aValidMsgSelf)

if (*aValidMsgSelf) // impossible aValidMsgSelf==nullptr since 022-Mar-11
{
// if (not hasMsg()) return; // rare nMsg_==0 when PongMainFN()
// if (not nMsg()) return; // rare nMsg_==0 when PongMainFN()
while (handleOneMsg()); // handleOneMsg() may create new high priority msg(s)
}
}
Expand All @@ -49,7 +49,7 @@ bool MsgSelf::handleOneMsg()
oneQueue.pop_front();
--nMsg_;

if (not hasMsg())
if (not nMsg())
return false; // no more to continue
if (isLowPri(EMsgPriority(msgPri)))
{
Expand Down
4 changes: 2 additions & 2 deletions src/msg_self/MsgSelf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MsgSelf : public UniLog
const shared_ptr<bool> getValid() const { return isValid_; }

void newMsg(const MsgCB&, const EMsgPriority = EMsgPri_NORM); // can't withdraw CB but easier usage
bool hasMsg() const { return nMsg_; }
size_t nMsg() const { return nMsg_; }
size_t nMsg(const EMsgPriority aPriority) const { return msgQueues_[aPriority].size(); }

static bool isLowPri(const EMsgPriority aPri) { return aPri < EMsgPri_NORM; }
Expand All @@ -87,7 +87,7 @@ class MsgSelf : public UniLog
deque<MsgCB> msgQueues_[EMsgPri_MAX];

shared_ptr<bool> isValid_ = make_shared<bool>(true); // MsgSelf is still valid?
PingMainFN pingMainFN_;
PingMainFN pingMainFN_ = nullptr;
size_t nMsg_ = 0;
};
} // namespace
Expand Down
32 changes: 16 additions & 16 deletions ut/domino/FreeHdlrDominoTest.cpp
Original file line number Diff line number Diff line change
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)" << endl;

PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->pongMainFN_();
if (this->msgSelf_->nMsg()) 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->pongMainFN_() ;
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1}), this->hdlrIDs_) << "REQ: no more cb since auto-rm" << endl;
}
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->pongMainFN_();
if (this->msgSelf_->nMsg()) 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->pongMainFN_() ;
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({3}), this->hdlrIDs_) << "REQ: no more cb since auto-rm" << endl;
}
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->pongMainFN_();
if (this->msgSelf_->nMsg()) 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->pongMainFN_() ;
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3}), this->hdlrIDs_) << "REQ: no more cb since auto-rm" << endl;

// 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->pongMainFN_() ;
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3, 4, 5, 6}), this->hdlrIDs_) << "REQ: re-add ok" << endl;

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->hasMsg()) this->pongMainFN_() ;
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3, 4, 5, 6}), this->hdlrIDs_) << "REQ: no more cb since auto-rm" << endl;
}
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->pongMainFN_(); // 1st cb
if (this->msgSelf_->nMsg()) this->pongMainFN_(); // 1st cb
EXPECT_EQ(multiset<int>({1, 2, 3}), this->hdlrIDs_) << "REQ: not auto-rm" << endl;

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

// 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->pongMainFN_() ;
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
EXPECT_EQ(multiset<int>({1, 2, 3, 1, 2, 3, 1, 2, 3}), this->hdlrIDs_) << "REQ: re-add nok" << endl;
}
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->pongMainFN_();
if (this->msgSelf_->nMsg()) 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->pongMainFN_();
if (this->msgSelf_->nMsg()) 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->pongMainFN_();
if (this->msgSelf_->nMsg()) this->pongMainFN_();
EXPECT_EQ(multiset<int>({1, 2}), this->hdlrIDs_); // req: no more cb since auto-rm
}

Expand All @@ -183,15 +183,15 @@ TYPED_TEST_P(FreeHdlrDominoTest, BugFix_noMemLeak_whenRmMsgSelf) // checked by
EXPECT_CALL(*this, h7()).Times(0);
PARA_DOM->setHdlr("e1", [&](){ this->h7(); });
PARA_DOM->setState({{"e1", true}});
ASSERT_TRUE(this->msgSelf_->hasMsg());
ASSERT_TRUE(this->msgSelf_->nMsg());
this->msgSelf_.reset(); // req: no mem leak when rm MsgSelf with h7 in msg queue
}
TYPED_TEST_P(FreeHdlrDominoTest, BugFix_noCrash_whenRmDom)
{
EXPECT_CALL(*this, h7()).Times(0);
PARA_DOM->setHdlr("e1", [&](){ this->h7(); });
PARA_DOM->setState({{"e1", true}});
ASSERT_TRUE(this->msgSelf_->hasMsg());
ASSERT_TRUE(this->msgSelf_->nMsg());
ObjAnywhere::set<TypeParam>(nullptr, *this); // req: no mem leak when rm MsgSelf with h7 in msg queue
this->pongMainFN_();
}
Expand Down
4 changes: 2 additions & 2 deletions ut/domino/HdlrDominoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ TYPED_TEST_P(HdlrDominoTest, rmHdlrOnRoad_noCallback)
PARA_DOM->multiHdlrByAliasEv("e0", this->hdlr0_, "e");
PARA_DOM->multiHdlrByAliasEv("e1", this->hdlr1_, "e");
PARA_DOM->setState({{"e", true}}); // cb on road
EXPECT_TRUE(msgSelf->hasMsg());
EXPECT_TRUE(msgSelf->nMsg());
EXPECT_TRUE(PARA_DOM->rmOneHdlrOK("e0")) << "REQ: rm hdlr on-road" << endl;

EXPECT_CALL(*this, hdlr0()).Times(0);
Expand Down Expand Up @@ -242,7 +242,7 @@ TYPED_TEST_P(NofreeHdlrDominoTest, rmHdlrOnRoad_thenReAdd_noCallbackUntilReTrigg
this->pongMainFN_();

PARA_DOM->setState({{"event", true}});
ASSERT_TRUE(msgSelf->hasMsg());
ASSERT_TRUE(msgSelf->nMsg());
EXPECT_CALL(*this, hdlr0()); // REQ: new cb
this->pongMainFN_();
}
Expand Down
6 changes: 3 additions & 3 deletions ut/domino/MultiHdlrDominoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,22 @@ TYPED_TEST_P(NofreeMultiHdlrDominoTest, rmHdlrOnRoad)
PARA_DOM->multiHdlrOnSameEv("event", this->hdlr1_, "this->hdlr1_");
PARA_DOM->multiHdlrOnSameEv("event", this->hdlr2_, "this->hdlr2_");
PARA_DOM->setState({{"event", true}}); // 3 cb on road
EXPECT_TRUE(msgSelf->hasMsg());
EXPECT_TRUE(msgSelf->nMsg());
EXPECT_TRUE(PARA_DOM->rmOneHdlrOK("event")) << "REQ: invalidate HdlrDom" << endl;
EXPECT_TRUE(PARA_DOM->rmOneHdlrOK("event", "this->hdlr2_")) << "REQ: invalidate MultiDom" << endl;

EXPECT_CALL(*this, hdlr0()).Times(0);
EXPECT_CALL(*this, hdlr1());
EXPECT_CALL(*this, hdlr2()).Times(0);
if (msgSelf->hasMsg()) this->pongMainFN_(); // manual trigger on road cb
if (msgSelf->nMsg()) 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->pongMainFN_(); // manual trigger on road cb
if (msgSelf->nMsg()) this->pongMainFN_(); // manual trigger on road cb
}
// ***********************************************************************************************
// rm invalid
Expand Down
2 changes: 1 addition & 1 deletion ut/domino/PriDominoTest.cpp
Original file line number Diff line number Diff line change
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->pongMainFN_();
if (this->msgSelf_->nMsg()) 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
6 changes: 3 additions & 3 deletions ut/msg_self/MsgSelfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ struct MsgSelfTest : public Test, public UniLog
TEST_F(MsgSelfTest, GOLD_sendMsg)
{
EXPECT_EQ(0u, msgSelf_->nMsg(EMsgPri_NORM)) << "REQ: init states" << endl;
EXPECT_FALSE(msgSelf_->hasMsg());
EXPECT_FALSE(msgSelf_->nMsg());

EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_)); // req: newMsg->pingMain that eg send msg to self
msgSelf_->newMsg(d1MsgHdlr_);
EXPECT_EQ(1u, msgSelf_->nMsg(EMsgPri_NORM));
EXPECT_TRUE(msgSelf_->hasMsg());
EXPECT_TRUE(msgSelf_->nMsg());
EXPECT_EQ(queue<int>(), hdlrIDs_) << "REQ: not immediate call d1MsgHdlr_ but wait msg-to-self" << endl;

EXPECT_CALL(*this, pingMain(_)).Times(0); // req: no more no call
pongMainFN_(); // simulate eg msg-to-self received, then call pongMainFN_()
EXPECT_EQ(queue<int>({1}), hdlrIDs_) << "REQ: call d1MsgHdlr_" << endl;
EXPECT_EQ(0u, msgSelf_->nMsg(EMsgPri_NORM));
EXPECT_FALSE(msgSelf_->hasMsg());
EXPECT_FALSE(msgSelf_->nMsg());
}
TEST_F(MsgSelfTest, dupSendMsg)
{
Expand Down
12 changes: 6 additions & 6 deletions ut/thread/MT_SemphoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,29 @@ TEST_F(MT_SemaphoreTest, GOLD_integrate_MsgSelf_ThreadBack_MtInQueue) // simula
for (;;)
{
// handle all done Thread
INF("nMsg=" << msgSelf_->nMsg(EMsgPri_NORM) << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
INF("nMsg=" << msgSelf_->nMsg() << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
ThreadBack::hdlFinishedThreads();

// handle all existing in mtQ_
for (;;)
{
INF("nMsg=" << msgSelf_->nMsg(EMsgPri_NORM) << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
INF("nMsg=" << msgSelf_->nMsg() << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
auto elePair = mtQ_.pop();
if (elePair.first == nullptr) break; // handle next eg MsgSelf queue

INF("nMsg=" << msgSelf_->nMsg(EMsgPri_NORM) << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
INF("nMsg=" << msgSelf_->nMsg() << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
msgHdlrs[elePair.second](elePair.first);
}

INF("nMsg=" << msgSelf_->nMsg(EMsgPri_NORM) << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
INF("nMsg=" << msgSelf_->nMsg() << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
if (pongMainFN_) // possible nullptr
pongMainFN_(); // handle all existing in MsgSelf

INF("nMsg=" << msgSelf_->nMsg(EMsgPri_NORM) << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
INF("nMsg=" << msgSelf_->nMsg() << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
if (expect == cb_info)
return;

INF("nMsg=" << msgSelf_->nMsg(EMsgPri_NORM) << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
INF("nMsg=" << msgSelf_->nMsg() << ", nQ=" << mtQ_.mt_size() << ", nTh=" << ThreadBack::nThread());
mt_waker_.mt_wait();
}
}
Expand Down

0 comments on commit b351bb7

Please sign in to comment.