Skip to content

Commit b08e317

Browse files
committed
add unit tests
1 parent b0a0739 commit b08e317

File tree

8 files changed

+120
-15
lines changed

8 files changed

+120
-15
lines changed

src/DbInterface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,10 @@ void DbInterface::warmRestartReconciliation(const std::string &portName)
826826
{
827827
MUXLOGDEBUG(portName);
828828

829-
setMuxMode(portName, "auto");
830-
mMuxManagerPtr->updateWarmRestartReconciliationCount(-1);
829+
if (isWarmStart()) {
830+
setMuxMode(portName, "auto");
831+
mMuxManagerPtr->updateWarmRestartReconciliationCount(-1);
832+
}
831833
}
832834

833835
//

src/DbInterface.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "swss/dbconnector.h"
3535
#include "swss/producerstatetable.h"
3636
#include "swss/subscriberstatetable.h"
37+
#include "swss/warm_restart.h"
3738

3839
#include "link_manager/LinkManagerStateMachineActiveStandby.h"
3940
#include "mux_state/MuxState.h"
@@ -295,7 +296,34 @@ class DbInterface
295296
*
296297
* @return none
297298
*/
298-
virtual void warmRestartReconciliation(const std::string &portName);
299+
void warmRestartReconciliation(const std::string &portName);
300+
301+
/**
302+
* @method isWarmStart
303+
*
304+
* @brief is warm start or not
305+
*
306+
* @return system flag for warm start context
307+
*/
308+
virtual bool isWarmStart(){return swss::WarmStart::isWarmStart();};
309+
310+
/**
311+
* @method getWarmStartTimer
312+
*
313+
* @brief get warm start time out in sec
314+
*
315+
* @return timeout in sec
316+
*/
317+
virtual uint32_t getWarmStartTimer(){return swss::WarmStart::getWarmStartTimer("linkmgrd", "mux");};
318+
319+
/**
320+
* @method setWarmStartStateReconciled
321+
*
322+
* @brief set warm start state reconciled
323+
*
324+
* @return none
325+
*/
326+
virtual void setWarmStartStateReconciled(){swss::WarmStart::setWarmStartState("linkmgrd", swss::WarmStart::RECONCILED);};
299327

300328
private:
301329
friend class test::MuxManagerTest;

src/MuxManager.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
#include <boost/bind/bind.hpp>
3030

31-
#include "swss/warm_restart.h"
32-
3331
#include "common/MuxException.h"
3432
#include "common/MuxLogger.h"
3533
#include "MuxManager.h"
@@ -80,21 +78,20 @@ void MuxManager::setUseWellKnownMacActiveActive(bool useWellKnownMac)
8078
//
8179
void MuxManager::initialize(bool enable_feature_measurement, bool enable_feature_default_route)
8280
{
83-
8481
for (uint8_t i = 0; (mMuxConfig.getNumberOfThreads() > 2) &&
8582
(i < mMuxConfig.getNumberOfThreads() - 2); i++) {
8683
mThreadGroup.create_thread(
8784
boost::bind(&boost::asio::io_service::run, &mIoService)
8885
);
8986
}
9087

91-
if (swss::WarmStart::isWarmStart()) {
88+
mDbInterfacePtr->initialize();
89+
90+
if (mDbInterfacePtr->isWarmStart()) {
9291
MUXLOGINFO("Detected warm restart context, starting reconciliation timer.");
93-
startWarmRestartReconciliationTimer(swss::WarmStart::getWarmStartTimer("linkmgrd", "mux"));
92+
startWarmRestartReconciliationTimer(mDbInterfacePtr->getWarmStartTimer());
9493
}
9594

96-
mDbInterfacePtr->initialize();
97-
9895
mMuxConfig.enableSwitchoverMeasurement(enable_feature_measurement);
9996
mMuxConfig.enableDefaultRouteFeature(enable_feature_default_route);
10097
}
@@ -575,7 +572,7 @@ void MuxManager::handleWarmRestartReconciliationTimeout(const boost::system::err
575572
MUXLOGWARNING("Reconciliation timed out after warm restart, set service to reconciled now.");
576573
}
577574

578-
swss::WarmStart::setWarmStartState("linkmgrd", swss::WarmStart::RECONCILED);
575+
mDbInterfacePtr->setWarmStartStateReconciled();
579576
}
580577

581578
} /* namespace mux */

src/MuxPort.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include <boost/random/uniform_int_distribution.hpp>
3737
#include <boost/random/mersenne_twister.hpp>
3838

39-
#include "swss/warm_restart.h"
40-
4139
#include "MuxPort.h"
4240
#include "common/MuxException.h"
4341
#include "common/MuxLogger.h"
@@ -414,7 +412,7 @@ void MuxPort::probeMuxState()
414412
//
415413
void MuxPort::warmRestartReconciliation()
416414
{
417-
if (swss::WarmStart::isWarmStart() && mMuxPortConfig.getMode() != common::MuxPortConfig::Mode::Auto) {
415+
if (mMuxPortConfig.getMode() != common::MuxPortConfig::Mode::Auto) {
418416
mDbInterfacePtr->warmRestartReconciliation(mMuxPortConfig.getPortName());
419417
}
420418
}

test/FakeDbInterface.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,24 @@ void FakeDbInterface::postPckLossRatio(
9797
mExpectedPacketCount = expectedPacketCount;
9898
}
9999

100+
void FakeDbInterface::handleSetMuxMode(const std::string &portName, const std::string state)
101+
{
102+
mSetMuxModeInvokeCount += 1;
103+
}
104+
105+
bool FakeDbInterface::isWarmStart()
106+
{
107+
return mWarmStartFlag;
108+
}
109+
110+
uint32_t FakeDbInterface::getWarmStartTimer()
111+
{
112+
return 0;
113+
}
114+
115+
void FakeDbInterface::setWarmStartStateReconciled()
116+
{
117+
mSetWarmStartStateReconciledInvokeCount++;
118+
}
119+
100120
} /* namespace test */

