Skip to content

Commit

Permalink
test code written
Browse files Browse the repository at this point in the history
  • Loading branch information
renukamanavalan committed Jun 8, 2022
1 parent c903f02 commit 78e5176
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 27 deletions.
15 changes: 13 additions & 2 deletions src/sonic-eventd/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
RM := rm -rf
EVENTD_TARGET := eventd
EVENTD_TEST := tests/tests
CP := cp
MKDIR := mkdir
CC := g++
MV := mv
LIBS := -levent -lhiredis -lswsscommon -pthread -lboost_thread -lboost_system
LIBS := -levent -lhiredis -lswsscommon -lpthread -lboost_thread -lboost_system -lzmq -lboost_serialization -luuid
TEST_LIBS := -L/usr/src/gtest -lgtest -lgtest_main -lgmock -lgmock_main

CFLAGS += -Wall -std=c++17 -fPIE -I$(PWD)/../sonic-swss-common/common
PWD := $(shell pwd)

Expand All @@ -15,8 +18,9 @@ endif
endif

-include src/subdir.mk
-include tests/subdir.mk

all: sonic-eventd
all: sonic-eventd eventd-tests

sonic-eventd: $(OBJS)
@echo 'Building target: $@'
Expand All @@ -25,6 +29,13 @@ sonic-eventd: $(OBJS)
@echo 'Finished building target: $@'
@echo ' '

eventd-tests: $(TEST_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: G++ Linker'
$(CC) $(LDFLAGS) -o $(EVENTD_TEST) $(TEST_OBJS) $(LIBS) $(TEST_LIBS)
@echo 'Finished building target: $@'
@echo ' '

install:
$(MKDIR) -p $(DESTDIR)/usr/sbin
$(MV) $(EVENTD_TARGET) $(DESTDIR)/usr/sbin
Expand Down
31 changes: 10 additions & 21 deletions src/sonic-eventd/src/eventd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ capture_service::do_capture()
* Capture stop will close the socket which fail the read
* and hence bail out.
*/
return;
}


int
capture_service::set_control(capture_control_t ctrl, events_data_lst_t *lst)
{
Expand Down Expand Up @@ -255,7 +257,7 @@ capture_service::set_control(capture_control_t ctrl, events_data_lst_t *lst)
* if overall mem consumption is too high. Clearing the map just before use
* is likely to help.
*/
for (i=0; i<MAX_PUBLISHERS_COUNT; ++i) {
for (int i=0; i<MAX_PUBLISHERS_COUNT; ++i) {
m_last_events[to_string(i)] = "";
}

Expand Down Expand Up @@ -313,9 +315,9 @@ run_eventd_service()
SWSS_LOG_ERROR("Eventd service starting\n");

void *zctx = zmq_ctx_new();
RET_ON_ERR(ctx != NULL, "Failed to get zmq ctx");
RET_ON_ERR(zctx != NULL, "Failed to get zmq ctx");

cache_max = get_config_data(string(CACHE_MAX_CNT), (int)MAX_CACHE_SIZE));
cache_max = get_config_data(string(CACHE_MAX_CNT), (int)MAX_CACHE_SIZE);
RET_ON_ERR(cache_max > 0, "Failed to get CACHE_MAX_CNT");

proxy = new eventd_proxy(zctx);
Expand All @@ -329,7 +331,7 @@ run_eventd_service()
int code, resp = -1;
events_data_lst_t req_data, resp_data;

RET_ON_ERR(channel_read(code, data) == 0,
RET_ON_ERR(service.channel_read(code, req_data) == 0,
"Failed to read request");

switch(code) {
Expand All @@ -354,7 +356,7 @@ run_eventd_service()
resp = -1;
break;
}
resp = capture->set_control(START_CAPTURE, req_data);
resp = capture->set_control(START_CAPTURE, &req_data);
break;


Expand Down Expand Up @@ -401,7 +403,7 @@ run_eventd_service()
if (sz == VEC_SIZE(capture_fifo_events)) {
events_data_lst_t().swap(capture_fifo_events);
} else {
events.erase(capture_fifo_events.begin(), it);
capture_fifo_events.erase(capture_fifo_events.begin(), it);
}
}
}
Expand All @@ -417,11 +419,11 @@ run_eventd_service()
assert(false);
break;
}
RET_ON_ERR(channel_write(resp_code, resp_data) == 0,
RET_ON_ERR(service.channel_write(resp, resp_data) == 0,
"Failed to write response back");
}
out:
m_service.close();
service.close_service();
if (proxy != NULL) {
delete proxy;
}
Expand All @@ -434,16 +436,3 @@ run_eventd_service()
SWSS_LOG_ERROR("Eventd service exiting\n");
}


int main()
{
SWSS_LOG_INFO("The eventd service started");

run_eventd_service();

SWSS_LOG_INFO("The eventd service exited");

return 0;
}


2 changes: 0 additions & 2 deletions src/sonic-eventd/src/eventd.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ typedef enum {
} capture_control_t;


