Skip to content

Commit f2694ea

Browse files
common ut passes
1 parent d28399e commit f2694ea

File tree

6 files changed

+65
-21
lines changed

6 files changed

+65
-21
lines changed

common/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ libswsscommon_la_SOURCES = \
7272

7373
libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)
7474
libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS) $(CODE_COVERAGE_CPPFLAGS)
75-
libswsscommon_la_LIBADD = -lpthread $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -lboost_serialization
75+
libswsscommon_la_LIBADD = -lpthread $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -lboost_serialization -luuid -lboost_serialization
7676

7777
swssloglevel_SOURCES = loglevel.cpp
7878

common/events_common.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "events_common.h"
22

33
int zerrno = 0;
4+
int running_ut = 0;
45

56
/*
67
* defaults for all config entries

common/events_common.h

+21-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <stdio.h>
77
#include <chrono>
88
#include <fstream>
9-
#include <thread>
109
#include <errno.h>
1110
#include <cxxabi.h>
1211
#include "string.h"
@@ -35,6 +34,8 @@ extern int zerrno;
3534
*/
3635
#define MAX_PUBLISHERS_COUNT 1000
3736

37+
extern int running_ut;
38+
3839

3940
/* TODO: Combine two SWSS_LOG_ERROR into one */
4041
#define RET_ON_ERR(res, msg, ...)\
@@ -43,6 +44,9 @@ extern int zerrno;
4344
zerrno = zmq_errno(); \
4445
SWSS_LOG_ERROR(msg, ##__VA_ARGS__); \
4546
SWSS_LOG_ERROR("last:errno=%d zerr=%d", _e, zerrno); \
47+
if (running_ut) { \
48+
printf(msg, ##__VA_ARGS__); \
49+
printf("last:errno=%d zerr=%d\n", _e, zerrno); }\
4650
goto out; }
4751

4852
#define ERR_CHECK(res, ...) {\
@@ -243,9 +247,10 @@ zmsg_to_map(zmq_msg_t &msg, Map& data)
243247
* First part only has the event source, so receivers could
244248
* filter by source.
245249
*
246-
* Second part contains map as defined in internal_event_t.
247-
* The map is serialized and sent as string.
248-
*
250+
* Second part contains serialized form of map as defined in internal_event_t.
251+
* The map is serialized and sent as string events_data_type_t.
252+
* Caching that handles of set of events, handleas ordered events
253+
* as declared in events_data_lst_t.
249254
*/
250255
/*
251256
* This is data going over wire and using cache. So be conservative
@@ -261,10 +266,12 @@ typedef map<string, string> internal_event_t;
261266
typedef uint32_t sequence_t;
262267
typedef string runtime_id_t;
263268

264-
internal_event_t internal_event_ref = {
265-
{ EVENT_STR_DATA, "" },
266-
{ EVENT_RUNTIME_ID, "" },
267-
{ EVENT_SEQUENCE, "" } };
269+
/*
270+
* internal_event_t internal_event_ref = {
271+
* { EVENT_STR_DATA, "" },
272+
* { EVENT_RUNTIME_ID, "" },
273+
* { EVENT_SEQUENCE, "" } };
274+
*/
268275

269276
/* ZMQ message part 2 contains serialized version of internal_event_t */
270277
typedef string events_data_type_t;
@@ -273,7 +280,7 @@ typedef vector<events_data_type_t> events_data_lst_t;
273280

274281
template<typename DT>
275282
int
276-
zmq_read_part(void *sock, int flag, int &more, DT data)
283+
zmq_read_part(void *sock, int flag, int &more, DT &data)
277284
{
278285
zmq_msg_t msg;
279286

@@ -298,15 +305,15 @@ zmq_read_part(void *sock, int flag, int &more, DT data)
298305

299306
template<typename DT>
300307
int
301-
zmq_send_part(void *sock, int flag, DT data)
308+
zmq_send_part(void *sock, int flag, DT &data)
302309
{
303310
zmq_msg_t msg;
304311

305312
int rc = map_to_zmsg(data, msg);
306-
RET_ON_ERR(rc == 0, "Failed to map to zmsg %d", 5);
313+
RET_ON_ERR(rc == 0, "Failed to map to zmsg %d", rc);
307314

308315
rc = zmq_msg_send (&msg, sock, flag);
309-
RET_ON_ERR(rc != -1, "Failed to send part %d", 5);
316+
RET_ON_ERR(rc != -1, "Failed to send part %d", rc);
310317

311318
rc = 0;
312319
out:
@@ -316,7 +323,7 @@ zmq_send_part(void *sock, int flag, DT data)
316323

317324
template<typename P1, typename P2>
318325
int
319-
zmq_message_send(void *sock, P1 pt1, P2 pt2)
326+
zmq_message_send(void *sock, P1 &pt1, P2 &pt2)
320327
{
321328
int rc = zmq_send_part(sock, pt2.empty() ? 0 : ZMQ_SNDMORE, pt1);
322329

@@ -330,7 +337,7 @@ zmq_message_send(void *sock, P1 pt1, P2 pt2)
330337

331338
template<typename P1, typename P2>
332339
int
333-
zmq_message_read(void *sock, int flag, P1 pt1, P2 pt2)
340+
zmq_message_read(void *sock, int flag, P1 &pt1, P2 &pt2)
334341
{
335342
int more = 0, rc, rc1;
336343

common/events_pi.h

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <chrono>
88
#include <ctime>
99
#include <fstream>
10-
#include <thread>
1110
#include <uuid/uuid.h>
1211
#include "string.h"
1312
#include "json.hpp"

tests/Makefile.am

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ tests_SOURCES = redis_ut.cpp \
3636
status_code_util_test.cpp \
3737
saiaclschema_ut.cpp \
3838
main.cpp \
39-
events_common_ut.cpp
39+
events_common_ut.cpp \
40+
events_service_ut.cpp
4041

4142
tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS)
4243
tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS)
43-
tests_LDADD = $(LDADD_GTEST) -lpthread -L$(top_srcdir)/common -lswsscommon $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS)
44+
tests_LDADD = $(LDADD_GTEST) -lpthread -L$(top_srcdir)/common -lswsscommon $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -luuid -lboost_serialization

tests/events_common_ut.cpp

+39-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ using namespace std;
1212
const char *test_cfg_data = "{\
1313
\"events\" : { \
1414
\"xsub_path\": \"xsub_path\", \
15-
\"pair_path\": \"pair_path\" \
15+
\"req_rep_path\": \"req_rep_path\" \
1616
}\
1717
}";
1818

