From 9d9f4f1e7aa374ccb33f4f98eb421570239afda7 Mon Sep 17 00:00:00 2001 From: Runming Wu Date: Mon, 22 Aug 2022 13:29:31 -0700 Subject: [PATCH] Add unit test. Change-Id: I8cbe5e7bc036f64a9c1ea0006d2baf89f3cd1bd2 --- lib/ZeroMQChannel.cpp | 26 +------------------- tests/tests.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/lib/ZeroMQChannel.cpp b/lib/ZeroMQChannel.cpp index 32e27a4f3..f037610a9 100644 --- a/lib/ZeroMQChannel.cpp +++ b/lib/ZeroMQChannel.cpp @@ -247,31 +247,7 @@ void ZeroMQChannel::del( std::vector values; - swss::FieldValueTuple opdata(key, command); - - values.insert(values.begin(), opdata); - - std::string msg = swss::JSon::buildJson(values); - - SWSS_LOG_DEBUG("sending: %s", msg.c_str()); - - for (int i = 0; true ; ++i) - { - int rc = zmq_send(m_socket, msg.c_str(), msg.length(), 0); - - if (rc <= 0 && zmq_errno() == EINTR && i < ZMQ_MAX_RETRY) - { - continue; - } - if (rc <= 0) - { - SWSS_LOG_THROW("zmq_send failed, on endpoint %s, zmqerrno: %d: %s", - m_endpoint.c_str(), - zmq_errno(), - zmq_strerror(zmq_errno())); - } - break; - } + set(key, values, command); } sai_status_t ZeroMQChannel::wait( diff --git a/tests/tests.cpp b/tests/tests.cpp index e11805484..7214bda70 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -11,6 +11,8 @@ #include "meta/sai_serialize.h" +#include +#include #include #include @@ -19,6 +21,18 @@ using namespace sairedis; #define ASSERT_EQ(a,b) if ((a) != (b)) { SWSS_LOG_THROW("ASSERT EQ FAILED: " #a " != " #b); } +#define ASSERT_THROW(a,b) \ + try { \ + a; \ + SWSS_LOG_ERROR("ASSERT_THROW FAILED"); \ + exit(1); \ + } \ + catch(const b &e) { \ + } \ + catch(...) { \ + SWSS_LOG_THROW("ASSERT_THROW FAILED"); \ + } + /* * Test if destructor proper clean and join zeromq socket and context, and * break recv method. @@ -86,13 +100,54 @@ static void test_zeromqchannel_first_notification() } } +void send_signals() +{ + SWSS_LOG_ENTER(); + pid_t pid = getpid(); + for (int i = 0; i < 11; ++i) + { + sleep(1); + kill(pid, SIGHUP); + } +}; + +/* + * Test if runtime_error will be thrown if zmq wait reaches max retry due to + * signal interrupt. + */ +static void test_zeromqchannel_eintr_errno_on_wait() +{ + SWSS_LOG_ENTER(); + + std::cout << " * " << __FUNCTION__ << std::endl; + + ZeroMQChannel z("ipc:///tmp/feeds1", "ipc:///tmp/feeds2", nullptr); + z.setResponseTimeout(60000); + + std::thread signal_thread(send_signals); + + swss::KeyOpFieldsValuesTuple kco; + ASSERT_THROW(z.wait("foo", kco), std::runtime_error); + + signal_thread.join(); +} + +void sighup_handler(int signo) +{ + SWSS_LOG_ENTER(); +} + int main() { SWSS_LOG_ENTER(); + signal(SIGHUP, sighup_handler); + test_zeromqchannel_destructor(); test_zeromqchannel_first_notification(); + test_zeromqchannel_eintr_errno_on_wait(); + return 0; }