Skip to content

Commit

Permalink
Merge pull request #195 from syvex/RemoteSyslogChannelReset
Browse files Browse the repository at this point in the history
Allow RemoteSyslogChannel to be used again after being closed.
  • Loading branch information
aleks-f committed Jun 18, 2013
2 parents 36025cc + 7f17255 commit 8961c4c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Net/src/RemoteSyslogChannel.cpp
Expand Up @@ -88,7 +88,10 @@ RemoteSyslogChannel::~RemoteSyslogChannel()
void RemoteSyslogChannel::open()
{
if (_open) return;


// reset socket for the case that it has been previously closed
_socket = DatagramSocket();

if (_logHost.find(':') != std::string::npos)
_socketAddress = SocketAddress(_logHost);
else
Expand All @@ -105,6 +108,8 @@ void RemoteSyslogChannel::open()
_host = _socket.address().host().toString();
}
}

_open = true;
}


Expand Down
40 changes: 40 additions & 0 deletions Net/testsuite/src/SyslogTest.cpp
Expand Up @@ -167,6 +167,45 @@ void SyslogTest::testListener()
}


void SyslogTest::testChannelOpenClose()
{
Poco::AutoPtr<RemoteSyslogChannel> channel = new RemoteSyslogChannel();
channel->setProperty("loghost", "localhost:51400");
channel->open();
Poco::AutoPtr<RemoteSyslogListener> listener = new RemoteSyslogListener(51400);
listener->open();
CachingChannel cl;
listener->addChannel(&cl);

assert (cl.getCurrentSize() == 0);
Poco::Message msg1("source1", "message1", Poco::Message::PRIO_CRITICAL);
channel->log(msg1);
Poco::Thread::sleep(1000);
assert (cl.getCurrentSize() == 1);

channel->close(); // close and re-open channel
channel->open();

Poco::Message msg2("source2", "message2", Poco::Message::PRIO_ERROR);
channel->log(msg2);
Poco::Thread::sleep(1000);
assert (cl.getCurrentSize() == 2);

listener->close();
std::vector<Poco::Message> msgs;
cl.getMessages(msgs, 0, 10);
assert (msgs.size() == 2);

assert (msgs[1].getSource() == "source1");
assert (msgs[1].getText() == "message1");
assert (msgs[1].getPriority() == Poco::Message::PRIO_CRITICAL);

assert (msgs[0].getSource() == "source2");
assert (msgs[0].getText() == "message2");
assert (msgs[0].getPriority() == Poco::Message::PRIO_ERROR);
}


void SyslogTest::testOldBSD()
{
Poco::AutoPtr<RemoteSyslogChannel> channel = new RemoteSyslogChannel();
Expand Down Expand Up @@ -209,6 +248,7 @@ CppUnit::Test* SyslogTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SyslogTest");

CppUnit_addTest(pSuite, SyslogTest, testListener);
CppUnit_addTest(pSuite, SyslogTest, testChannelOpenClose);
CppUnit_addTest(pSuite, SyslogTest, testOldBSD);

return pSuite;
Expand Down
1 change: 1 addition & 0 deletions Net/testsuite/src/SyslogTest.h
Expand Up @@ -47,6 +47,7 @@ class SyslogTest: public CppUnit::TestCase
~SyslogTest();

void testListener();
void testChannelOpenClose();
void testOldBSD();

void setUp();
Expand Down

0 comments on commit 8961c4c

Please sign in to comment.