int capture_status;

class capture_service
{
public:
Expand Down
14 changes: 14 additions & 0 deletions src/sonic-eventd/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

void run_eventd_service();

int main()
{
SWSS_LOG_INFO("The eventd service started");

run_eventd_service();

SWSS_LOG_INFO("The eventd service exited");

return 0;
}

5 changes: 3 additions & 2 deletions src/sonic-eventd/src/subdir.mk
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CC := g++

OBJS += ./src/eventd.o
TEST_OBJS += ./src/eventd.o
OBJS += ./src/eventd.o ./src/main.o

C_DEPS += ./src/eventd.d
C_DEPS += ./src/eventd.d ./src/main.d

src/%.o: src/%.cpp
@echo 'Building file: $<'
Expand Down
117 changes: 117 additions & 0 deletions src/sonic-eventd/tests/eventd_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <iostream>
#include <memory>
#include <thread>
#include <algorithm>
#include <deque>
#include <regex>
#include <chrono>
#include "gtest/gtest.h"
#include "events_common.h"
#include "events.h"
#include "../src/eventd.h"

using namespace std;

typedef vector<internal_event_t> lst_events_t;

void run_sub(void *zctx, bool &term, string &read_source, lst_events_t &lst)
{
void *mock_sub = zmq_socket (zctx, ZMQ_SUB);
string source;
internal_event_t ev_int;
int block_ms = 200;

EXPECT_TRUE(NULL != mock_sub);
EXPECT_EQ(0, zmq_connect(mock_sub, get_config(XPUB_END_KEY).c_str()));
EXPECT_EQ(0, zmq_setsockopt(mock_sub, ZMQ_SUBSCRIBE, "", 0));
EXPECT_EQ(0, zmq_setsockopt(mock_sub, ZMQ_RCVTIMEO, &block_ms, sizeof (block_ms)));

while(!term) {
if (0 == zmq_message_read(mock_sub, 0, source, ev_int)) {
lst.push_back(ev_int);
read_source.swap(source);
}
}

zmq_close(mock_sub);
}

void *init_pub(void *zctx)
{
void *mock_pub = zmq_socket (zctx, ZMQ_PUB);
EXPECT_TRUE(NULL != mock_pub);
EXPECT_EQ(0, zmq_connect(mock_pub, get_config(XSUB_END_KEY).c_str()));

return mock_pub;
}

void run_pub(void *mock_pub, const string wr_source, lst_events_t &lst)
{
for(lst_events_t::const_iterator itc = lst.begin(); itc != lst.end(); ++itc) {
EXPECT_EQ(0, zmq_message_send(mock_pub, wr_source, *itc));
}
}


static internal_event_t
create_ev(const string rid, sequence_t n, const string d)
{
stringstream ss;

ss << d << ":" << n;

return internal_event_t({ {EVENT_STR_DATA, ss.str()},
{ EVENT_RUNTIME_ID, rid }, { EVENT_SEQUENCE, seq_to_str(n) }});
}



TEST(eventd, proxy)
{
printf("TEST started\n");
bool term_sub = false;
string rd_source, wr_source("hello");
lst_events_t rd_evts, wr_evts;

void *zctx = zmq_ctx_new();
EXPECT_TRUE(NULL != zctx);

eventd_proxy *pxy = new eventd_proxy(zctx);
EXPECT_TRUE(NULL != pxy);

/* Starting proxy */
EXPECT_EQ(0, pxy->init());

/* subscriber in a thread */
thread thr(&run_sub, zctx, ref(term_sub), ref(rd_source), ref(rd_evts));

/* Init pub connection */
void *mock_pub = init_pub(zctx);

/* Provide time for async connect to complete */
this_thread::sleep_for(chrono::milliseconds(100));

for(int i=0; i<5; ++i) {
wr_evts.push_back(create_ev("hello", i, "test body"));
}

EXPECT_TRUE(rd_evts.empty());
EXPECT_TRUE(rd_source.empty());

/* Publish events. */
run_pub(mock_pub, wr_source, wr_evts);

while(rd_evts.size() != wr_evts.size()) {
printf("rd_evts.size != wr_evts.size %d != %d\n",
(int)rd_evts.size(), (int)wr_evts.size());
this_thread::sleep_for(chrono::milliseconds(10));
}

term_sub = true;
printf("Waiting for sub thread to join...\n");

thr.join();
zmq_close(mock_pub);
zmq_ctx_term(zctx);
}

10 changes: 10 additions & 0 deletions src/sonic-eventd/tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "gtest/gtest.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
12 changes: 12 additions & 0 deletions src/sonic-eventd/tests/subdir.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CC := g++

TEST_OBJS += ./tests/eventd_ut.o ./tests/main.o

C_DEPS += ./tests/eventd_ut.d ./tests/main.d

tests/%.o: tests/%.cpp
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
$(CC) -D__FILENAME__="$(subst src/,,$<)" $(CFLAGS) -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

0 comments on commit 78e5176

Please sign in to comment.