From 5f6b004d635be95c52fb1e92c70b2af808bc3a29 Mon Sep 17 00:00:00 2001 From: kcudnik Date: Wed, 18 Aug 2021 20:25:50 +0200 Subject: [PATCH] Add NetMsgRegistrar tests --- unittest/vslib/Makefile.am | 3 +- unittest/vslib/TestNetMsgRegistrar.cpp | 53 ++++++++++++++++++++++++++ vslib/NetMsgRegistrar.cpp | 4 +- vslib/NetMsgRegistrar.h | 2 +- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 unittest/vslib/TestNetMsgRegistrar.cpp diff --git a/unittest/vslib/Makefile.am b/unittest/vslib/Makefile.am index 2835492f671b..49be5c7a3b82 100644 --- a/unittest/vslib/Makefile.am +++ b/unittest/vslib/Makefile.am @@ -21,7 +21,8 @@ tests_SOURCES = main.cpp \ TestMACsecAttr.cpp \ TestMACsecEgressFilter.cpp \ TestMACsecForwarder.cpp \ - TestMACsecIngressFilter.cpp + TestMACsecIngressFilter.cpp \ + TestNetMsgRegistrar.cpp tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 \ diff --git a/unittest/vslib/TestNetMsgRegistrar.cpp b/unittest/vslib/TestNetMsgRegistrar.cpp new file mode 100644 index 000000000000..be8cd4a30429 --- /dev/null +++ b/unittest/vslib/TestNetMsgRegistrar.cpp @@ -0,0 +1,53 @@ +#include "NetMsgRegistrar.h" + +#include + +using namespace saivs; + +static void callback(int, struct nl_object*) +{ + // empty +} + +TEST(NetMsgRegistrar, registerCallback) +{ + auto& reg = NetMsgRegistrar::getInstance(); + + auto index = reg.registerCallback(callback); + + reg.unregisterCallback(index); +} + +TEST(NetMsgRegistrar, unregisterAll) +{ + auto& reg = NetMsgRegistrar::getInstance(); + + reg.registerCallback(callback); + + usleep(100*1000); + + reg.unregisterAll(); +} + +TEST(NetMsgRegistrar, resetIndex) +{ + auto& reg = NetMsgRegistrar::getInstance(); + + reg.resetIndex(); + + auto index = reg.registerCallback(callback); + + reg.unregisterAll(); + + auto index2 = reg.registerCallback(callback); + + EXPECT_NE(index, index2); + + reg.unregisterAll(); + + reg.resetIndex(); + + auto index3 = reg.registerCallback(callback); + + EXPECT_EQ(index, index3); +} diff --git a/vslib/NetMsgRegistrar.cpp b/vslib/NetMsgRegistrar.cpp index 8b74666753e8..56702d4f351f 100644 --- a/vslib/NetMsgRegistrar.cpp +++ b/vslib/NetMsgRegistrar.cpp @@ -77,7 +77,7 @@ void NetMsgRegistrar::unregisterCallback( } } -void NetMsgRegistrar::unregisteraAll() +void NetMsgRegistrar::unregisterAll() { SWSS_LOG_ENTER(); @@ -129,7 +129,7 @@ void NetMsgRegistrar::run() } catch (const std::exception& e) { - SWSS_LOG_ERROR("exception: %s", e.what()); + SWSS_LOG_ERROR("NetMsgRegistrar::run: exception: %s", e.what()); break; } } diff --git a/vslib/NetMsgRegistrar.h b/vslib/NetMsgRegistrar.h index 8b69c6fe4a9e..568ec49d4dba 100644 --- a/vslib/NetMsgRegistrar.h +++ b/vslib/NetMsgRegistrar.h @@ -48,7 +48,7 @@ namespace saivs void unregisterCallback( _In_ uint64_t index); - void unregisteraAll(); + void unregisterAll(); void resetIndex();