Skip to content

Commit

Permalink
Add RedisChannel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Sep 20, 2021
1 parent 62b8fdd commit e16606a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
29 changes: 2 additions & 27 deletions lib/RedisChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,6 @@ std::shared_ptr<swss::DBConnector> RedisChannel::getDbConnector() const
return m_db;
}

static std::string getSelectResultAsString(int result)
{
SWSS_LOG_ENTER();

std::string res;

switch (result)
{
case swss::Select::ERROR:
res = "ERROR";
break;

case swss::Select::TIMEOUT:
res = "TIMEOUT";
break;

default:
SWSS_LOG_WARN("non recognized select result: %d", result);
res = std::to_string(result);
break;
}

return res;
}

void RedisChannel::notificationThreadFunction()
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -119,7 +94,7 @@ void RedisChannel::notificationThreadFunction()
}
else
{
SWSS_LOG_ERROR("select failed: %s", getSelectResultAsString(result).c_str());
SWSS_LOG_ERROR("select failed: %s", swss::Select::resultToString(result).c_str());
}
}
}
Expand Down Expand Up @@ -201,7 +176,7 @@ sai_status_t RedisChannel::wait(
return status;
}

SWSS_LOG_ERROR("SELECT operation result: %s on %s", getSelectResultAsString(result).c_str(), command.c_str());
SWSS_LOG_ERROR("SELECT operation result: %s on %s", swss::Select::resultToString(result).c_str(), command.c_str());
break;
}

Expand Down
3 changes: 2 additions & 1 deletion unittest/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ tests_SOURCES = \
TestSwitchConfigContainer.cpp \
TestSkipRecordAttrContainer.cpp \
TestServerConfig.cpp \
TestRedisVidIndexGenerator.cpp
TestRedisVidIndexGenerator.cpp \
TestRedisChannel.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/lib/libSaiRedis.a -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)
Expand Down
56 changes: 56 additions & 0 deletions unittest/lib/TestRedisChannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "RedisChannel.h"
#include "sairediscommon.h"

#include "swss/notificationproducer.h"

#include <gtest/gtest.h>

#include <memory>

using namespace sairedis;

static std::string g_op;
static std::string g_data;

static void callback(
_In_ const std::string& op,
_In_ const std::string& data,
_In_ const std::vector<swss::FieldValueTuple>& values)
{
SWSS_LOG_ENTER();

g_op = op;
g_data = data;
}

TEST(RedisChannel, reset)
{
RedisChannel rc("ASIC_DB", callback);

EXPECT_NE(nullptr, rc.getDbConnector());
}

TEST(RedisChannel, notificationThreadFunction)
{
RedisChannel rc("ASIC_DB", callback);

rc.setResponseTimeout(10);

auto db = std::make_shared<swss::DBConnector>("ASIC_DB", 0);

swss::NotificationProducer p(db.get(), REDIS_TABLE_NOTIFICATIONS);

std::vector<swss::FieldValueTuple> vals;
p.send("foo", "bar", vals);

usleep(200*1000);

EXPECT_EQ(g_op, "foo");
}

TEST(RedisChannel, flush)
{
RedisChannel rc("ASIC_DB", callback);

rc.flush();
}
2 changes: 2 additions & 0 deletions unittest/lib/files/server_config_bad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"zm
4 changes: 4 additions & 0 deletions unittest/lib/files/server_config_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"zmq_endpoint": "ipc:///tmp/saiServer",
"zmq_ntf_endpoint": "ipc:///tmp/saiServerNtf"
}

0 comments on commit e16606a

Please sign in to comment.