Skip to content

Commit 78e5176

Browse files
test code written
1 parent c903f02 commit 78e5176

File tree

8 files changed

+179
-27
lines changed

8 files changed

+179
-27
lines changed

src/sonic-eventd/Makefile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
RM := rm -rf
22
EVENTD_TARGET := eventd
3+
EVENTD_TEST := tests/tests
34
CP := cp
45
MKDIR := mkdir
56
CC := g++
67
MV := mv
7-
LIBS := -levent -lhiredis -lswsscommon -pthread -lboost_thread -lboost_system
8+
LIBS := -levent -lhiredis -lswsscommon -lpthread -lboost_thread -lboost_system -lzmq -lboost_serialization -luuid
9+
TEST_LIBS := -L/usr/src/gtest -lgtest -lgtest_main -lgmock -lgmock_main
10+
811
CFLAGS += -Wall -std=c++17 -fPIE -I$(PWD)/../sonic-swss-common/common
912
PWD := $(shell pwd)
1013

@@ -15,8 +18,9 @@ endif
1518
endif
1619

1720
-include src/subdir.mk
21+
-include tests/subdir.mk
1822

19-
all: sonic-eventd
23+
all: sonic-eventd eventd-tests
2024

2125
sonic-eventd: $(OBJS)
2226
@echo 'Building target: $@'
@@ -25,6 +29,13 @@ sonic-eventd: $(OBJS)
2529
@echo 'Finished building target: $@'
2630
@echo ' '
2731

32+
eventd-tests: $(TEST_OBJS)
33+
@echo 'Building target: $@'
34+
@echo 'Invoking: G++ Linker'
35+
$(CC) $(LDFLAGS) -o $(EVENTD_TEST) $(TEST_OBJS) $(LIBS) $(TEST_LIBS)
36+
@echo 'Finished building target: $@'
37+
@echo ' '
38+
2839
install:
2940
$(MKDIR) -p $(DESTDIR)/usr/sbin
3041
$(MV) $(EVENTD_TARGET) $(DESTDIR)/usr/sbin

src/sonic-eventd/src/eventd.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ capture_service::do_capture()
217217
* Capture stop will close the socket which fail the read
218218
* and hence bail out.
219219
*/
220+
return;
220221
}
221222

223+
222224
int
223225
capture_service::set_control(capture_control_t ctrl, events_data_lst_t *lst)
224226
{
@@ -255,7 +257,7 @@ capture_service::set_control(capture_control_t ctrl, events_data_lst_t *lst)
255257
* if overall mem consumption is too high. Clearing the map just before use
256258
* is likely to help.
257259
*/
258-
for (i=0; i<MAX_PUBLISHERS_COUNT; ++i) {
260+
for (int i=0; i<MAX_PUBLISHERS_COUNT; ++i) {
259261
m_last_events[to_string(i)] = "";
260262
}
261263

@@ -313,9 +315,9 @@ run_eventd_service()
313315
SWSS_LOG_ERROR("Eventd service starting\n");
314316

315317
void *zctx = zmq_ctx_new();
316-
RET_ON_ERR(ctx != NULL, "Failed to get zmq ctx");
318+
RET_ON_ERR(zctx != NULL, "Failed to get zmq ctx");
317319

318-
cache_max = get_config_data(string(CACHE_MAX_CNT), (int)MAX_CACHE_SIZE));
320+
cache_max = get_config_data(string(CACHE_MAX_CNT), (int)MAX_CACHE_SIZE);
319321
RET_ON_ERR(cache_max > 0, "Failed to get CACHE_MAX_CNT");
320322

321323
proxy = new eventd_proxy(zctx);
@@ -329,7 +331,7 @@ run_eventd_service()
329331
int code, resp = -1;
330332
events_data_lst_t req_data, resp_data;
331333

332-
RET_ON_ERR(channel_read(code, data) == 0,
334+
RET_ON_ERR(service.channel_read(code, req_data) == 0,
333335
"Failed to read request");
334336

335337
switch(code) {
@@ -354,7 +356,7 @@ run_eventd_service()
354356
resp = -1;
355357
break;
356358
}
357-
resp = capture->set_control(START_CAPTURE, req_data);
359+
resp = capture->set_control(START_CAPTURE, &req_data);
358360
break;
359361

360362

@@ -401,7 +403,7 @@ run_eventd_service()
401403
if (sz == VEC_SIZE(capture_fifo_events)) {
402404
events_data_lst_t().swap(capture_fifo_events);
403405
} else {
404-
events.erase(capture_fifo_events.begin(), it);
406+
capture_fifo_events.erase(capture_fifo_events.begin(), it);
405407
}
406408
}
407409
}
@@ -417,11 +419,11 @@ run_eventd_service()
417419
assert(false);
418420
break;
419421
}
420-
RET_ON_ERR(channel_write(resp_code, resp_data) == 0,
422+
RET_ON_ERR(service.channel_write(resp, resp_data) == 0,
421423
"Failed to write response back");
422424
}
423425
out:
424-
m_service.close();
426+
service.close_service();
425427
if (proxy != NULL) {
426428
delete proxy;
427429
}
@@ -434,16 +436,3 @@ run_eventd_service()
434436
SWSS_LOG_ERROR("Eventd service exiting\n");
435437
}
436438

