Skip to content

Commit

Permalink
MsgSelf & FreeDom: replace pongMainFN_ by handleAllMsg in ut
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Oct 27, 2023
1 parent f56c0a0 commit 060d6bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/msg_self/MsgSelf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MsgSelf : public UniLog
void newMsg(const MsgCB&, const EMsgPriority = EMsgPri_NORM); // can't withdraw CB but easier usage
size_t nMsg() const { return nMsg_; }
size_t nMsg(const EMsgPriority aPriority) const { return msgQueues_[aPriority].size(); }
void handleAllMsg(const shared_ptr<bool> aValidMsgSelf = make_shared<bool>(true));
void handleAllMsg(const shared_ptr<bool> aValidMsgSelf);

static bool isLowPri(const EMsgPriority aPri) { return aPri < EMsgPri_NORM; }

Expand Down
30 changes: 15 additions & 15 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_->nMsg()) this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
EXPECT_EQ(multiset<int>({1}), this->hdlrIDs_); // cb

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()) ;
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_->nMsg()) this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
EXPECT_EQ(multiset<int>({3}), this->hdlrIDs_); // cb

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()) ;
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_->nMsg()) this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
EXPECT_EQ(multiset<int>({1, 2, 3}), this->hdlrIDs_); // cb

PARA_DOM->setState({{"e1", false}});
PARA_DOM->setState({{"e1", true}});
if (this->msgSelf_->nMsg()) this->pongMainFN_() ;
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()) ;
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_->nMsg()) this->pongMainFN_() ;
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()) ;
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_->nMsg()) this->pongMainFN_() ;
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()) ;
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_->nMsg()) this->pongMainFN_(); // 1st cb
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()); // 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_->nMsg()) this->pongMainFN_(); // 2nd cb
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()); // 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_->nMsg()) this->pongMainFN_() ;
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid()) ;
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_->nMsg()) this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
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_->nMsg()) this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
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_->nMsg()) this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
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_->nMsg());
ObjAnywhere::set<TypeParam>(nullptr, *this); // req: no mem leak when rm MsgSelf with h7 in msg queue
this->pongMainFN_();
this->msgSelf_->handleAllMsg(this->msgSelf_->getValid());
}

#define ID_STATE
Expand Down
22 changes: 6 additions & 16 deletions ut/msg_self/MsgSelfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ struct MsgSelfTest : public Test, public UniLog
MsgSelfTest() : UniLog(UnitTest::GetInstance()->current_test_info()->name()) {}
~MsgSelfTest() { GTEST_LOG_FAIL }

MOCK_METHOD(void, pingMain, (const PongMainFN&)); // simulate pingMain() which may send msg to self

// -------------------------------------------------------------------------------------------
shared_ptr<MsgSelf> msgSelf_ = make_shared<MsgSelf>(
[&](const PongMainFN& aPongFn){ this->pingMain(aPongFn); },
uniLogName()
);
PongMainFN pongMainFN_ = nullptr;
MOCK_METHOD(void, pingMain, (const PongMainFN&));

