From e81ba73c71b2b12689ab983d21dfbf2f7eb24d8c Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Tue, 19 Apr 2016 14:40:57 -0700 Subject: [PATCH 1/2] Add NotificationConsumer/Producer unittest --- tests/redis_ut.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/redis_ut.cpp b/tests/redis_ut.cpp index c937366e7..95da0b271 100644 --- a/tests/redis_ut.cpp +++ b/tests/redis_ut.cpp @@ -1,6 +1,8 @@ #include "common/dbconnector.h" #include "common/producertable.h" #include "common/consumertable.h" +#include "common/notificationconsumer.h" +#include "common/notificationproducer.h" #include "common/select.h" #include "common/selectableevent.h" #include @@ -222,6 +224,73 @@ TEST(DBConnector, multitable) cout << endl << "Done." << endl; } + +void notificationProducer() +{ + sleep(1); + + DBConnector db(TEST_VIEW, "localhost", 6379, 0); + + swss::NotificationProducer np(&db, "fooChannel"); + + std::vector values; + + swss::FieldValueTuple e("foo", "bar"); + + values.push_back(e); + + std::cout << "sending ntf" << std::endl; + + np.send("a", "b", values); +} + +TEST(DBConnector, notifications) +{ + clearDB(); + + DBConnector db(TEST_VIEW, "localhost", 6379, 0); + + swss::NotificationConsumer nc(&db, "fooChannel"); + + std::thread np(notificationProducer); + + swss::Select s; + + s.addSelectable(&nc); + + swss::Selectable *sel; + + int fd; + + int value = 1; + + int result = s.select(&sel, &fd, 2000); + + if (result == swss::Select::OBJECT) + { + std::cout << "Got notification " << std::endl; + + value = 2; + + std::string op, data; + std::vector values; + + nc.pop(op, data, values); + + EXPECT_EQ(op, "a"); + EXPECT_EQ(data, "b"); + + auto v = values.at(0); + + EXPECT_EQ(fvField(v), "foo"); + EXPECT_EQ(fvValue(v), "bar"); + } + + np.join(); + + EXPECT_EQ(value, 2); +} + void selectableEventThread(swss::Selectable *ev, int *value) { swss::Select s; @@ -264,3 +333,4 @@ TEST(DBConnector, selectableevent) EXPECT_EQ(value, 2); } + From 62ecbc4f5a7934d66556e006040743fa6c0f39d1 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Wed, 20 Apr 2016 11:42:31 -0700 Subject: [PATCH 2/2] Allow producer to pass arbitrary DEL command --- common/consumertable.cpp | 7 ++++++- common/producertable.cpp | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/common/consumertable.cpp b/common/consumertable.cpp index 529095a12..30a88f51d 100644 --- a/common/consumertable.cpp +++ b/common/consumertable.cpp @@ -81,6 +81,11 @@ void ConsumerTable::pop(KeyOpFieldsValuesTuple &kco) vector fieldsValues; string key = pop_front(m_results); string op = pop_front(m_results); + + string dbop = op.at(0) == 'D' ? DEL_COMMAND : SET_COMMAND; + + op = op.substr(1); + JSon::readJson(pop_front(m_results), fieldsValues); kco = std::make_tuple(key, op, fieldsValues); @@ -93,7 +98,7 @@ void ConsumerTable::pop(KeyOpFieldsValuesTuple &kco) multi(); - if (op == DEL_COMMAND) + if (dbop == DEL_COMMAND) { std::string del("DEL "); diff --git a/common/producertable.cpp b/common/producertable.cpp index 543c5e422..3af7114a1 100644 --- a/common/producertable.cpp +++ b/common/producertable.cpp @@ -46,7 +46,7 @@ void ProducerTable::set(string key, vector &values, string op) { multi(); - enqueueDbChange(key, JSon::buildJson(values), op); + enqueueDbChange(key, JSon::buildJson(values), "S" + op); exec(); } @@ -57,7 +57,7 @@ void ProducerTable::del(string key, string op) multi(); - enqueueDbChange(key, "{}", op); + enqueueDbChange(key, "{}", "D" + op); exec(); }