437-
438-
int main()
439-
{
440-
SWSS_LOG_INFO("The eventd service started");
441-
442-
run_eventd_service();
443-
444-
SWSS_LOG_INFO("The eventd service exited");
445-
446-
return 0;
447-
}
448-
449-

src/sonic-eventd/src/eventd.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ typedef enum {
9090
} capture_control_t;
9191

9292

93-
int capture_status;
94-
9593
class capture_service
9694
{
9795
public:

src/sonic-eventd/src/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
void run_eventd_service();
3+
4+
int main()
5+
{
6+
SWSS_LOG_INFO("The eventd service started");
7+
8+
run_eventd_service();
9+
10+
SWSS_LOG_INFO("The eventd service exited");
11+
12+
return 0;
13+
}
14+

src/sonic-eventd/src/subdir.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
CC := g++
22

3-
OBJS += ./src/eventd.o
3+
TEST_OBJS += ./src/eventd.o
4+
OBJS += ./src/eventd.o ./src/main.o
45

5-
C_DEPS += ./src/eventd.d
6+
C_DEPS += ./src/eventd.d ./src/main.d
67

78
src/%.o: src/%.cpp
89
@echo 'Building file: $<'
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include <iostream>
2+
#include <memory>
3+
#include <thread>
4+
#include <algorithm>
5+
#include <deque>
6+
#include <regex>
7+
#include <chrono>
8+
#include "gtest/gtest.h"
9+
#include "events_common.h"
10+
#include "events.h"
11+
#include "../src/eventd.h"
12+
13+
using namespace std;
14+
15+
typedef vector<internal_event_t> lst_events_t;
16+
17+
void run_sub(void *zctx, bool &term, string &read_source, lst_events_t &lst)
18+
{
19+
void *mock_sub = zmq_socket (zctx, ZMQ_SUB);
20+
string source;
21+
internal_event_t ev_int;
22+
int block_ms = 200;
23+
24+
EXPECT_TRUE(NULL != mock_sub);
25+
EXPECT_EQ(0, zmq_connect(mock_sub, get_config(XPUB_END_KEY).c_str()));
26+
EXPECT_EQ(0, zmq_setsockopt(mock_sub, ZMQ_SUBSCRIBE, "", 0));
27+
EXPECT_EQ(0, zmq_setsockopt(mock_sub, ZMQ_RCVTIMEO, &block_ms, sizeof (block_ms)));
28+
29+
while(!term) {
30+
if (0 == zmq_message_read(mock_sub, 0, source, ev_int)) {
31+
lst.push_back(ev_int);
32+
read_source.swap(source);
33+
}
34+
}
35+
36+
zmq_close(mock_sub);
37+
}
38+
39+
void *init_pub(void *zctx)
40+
{
41+
void *mock_pub = zmq_socket (zctx, ZMQ_PUB);
42+
EXPECT_TRUE(NULL != mock_pub);
43+
EXPECT_EQ(0, zmq_connect(mock_pub, get_config(XSUB_END_KEY).c_str()));
44+
45+
return mock_pub;
46+
}
47+
48+
void run_pub(void *mock_pub, const string wr_source, lst_events_t &lst)
49+
{
50+
for(lst_events_t::const_iterator itc = lst.begin(); itc != lst.end(); ++itc) {
51+
EXPECT_EQ(0, zmq_message_send(mock_pub, wr_source, *itc));
52+
}
53+
}
54+
55+
56+
static internal_event_t
57+
create_ev(const string rid, sequence_t n, const string d)
58+
{
59+
stringstream ss;
60+
61+
ss << d << ":" << n;
62+
63+
return internal_event_t({ {EVENT_STR_DATA, ss.str()},
64+
{ EVENT_RUNTIME_ID, rid }, { EVENT_SEQUENCE, seq_to_str(n) }});
65+
}
66+
67+
68+
69+
TEST(eventd, proxy)
70+
{
71+
printf("TEST started\n");
72+
bool term_sub = false;
73+
string rd_source, wr_source("hello");
74+
lst_events_t rd_evts, wr_evts;
75+
76+
void *zctx = zmq_ctx_new();
77+
EXPECT_TRUE(NULL != zctx);
78+
79+
eventd_proxy *pxy = new eventd_proxy(zctx);
80+
EXPECT_TRUE(NULL != pxy);
81+
82+
/* Starting proxy */
83+
EXPECT_EQ(0, pxy->init());
84+
85+
/* subscriber in a thread */
86+
thread thr(&run_sub, zctx, ref(term_sub), ref(rd_source), ref(rd_evts));
87+
88+
/* Init pub connection */
89+
void *mock_pub = init_pub(zctx);
90+
91+
/* Provide time for async connect to complete */
92+
this_thread::sleep_for(chrono::milliseconds(100));
93+
94+
for(int i=0; i<5; ++i) {
95+
wr_evts.push_back(create_ev("hello", i, "test body"));
96+
}
97+
98+
EXPECT_TRUE(rd_evts.empty());
99+
EXPECT_TRUE(rd_source.empty());
100+
101+
/* Publish events. */
102+
run_pub(mock_pub, wr_source, wr_evts);
103+
104+
while(rd_evts.size() != wr_evts.size()) {
105+
printf("rd_evts.size != wr_evts.size %d != %d\n",
106+
(int)rd_evts.size(), (int)wr_evts.size());
107+
this_thread::sleep_for(chrono::milliseconds(10));
108+
}
109+
110+
term_sub = true;
111+
printf("Waiting for sub thread to join...\n");
112+
113+
thr.join();
114+
zmq_close(mock_pub);
115+
zmq_ctx_term(zctx);
116+
}
117+

src/sonic-eventd/tests/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "gtest/gtest.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
int main(int argc, char* argv[])
7+
{
8+
testing::InitGoogleTest(&argc, argv);
9+
return RUN_ALL_TESTS();
10+
}

src/sonic-eventd/tests/subdir.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CC := g++
2+
3+
TEST_OBJS += ./tests/eventd_ut.o ./tests/main.o
4+
5+
C_DEPS += ./tests/eventd_ut.d ./tests/main.d
6+
7+
tests/%.o: tests/%.cpp
8+
@echo 'Building file: $<'
9+
@echo 'Invoking: GCC C++ Compiler'
10+
$(CC) -D__FILENAME__="$(subst src/,,$<)" $(CFLAGS) -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
11+
@echo 'Finished building: $<'
12+
@echo ' '

0 commit comments

Comments
 (0)