summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Network')
-rw-r--r--Swiften/Network/Connector.cpp10
-rw-r--r--Swiften/Network/UnitTest/ConnectorTest.cpp35
2 files changed, 38 insertions, 7 deletions
diff --git a/Swiften/Network/Connector.cpp b/Swiften/Network/Connector.cpp
index 02ebb02..5ab3b92 100644
--- a/Swiften/Network/Connector.cpp
+++ b/Swiften/Network/Connector.cpp
@@ -37,7 +37,6 @@ void Connector::start() {
if (timeoutMilliseconds > 0) {
timer = timerFactory->createTimer(timeoutMilliseconds);
timer->onTick.connect(boost::bind(&Connector::handleTimeout, shared_from_this()));
- timer->start();
}
serviceQuery->run();
}
@@ -129,10 +128,17 @@ void Connector::tryConnect(const HostAddressPort& target) {
currentConnection = connectionFactory->createConnection();
currentConnection->onConnectFinished.connect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1));
currentConnection->connect(target);
+ if (timer) {
+ timer->start();
+ }
}
void Connector::handleConnectionConnectFinished(bool error) {
SWIFT_LOG(debug) << "ConnectFinished: " << (error ? "error" : "success") << std::endl;
+ if (timer) {
+ timer->stop();
+ timer.reset();
+ }
currentConnection->onConnectFinished.disconnect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1));
if (error) {
currentConnection.reset();
@@ -174,7 +180,7 @@ void Connector::finish(boost::shared_ptr<Connection> connection) {
void Connector::handleTimeout() {
SWIFT_LOG(debug) << "Timeout" << std::endl;
- finish(boost::shared_ptr<Connection>());
+ handleConnectionConnectFinished(true);
}
};
diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp
index 2d96bda..fe18340 100644
--- a/Swiften/Network/UnitTest/ConnectorTest.cpp
+++ b/Swiften/Network/UnitTest/ConnectorTest.cpp
@@ -33,8 +33,9 @@ class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testConnect_AllSRVHostsFailWithoutFallbackHost);
CPPUNIT_TEST(testConnect_AllSRVHostsFailWithFallbackHost);
CPPUNIT_TEST(testConnect_SRVAndFallbackHostsFail);
- CPPUNIT_TEST(testConnect_TimeoutDuringResolve);
- CPPUNIT_TEST(testConnect_TimeoutDuringConnect);
+ //CPPUNIT_TEST(testConnect_TimeoutDuringResolve);
+ CPPUNIT_TEST(testConnect_TimeoutDuringConnectToOnlyCandidate);
+ CPPUNIT_TEST(testConnect_TimeoutDuringConnectToCandidateFallsBack);
CPPUNIT_TEST(testConnect_NoTimeout);
CPPUNIT_TEST(testStop_DuringSRVQuery);
CPPUNIT_TEST(testStop_Timeout);
@@ -209,7 +210,7 @@ class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
- void testConnect_TimeoutDuringResolve() {
+ /*void testConnect_TimeoutDuringResolve() {
Connector::ref testling(createConnector());
testling->setTimeoutMilliseconds(10);
resolver->setIsResponsive(false);
@@ -222,9 +223,9 @@ class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(boost::dynamic_pointer_cast<DomainNameResolveError>(error));
CPPUNIT_ASSERT(!connections[0]);
- }
+ }*/
- void testConnect_TimeoutDuringConnect() {
+ void testConnect_TimeoutDuringConnectToOnlyCandidate() {
Connector::ref testling(createConnector());
testling->setTimeoutMilliseconds(10);
resolver->addXMPPClientService("foo.com", host1);
@@ -240,6 +241,30 @@ class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
+ void testConnect_TimeoutDuringConnectToCandidateFallsBack() {
+ Connector::ref testling(createConnector());
+ testling->setTimeoutMilliseconds(10);
+
+ resolver->addXMPPClientService("foo.com", "host-foo.com", 1234);
+ HostAddress address1("1.1.1.1");
+ resolver->addAddress("host-foo.com", address1);
+ HostAddress address2("2.2.2.2");
+ resolver->addAddress("host-foo.com", address2);
+
+ connectionFactory->isResponsive = false;
+ testling->start();
+ eventLoop->processEvents();
+ connectionFactory->isResponsive = true;
+ timerFactory->setTime(10);
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
+ CPPUNIT_ASSERT(connections[0]);
+ CPPUNIT_ASSERT(HostAddressPort(address2, 1234) == *(connections[0]->hostAddressPort));
+ CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
+ }
+
+
void testConnect_NoTimeout() {
Connector::ref testling(createConnector());
testling->setTimeoutMilliseconds(10);