Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SocketConnector test #2875

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions Net/include/Poco/Net/SocketConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Observer.h"
#include "Poco/Mutex.h"


namespace Poco {
Expand Down Expand Up @@ -92,6 +93,7 @@ class SocketConnector
virtual ~SocketConnector()
/// Destroys the SocketConnector.
{
Mutex::ScopedLock lock(_mutex);
try
{
unregisterConnector();
Expand Down Expand Up @@ -134,6 +136,7 @@ class SocketConnector

void onReadable(ReadableNotification* pNotification)
{
Mutex::ScopedLock lock(_mutex);
pNotification->release();
int err = _socket.impl()->socketError();
if (err)
Expand All @@ -149,6 +152,7 @@ class SocketConnector

void onWritable(WritableNotification* pNotification)
{
Mutex::ScopedLock lock(_mutex);
pNotification->release();
onConnect();
}
Expand All @@ -162,6 +166,7 @@ class SocketConnector

void onError(ErrorNotification* pNotification)
{
Mutex::ScopedLock lock(_mutex);
pNotification->release();
onError(_socket.impl()->socketError());
unregisterConnector();
Expand Down Expand Up @@ -205,6 +210,7 @@ class SocketConnector

StreamSocket _socket;
SocketReactor* _pReactor;
Mutex _mutex;
};


Expand Down
5 changes: 3 additions & 2 deletions Net/testsuite/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ objects = \
SyslogTest \
OAuth10CredentialsTest OAuth20CredentialsTest OAuthTestSuite \
PollSetTest UDPServerTest UDPServerTestSuite \
NTLMCredentialsTest

NTLMCredentialsTest \
SocketConnectorTest

target = testrunner
target_version = 1
target_libs = PocoNet PocoFoundation CppUnit
Expand Down
2 changes: 2 additions & 0 deletions Net/testsuite/src/NetCoreTestSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "SocketAddressTest.h"
#include "DNSTest.h"
#include "NetworkInterfaceTest.h"
#include "SocketConnectorTest.h"


CppUnit::Test* NetCoreTestSuite::suite()
Expand All @@ -22,6 +23,7 @@ CppUnit::Test* NetCoreTestSuite::suite()
pSuite->addTest(IPAddressTest::suite());
pSuite->addTest(SocketAddressTest::suite());
pSuite->addTest(DNSTest::suite());
pSuite->addTest(SocketConnectorTest::suite());
#ifdef POCO_NET_HAS_INTERFACE
pSuite->addTest(NetworkInterfaceTest::suite());
#endif // POCO_NET_HAS_INTERFACE
Expand Down
163 changes: 163 additions & 0 deletions Net/testsuite/src/SocketConnectorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//
// SocketConnectorTest.cpp
//
// Copyright (c) 2005-2019, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#include "SocketConnectorTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/SocketReactor.h"
#include "Poco/Net/SocketNotification.h"
#include "Poco/Net/SocketConnector.h"
#include "Poco/Net/SocketAcceptor.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Observer.h"


using Poco::Net::SocketReactor;
using Poco::Net::SocketConnector;
using Poco::Net::SocketAcceptor;
using Poco::Net::StreamSocket;
using Poco::Net::ServerSocket;
using Poco::Net::SocketAddress;
using Poco::Net::SocketNotification;
using Poco::Net::ReadableNotification;
using Poco::Net::WritableNotification;
using Poco::Net::TimeoutNotification;
using Poco::Net::ShutdownNotification;
using Poco::Observer;


namespace
{
class EchoServiceHandler
{
public:
EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor):
_socket(socket),
_reactor(reactor)
{
_reactor.addEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
}

~EchoServiceHandler()
{
_reactor.removeEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
}

void onReadable(ReadableNotification* pNf)
{
pNf->release();
char buffer[8];
int n = _socket.receiveBytes(buffer, sizeof(buffer));
if (n > 0)
{
_socket.sendBytes(buffer, n);
}
}

private:
StreamSocket _socket;
SocketReactor& _reactor;
};

class ClientServiceHandler
{
public:
ClientServiceHandler(StreamSocket& socket, SocketReactor& reactor):
_socket(socket),
_reactor(reactor),
_or(*this, &ClientServiceHandler::onReadable),
_ow(*this, &ClientServiceHandler::onWritable)
{
_reactor.addEventHandler(_socket, _or);
_reactor.addEventHandler(_socket, _ow);

do_something();
_reactor.stop();
}

~ClientServiceHandler()
{
}

void do_something(){
Copy link
Member

Choose a reason for hiding this comment

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

naming style

volatile long i = 0;
while(true){
i++;
// if(i >= 1000000) break;
if(i >= 10000000) break;
}
}
void onReadable(ReadableNotification* pNf)
{
pNf->release();
_reactor.removeEventHandler(_socket, Observer<ClientServiceHandler, ReadableNotification>(*this, &ClientServiceHandler::onReadable));
}

void onWritable(WritableNotification* pNf)
{
pNf->release();
_reactor.removeEventHandler(_socket, Observer<ClientServiceHandler, WritableNotification>(*this, &ClientServiceHandler::onWritable));
}

StreamSocket _socket;
SocketReactor& _reactor;
Observer<ClientServiceHandler, ReadableNotification> _or;
Observer<ClientServiceHandler, WritableNotification> _ow;
};
}

SocketConnectorTest::SocketConnectorTest(const std::string& name): CppUnit::TestCase(name)
{
}


SocketConnectorTest::~SocketConnectorTest()
{
}

void SocketConnectorTest::testCallUnregisterConnectorInSameTime()
{
SocketAddress ssa;
ServerSocket ss(ssa);
SocketReactor reactor1;
SocketReactor reactor2;
SocketAcceptor<EchoServiceHandler> acceptor(ss, reactor1);
Poco::Thread th;
th.start(reactor1);
SocketAddress sa("127.0.0.1", ss.address().port());
SocketConnector<ClientServiceHandler> *connector = new SocketConnector<ClientServiceHandler>(sa, reactor2);
Poco::Thread th2;
th2.start(reactor2);
Poco::Thread::sleep(1);
delete connector;

}


void SocketConnectorTest::setUp()
{
}


void SocketConnectorTest::tearDown()
{
}


CppUnit::Test* SocketConnectorTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketConnectorTest");

CppUnit_addTest(pSuite, SocketConnectorTest, testCallUnregisterConnectorInSameTime);

return pSuite;
}
36 changes: 36 additions & 0 deletions Net/testsuite/src/SocketConnectorTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// SocketConnectorTest.h
//
// Definition of the SocketConnectorTest class.
//
// Copyright (c) 2005-2019, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#ifndef SocketConnectorTest_INCLUDED
#define SocketConnectorTest_INCLUDED


#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"


class SocketConnectorTest: public CppUnit::TestCase
{
public:
SocketConnectorTest(const std::string& name);
~SocketConnectorTest();

void testCallUnregisterConnectorInSameTime();

void setUp();
void tearDown();

static CppUnit::Test* suite();
};


#endif // SocketConnectorTest_INCLUDED