diff options
| author | Remko Tronçon <git@el-tramo.be> | 2009-11-20 18:23:09 (GMT) |
|---|---|---|
| committer | Remko Tronçon <git@el-tramo.be> | 2009-11-20 18:23:09 (GMT) |
| commit | cf8e2aca04c9a4021448f829e5b264dad25b28c8 (patch) | |
| tree | 6f11cb1a32a9d73f3b9a62db6d06e6ae2dcab6ab | |
| parent | 97bdf1a66d321bcd059a0ee218bec0cc92d1bc7b (diff) | |
| download | swift-cf8e2aca04c9a4021448f829e5b264dad25b28c8.zip swift-cf8e2aca04c9a4021448f829e5b264dad25b28c8.tar.bz2 | |
Fixed connection leak.
| -rw-r--r-- | Swiften/Network/BoostConnectionServer.cpp | 1 | ||||
| -rw-r--r-- | Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp | 3 | ||||
| -rw-r--r-- | Swiften/QA/NetworkTest/BoostConnectionTest.cpp | 29 |
3 files changed, 33 insertions, 0 deletions
diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp index 4e83ad5..cea016d 100644 --- a/Swiften/Network/BoostConnectionServer.cpp +++ b/Swiften/Network/BoostConnectionServer.cpp @@ -1,67 +1,68 @@ #include "Swiften/Network/BoostConnectionServer.h" #include <boost/bind.hpp> #include <boost/system/system_error.hpp> #include "Swiften/EventLoop/MainEventLoop.h" namespace Swift { BoostConnectionServer::BoostConnectionServer(int port, boost::asio::io_service* ioService) : port_(port), ioService_(ioService), acceptor_(NULL) { } void BoostConnectionServer::start() { try { assert(!acceptor_); acceptor_ = new boost::asio::ip::tcp::acceptor( *ioService_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port_)); acceptNextConnection(); } catch (const boost::system::system_error& e) { if (e.code() == boost::asio::error::address_in_use) { MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), Conflict), shared_from_this()); } else { MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), UnknownError), shared_from_this()); } } } void BoostConnectionServer::stop() { stop(boost::optional<Error>()); } void BoostConnectionServer::stop(boost::optional<Error> e) { if (acceptor_) { acceptor_->close(); + delete acceptor_; acceptor_ = NULL; } MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), e), shared_from_this()); } void BoostConnectionServer::acceptNextConnection() { boost::shared_ptr<BoostConnection> newConnection(new BoostConnection(&acceptor_->io_service())); acceptor_->async_accept(newConnection->getSocket(), boost::bind(&BoostConnectionServer::handleAccept, shared_from_this(), newConnection, boost::asio::placeholders::error)); } void BoostConnectionServer::handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) { if (error) { MainEventLoop::postEvent( boost::bind( &BoostConnectionServer::stop, shared_from_this(), UnknownError), shared_from_this()); } else { MainEventLoop::postEvent( boost::bind(boost::ref(onNewConnection), newConnection), shared_from_this()); newConnection->listen(); acceptNextConnection(); } } } diff --git a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp index a5c51aa..926c9ae 100644 --- a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp +++ b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp @@ -1,72 +1,75 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include <boost/shared_ptr.hpp> #include "Swiften/Base/String.h" #include "Swiften/Network/BoostConnectionServer.h" #include "Swiften/Network/BoostIOServiceThread.h" #include "Swiften/EventLoop/DummyEventLoop.h" using namespace Swift; class BoostConnectionServerTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(BoostConnectionServerTest); CPPUNIT_TEST(testConstructor_TwoServersOnSamePort); CPPUNIT_TEST(testStart_Conflict); CPPUNIT_TEST(testStop); CPPUNIT_TEST_SUITE_END(); public: void setUp() { boostIOServiceThread_ = new BoostIOServiceThread(); eventLoop_ = new DummyEventLoop(); stopped = false; stoppedError.reset(); } void tearDown() { + while (eventLoop_->hasEvents()) { + eventLoop_->processEvents(); + } delete eventLoop_; delete boostIOServiceThread_; } void testConstructor_TwoServersOnSamePort() { boost::shared_ptr<BoostConnectionServer> testling(new BoostConnectionServer(9999, &boostIOServiceThread_->getIOService())); boost::shared_ptr<BoostConnectionServer> testling2(new BoostConnectionServer(9999, &boostIOServiceThread_->getIOService())); } void testStart_Conflict() { boost::shared_ptr<BoostConnectionServer> testling(new BoostConnectionServer(9999, &boostIOServiceThread_->getIOService())); testling->start(); boost::shared_ptr<BoostConnectionServer> testling2(new BoostConnectionServer(9999, &boostIOServiceThread_->getIOService())); testling2->onStopped.connect( boost::bind(&BoostConnectionServerTest::handleStopped, this, _1)); testling->stop(); } void testStop() { boost::shared_ptr<BoostConnectionServer> testling(new BoostConnectionServer(9999, &boostIOServiceThread_->getIOService())); testling->start(); testling->stop(); boost::shared_ptr<BoostConnectionServer> testling2(new BoostConnectionServer(9999, &boostIOServiceThread_->getIOService())); testling2->start(); testling2->stop(); } void handleStopped(boost::optional<BoostConnectionServer::Error> e) { stopped = true; stoppedError = e; } private: BoostIOServiceThread* boostIOServiceThread_; DummyEventLoop* eventLoop_; bool stopped; boost::optional<BoostConnectionServer::Error> stoppedError; }; CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionServerTest); diff --git a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp index 9929eaa..af4e003 100644 --- a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp +++ b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp @@ -1,59 +1,88 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include <boost/shared_ptr.hpp> #include "Swiften/Base/String.h" #include "Swiften/Base/sleep.h" #include "Swiften/Network/BoostConnection.h" #include "Swiften/Network/HostAddress.h" #include "Swiften/Network/HostAddressPort.h" #include "Swiften/Network/BoostIOServiceThread.h" #include "Swiften/EventLoop/DummyEventLoop.h" const unsigned char* address = reinterpret_cast<const unsigned char*>("\x41\x63\xde\x89"); using namespace Swift; class BoostConnectionTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(BoostConnectionTest); CPPUNIT_TEST(testDestructor); CPPUNIT_TEST(testDestructor_PendingEvents); + CPPUNIT_TEST(testWrite); CPPUNIT_TEST_SUITE_END(); public: BoostConnectionTest() {} void setUp() { boostIOServiceThread_ = new BoostIOServiceThread(); eventLoop_ = new DummyEventLoop(); + disconnected = false; } void tearDown() { delete eventLoop_; delete boostIOServiceThread_; } void testDestructor() { { boost::shared_ptr<BoostConnection> testling(new BoostConnection(&boostIOServiceThread_->getIOService())); testling->connect(HostAddressPort(HostAddress(address, 4), 5222)); } } void testDestructor_PendingEvents() { { boost::shared_ptr<BoostConnection> testling(new BoostConnection(&boostIOServiceThread_->getIOService())); testling->connect(HostAddressPort(HostAddress(address, 4), 5222)); while (!eventLoop_->hasEvents()) { Swift::sleep(10); } } eventLoop_->processEvents(); } + void testWrite() { + boost::shared_ptr<BoostConnection> testling(new BoostConnection(&boostIOServiceThread_->getIOService())); + testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get())); + testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); + testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); + testling->connect(HostAddressPort(HostAddress("65.99.222.137"), 5222)); + while (receivedData.isEmpty()) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + testling->disconnect(); + } + + void doWrite(BoostConnection* connection) { + connection->write(ByteArray("<stream:stream>")); + } + + void handleDataRead(const ByteArray& data) { + receivedData += data; + } + + void handleDisconnected() { + disconnected = true; + } + private: BoostIOServiceThread* boostIOServiceThread_; DummyEventLoop* eventLoop_; + ByteArray receivedData; + bool disconnected; }; CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionTest); |
Swift