MsgCB d1MsgHdlr_ = [&](){ hdlrIDs_.push(1); };
MsgCB d2MsgHdlr_ = [&](){ hdlrIDs_.push(2); };
Expand All @@ -48,12 +46,12 @@ struct MsgSelfTest : public Test, public UniLog
// ***********************************************************************************************
TEST_F(MsgSelfTest, GOLD_sendMsg)
{
EXPECT_EQ(0u, msgSelf_->nMsg(EMsgPri_NORM)) << "REQ: init states" << endl;
EXPECT_EQ(0u, msgSelf_->nMsg()) << "REQ: init states" << endl;
EXPECT_FALSE(msgSelf_->nMsg());

msgSelf_->newMsg(d1MsgHdlr_);
EXPECT_EQ(1u, msgSelf_->nMsg(EMsgPri_NORM));
EXPECT_TRUE(msgSelf_->nMsg());
EXPECT_EQ(1u, 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
Expand All @@ -64,7 +62,6 @@ TEST_F(MsgSelfTest, GOLD_sendMsg)
}
TEST_F(MsgSelfTest, dupSendMsg)
{
EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->newMsg(d1MsgHdlr_);
msgSelf_->newMsg(d1MsgHdlr_); // req: dup
EXPECT_EQ(2u, msgSelf_->nMsg(EMsgPri_NORM));
Expand All @@ -77,7 +74,6 @@ TEST_F(MsgSelfTest, dupSendMsg)
}
TEST_F(MsgSelfTest, sendInvalidMsg_noCrash)
{
EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->newMsg(d1MsgHdlr_); // valid cb to get pongMainFN_
msgSelf_->handleAllMsg(msgSelf_->getValid());
EXPECT_EQ(queue<int>({1}), hdlrIDs_);
Expand All @@ -88,7 +84,6 @@ TEST_F(MsgSelfTest, sendInvalidMsg_noCrash)
}
TEST_F(MsgSelfTest, GOLD_loopback_handleAll_butOneByOneLowPri)
{
EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->newMsg(d1MsgHdlr_);
msgSelf_->newMsg(d1MsgHdlr_);
msgSelf_->newMsg(d1MsgHdlr_, EMsgPri_HIGH);
Expand All @@ -100,13 +95,11 @@ TEST_F(MsgSelfTest, GOLD_loopback_handleAll_butOneByOneLowPri)
EXPECT_EQ(2u, msgSelf_->nMsg(EMsgPri_HIGH));
EXPECT_EQ(3u, msgSelf_->nMsg(EMsgPri_LOW));

EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->handleAllMsg(msgSelf_->getValid());
EXPECT_EQ(0u, msgSelf_->nMsg(EMsgPri_NORM)) << "REQ: all normal" << endl;
EXPECT_EQ(0u, msgSelf_->nMsg(EMsgPri_HIGH)) << "REQ: all high" << endl;
EXPECT_EQ(2u, msgSelf_->nMsg(EMsgPri_LOW)) << "REQ: 1/loopback()" << endl;

EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->handleAllMsg(msgSelf_->getValid());
EXPECT_EQ(1u, msgSelf_->nMsg(EMsgPri_LOW));

Expand All @@ -123,33 +116,30 @@ TEST_F(MsgSelfTest, GOLD_loopback_handleAll_butOneByOneLowPri)
// ***********************************************************************************************
TEST_F(MsgSelfTest, GOLD_highPriority_first)
{
EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->newMsg(d1MsgHdlr_);
msgSelf_->newMsg(d2MsgHdlr_, EMsgPri_HIGH);
msgSelf_->handleAllMsg(msgSelf_->getValid());

msgSelf_->handleAllMsg(msgSelf_->getValid());
EXPECT_EQ(queue<int>({2, 1}), hdlrIDs_);
}
TEST_F(MsgSelfTest, GOLD_samePriority_fifo)
{
EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->newMsg(d1MsgHdlr_);
msgSelf_->newMsg(d2MsgHdlr_, EMsgPri_HIGH);
msgSelf_->newMsg(d3MsgHdlr_);
msgSelf_->newMsg(d4MsgHdlr_, EMsgPri_HIGH);
msgSelf_->handleAllMsg(msgSelf_->getValid());

msgSelf_->handleAllMsg(msgSelf_->getValid());
EXPECT_EQ(queue<int>({2, 4, 1, 3}), hdlrIDs_);
}
TEST_F(MsgSelfTest, newHighPri_first)
{
EXPECT_CALL(*this, pingMain(_)).WillOnce(SaveArg<0>(&pongMainFN_));
msgSelf_->newMsg(d1MsgHdlr_);
msgSelf_->newMsg(d5MsgHdlr_, EMsgPri_HIGH);
msgSelf_->newMsg(d3MsgHdlr_);
msgSelf_->newMsg(d4MsgHdlr_, EMsgPri_HIGH);
msgSelf_->handleAllMsg(msgSelf_->getValid());

msgSelf_->handleAllMsg(msgSelf_->getValid());
EXPECT_EQ(queue<int>({5, 4, 2, 1, 3}), hdlrIDs_);
}

Expand Down

0 comments on commit 060d6bf

Please sign in to comment.