1919

2020
TEST(events_common, get_config)
2121
{
2222
EXPECT_EQ(string("tcp://127.0.0.1:5570"), get_config(string(XSUB_END_KEY)));
23-
EXPECT_EQ(string("tcp://127.0.0.1:5573"), get_config(string(PAIR_END_KEY)));
23+
EXPECT_EQ(string("tcp://127.0.0.1:5572"), get_config(string(REQ_REP_END_KEY)));
2424
EXPECT_EQ(string("5"), get_config(string(STATS_UPD_SECS)));
2525

2626
ofstream tfile;
@@ -32,9 +32,11 @@ TEST(events_common, get_config)
3232
read_init_config(tfile_name);
3333

3434
EXPECT_EQ(string(XSUB_END_KEY), get_config(string(XSUB_END_KEY)));
35-
EXPECT_EQ(string(PAIR_END_KEY), get_config(string(PAIR_END_KEY)));
35+
EXPECT_EQ(string(REQ_REP_END_KEY), get_config(string(REQ_REP_END_KEY)));
3636
EXPECT_EQ(string("5"), get_config(string(STATS_UPD_SECS)));
3737

38+
EXPECT_EQ(100, get_config_data(CACHE_MAX_CNT, 100));
39+
3840
cout << "events_common: get_config succeeded\n";
3941
}
4042

@@ -68,3 +70,37 @@ TEST(events_common, msg)
6870
EXPECT_EQ(t, t1);
6971
}
7072

73+
TEST(events_common, send_recv)
74+
{
75+
running_ut = 1;
76+
77+
char *path = "tcp://127.0.0.1:5570";
78+
void *zmq_ctx = zmq_ctx_new();
79+
void *sock_p0 = zmq_socket (zmq_ctx, ZMQ_PAIR);
80+
EXPECT_EQ(0, zmq_connect (sock_p0, path));
81+
82+
void *sock_p1 = zmq_socket (zmq_ctx, ZMQ_PAIR);
83+
EXPECT_EQ(0, zmq_bind (sock_p1, path));
84+
85+
string source("Hello"), source1;
86+
87+
map<string, string> m = {{"foo", "bar"}, {"hello", "world"}, {"good", "day"}};
88+
map<string, string> m1;
89+
90+
EXPECT_EQ(0, zmq_message_send(sock_p0, source, m));
91+
92+
EXPECT_EQ(0, zmq_message_read(sock_p1, 0, source1, m1));
93+
94+
EXPECT_EQ(source, source1);
95+
EXPECT_EQ(m, m1);
96+
zmq_close(sock_p0);
97+
zmq_close(sock_p1);
98+
zmq_ctx_term(zmq_ctx);
99+
}
100+
101+
102+
103+
104+
105+
106+

0 commit comments

Comments
 (0)