|
| 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 | + |
0 commit comments