Skip to content

Commit e16606a

Browse files
committed
Add RedisChannel tests
1 parent 62b8fdd commit e16606a

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

lib/RedisChannel.cpp

+2-27
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,6 @@ std::shared_ptr<swss::DBConnector> RedisChannel::getDbConnector() const
5757
return m_db;
5858
}
5959

60-
static std::string getSelectResultAsString(int result)
61-
{
62-
SWSS_LOG_ENTER();
63-
64-
std::string res;
65-
66-
switch (result)
67-
{
68-
case swss::Select::ERROR:
69-
res = "ERROR";
70-
break;
71-
72-
case swss::Select::TIMEOUT:
73-
res = "TIMEOUT";
74-
break;
75-
76-
default:
77-
SWSS_LOG_WARN("non recognized select result: %d", result);
78-
res = std::to_string(result);
79-
break;
80-
}
81-
82-
return res;
83-
}
84-
8560
void RedisChannel::notificationThreadFunction()
8661
{
8762
SWSS_LOG_ENTER();
@@ -119,7 +94,7 @@ void RedisChannel::notificationThreadFunction()
11994
}
12095
else
12196
{
122-
SWSS_LOG_ERROR("select failed: %s", getSelectResultAsString(result).c_str());
97+
SWSS_LOG_ERROR("select failed: %s", swss::Select::resultToString(result).c_str());
12398
}
12499
}
125100
}
@@ -201,7 +176,7 @@ sai_status_t RedisChannel::wait(
201176
return status;
202177
}
203178

204-
SWSS_LOG_ERROR("SELECT operation result: %s on %s", getSelectResultAsString(result).c_str(), command.c_str());
179+
SWSS_LOG_ERROR("SELECT operation result: %s on %s", swss::Select::resultToString(result).c_str(), command.c_str());
205180
break;
206181
}
207182

unittest/lib/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ tests_SOURCES = \
2020
TestSwitchConfigContainer.cpp \
2121
TestSkipRecordAttrContainer.cpp \
2222
TestServerConfig.cpp \
23-
TestRedisVidIndexGenerator.cpp
23+
TestRedisVidIndexGenerator.cpp \
24+
TestRedisChannel.cpp
2425

2526
tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
2627
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/lib/libSaiRedis.a -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)

unittest/lib/TestRedisChannel.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "RedisChannel.h"
2+
#include "sairediscommon.h"
3+
4+
#include "swss/notificationproducer.h"
5+
6+
#include <gtest/gtest.h>
7+
8+
#include <memory>
9+
10+
using namespace sairedis;
11+
12+
static std::string g_op;
13+
static std::string g_data;
14+
15+
static void callback(
16+
_In_ const std::string& op,
17+
_In_ const std::string& data,
18+
_In_ const std::vector<swss::FieldValueTuple>& values)
19+
{
20+
SWSS_LOG_ENTER();
21+
22+
g_op = op;
23+
g_data = data;
24+
}
25+
26+
TEST(RedisChannel, reset)
27+
{
28+
RedisChannel rc("ASIC_DB", callback);
29+
30+
EXPECT_NE(nullptr, rc.getDbConnector());
31+
}
32+
33+
TEST(RedisChannel, notificationThreadFunction)
34+
{
35+
RedisChannel rc("ASIC_DB", callback);
36+
37+
rc.setResponseTimeout(10);
38+
39+
auto db = std::make_shared<swss::DBConnector>("ASIC_DB", 0);
40+
41+
swss::NotificationProducer p(db.get(), REDIS_TABLE_NOTIFICATIONS);
42+
43+
std::vector<swss::FieldValueTuple> vals;
44+
p.send("foo", "bar", vals);
45+
46+
usleep(200*1000);
47+
48+
EXPECT_EQ(g_op, "foo");
49+
}
50+
51+
TEST(RedisChannel, flush)
52+
{
53+
RedisChannel rc("ASIC_DB", callback);
54+
55+
rc.flush();
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
"zm
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"zmq_endpoint": "ipc:///tmp/saiServer",
3+
"zmq_ntf_endpoint": "ipc:///tmp/saiServerNtf"
4+
}

0 commit comments

Comments
 (0)