test/FakeDbInterface.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ class FakeDbInterface: public mux::DbInterface
5959
const uint64_t unknownEventCount,
6060
const uint64_t expectedPacketCount
6161
) override;
62-
62+
virtual bool isWarmStart() override;
63+
virtual uint32_t getWarmStartTimer() override;
64+
virtual void setWarmStartStateReconciled() override;
6365

6466
void setNextMuxState(mux_state::MuxState::Label label) {mNextMuxState = label;};
6567

68+
private:
69+
virtual void handleSetMuxMode(const std::string &portName, const std::string state) override;
6670

6771
public:
6872
mux_state::MuxState::Label mNextMuxState;
@@ -82,6 +86,9 @@ class FakeDbInterface: public mux::DbInterface
8286
uint32_t mPostLinkProberMetricsInvokeCount = 0;
8387
uint64_t mUnknownEventCount = 0;
8488
uint64_t mExpectedPacketCount = 0;
89+
uint32_t mSetMuxModeInvokeCount = 0;
90+
uint32_t mSetWarmStartStateReconciledInvokeCount = 0;
91+
bool mWarmStartFlag = false;
8592
};
8693

8794
} /* namespace test */

test/MuxManagerTest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ void MuxManagerTest::updatePortCableType(const std::string &port, const std::str
245245
mMuxManagerPtr->updatePortCableType(port, cableType);
246246
}
247247

248+
void MuxManagerTest::warmRestartReconciliation(const std::string &portName)
249+
{
250+
std::shared_ptr<mux::MuxPort> muxPortPtr = mMuxManagerPtr->mPortMap[portName];
251+
252+
muxPortPtr->warmRestartReconciliation();
253+
}
254+
255+
void MuxManagerTest::updatePortReconciliationCount(int increment)
256+
{
257+
mMuxManagerPtr->updateWarmRestartReconciliationCount(increment);
258+
runIoService(1);
259+
}
260+
261+
void MuxManagerTest::startWarmRestartReconciliationTimer(uint32_t timeout)
262+
{
263+
mMuxManagerPtr->startWarmRestartReconciliationTimer(
264+
timeout
265+
);
266+
}
267+
248268
void MuxManagerTest::initLinkProberActiveActive(std::shared_ptr<link_manager::ActiveActiveStateMachine> linkManagerStateMachineActiveActive)
249269
{
250270
mFakeLinkProber = std::make_shared<FakeLinkProber> (linkManagerStateMachineActiveActive->getLinkProberStateMachinePtr().get());
@@ -818,4 +838,34 @@ INSTANTIATE_TEST_CASE_P(
818838
)
819839
);
820840

841+
TEST_F(MuxManagerTest, WarmRestart)
842+
{
843+
std::string port = "Ethernet0";
844+
845+
createPort(port);
846+
847+
mDbInterfacePtr->mWarmStartFlag = true;
848+
startWarmRestartReconciliationTimer(UINT32_MAX);
849+
updatePortReconciliationCount(1);
850+
warmRestartReconciliation(port);
851+
852+
runIoService(3);
853+
854+
EXPECT_EQ(mDbInterfacePtr->mSetMuxModeInvokeCount, 1);
855+
EXPECT_EQ(mDbInterfacePtr->mSetWarmStartStateReconciledInvokeCount, 1);
856+
}
857+
858+
TEST_F(MuxManagerTest, WarmRestartTimeout)
859+
{
860+
std::string port = "Ethernet0";
861+
862+
createPort(port);
863+
864+
mDbInterfacePtr->mWarmStartFlag = true;
865+
startWarmRestartReconciliationTimer(mDbInterfacePtr->getWarmStartTimer());
866+
867+
runIoService(1);
868+
EXPECT_EQ(mDbInterfacePtr->mSetWarmStartStateReconciledInvokeCount, 1);
869+
}
870+
821871
} /* namespace test */

test/MuxManagerTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class MuxManagerTest: public testing::Test
8383
void initLinkProberActiveStandby(std::shared_ptr<link_manager::ActiveStandbyStateMachine> linkManagerStateMachine);
8484
void generateServerMac(const std::string &portName, std::array<uint8_t, ETHER_ADDR_LEN> &address);
8585
void createPort(std::string port, common::MuxPortConfig::PortCableType portCableType = common::MuxPortConfig::PortCableType::ActiveStandby);
86+
void warmRestartReconciliation(const std::string &portName);
87+
void updatePortReconciliationCount(int increment);
88+
void startWarmRestartReconciliationTimer(uint32_t timeout);
8689

8790
public:
8891
static const std::string PortName;

0 commit comments

Comments
 (0)