Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ libswsscommon_la_SOURCES = \
json.cpp \
producertable.cpp \
select.cpp \
selectableevent.cpp \
consumertable.cpp \
ipaddress.cpp \
ipprefix.cpp \
Expand Down
75 changes: 75 additions & 0 deletions common/selectableevent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <system_error>

#include "common/logger.h"
#include "common/selectableevent.h"

namespace swss {

SelectableEvent::SelectableEvent() :
m_efd(0)
{
m_efd = eventfd(0, 0);

if (m_efd == -1)
{
SWSS_LOG_ERROR("failed to create eventfd, errno: %s", strerror(errno));

throw std::runtime_error("failed to create eventfd");
}
}

SelectableEvent::~SelectableEvent()
{
close(m_efd);
}

void SelectableEvent::addFd(fd_set *fd)
{
FD_SET(m_efd, fd);
}

int SelectableEvent::readCache()
{
return Selectable::NODATA;
}

void SelectableEvent::readMe()
{
uint64_t r;
ssize_t s = read(m_efd, &r, sizeof(uint64_t));

if (s != sizeof(uint64_t))
{
SWSS_LOG_ERROR("read failed, errno: %s", strerror(errno));

throw std::runtime_error("read failed");
}
}

bool SelectableEvent::isMe(fd_set *fd)
{
return FD_ISSET(m_efd, fd);
}

void SelectableEvent::notify()
{
SWSS_LOG_ENTER();

uint64_t value = 1;
ssize_t s = write(m_efd, &value, sizeof(uint64_t));

if (s != sizeof(uint64_t))
{
SWSS_LOG_ERROR("write failed, errno: %s", strerror(errno));

throw std::runtime_error("write failed");
}
}

}
31 changes: 31 additions & 0 deletions common/selectableevent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef __SELECTABLEEVENT__
#define __SELECTABLEEVENT__

#include <string>
#include <vector>
#include <limits>
#include <sys/eventfd.h>
#include "selectable.h"

namespace swss {

class SelectableEvent : public Selectable
{
public:
SelectableEvent();
virtual ~SelectableEvent();

void notify();

virtual void addFd(fd_set *fd);
virtual bool isMe(fd_set *fd);
virtual int readCache();
virtual void readMe();

private:
int m_efd;
};

}

#endif // __SELECTABLEEVENT__
4 changes: 2 additions & 2 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ tests_SOURCES = redis_ut.cpp

tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST)
tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST)
tests_LDADD = -lnl-genl-3 -lhiredis -lhiredis -lpthread \
-L$(top_srcdir)/common -lswsscommon $(LDADD_GTEST)
tests_LDADD = $(LDADD_GTEST) -lnl-genl-3 -lhiredis -lhiredis -lpthread \
-L$(top_srcdir)/common -lswsscommon

43 changes: 43 additions & 0 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "common/producertable.h"
#include "common/consumertable.h"
#include "common/select.h"
#include "common/selectableevent.h"
#include <iostream>
#include <memory>
#include <thread>
Expand Down Expand Up @@ -221,3 +222,45 @@ TEST(DBConnector, multitable)
cout << endl << "Done." << endl;
}

void selectableEventThread(swss::Selectable *ev, int *value)
{
swss::Select s;

s.addSelectable(ev);

swss::Selectable *sel;

int fd;

std::cout << "listening ... " << std::endl;

int result = s.select(&sel, &fd, 2000);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still have the timeout? I thought this selectevent can get rid of timeout which is the purpose?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i put timeout explicitly that if something goes wrong, it will not set value, and test will fails, since there is thread.join() and it will block unittest. Purpose of this test is to see if event notification is working correctly and it is.


if (result == swss::Select::OBJECT)
{
if (sel == ev)
{
std::cout << "Got notification: "<<std::endl;
*value = 2;
}
}
}

TEST(DBConnector, selectableevent)
{
int value = 1;

swss::SelectableEvent ev;

std::thread t(selectableEventThread, &ev, &value);

sleep(1);

EXPECT_EQ(value, 1);

ev.notify();

t.join();

EXPECT_EQ(value, 2);
}