From 7db013780b5318cb383c49caef4f77d39d440ea9 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 14 Jul 2016 00:27:06 +0200 Subject: Ping the server first to make sure it's available. This significantely reduces the time required to execute the tests. --- examples/imapresource/imapserverproxy.cpp | 46 +++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'examples/imapresource/imapserverproxy.cpp') diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 510ca3e..b1ba5e7 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include #include #include #include @@ -36,6 +38,7 @@ #include #include "log.h" +#include "test.h" SINK_DEBUG_AREA("imapserverproxy") @@ -97,7 +100,45 @@ class SessionUiProxy : public KIMAP::SessionUiProxy { ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port) : mSession(new KIMAP::Session(serverUrl, qint16(port))) { mSession->setUiProxy(SessionUiProxy::Ptr(new SessionUiProxy)); - mSession->setTimeout(10); + if (Sink::Test::testModeEnabled()) { + mSession->setTimeout(1); + } else { + mSession->setTimeout(10); + } +} + +KAsync::Job ImapServerProxy::ping() +{ + auto hostname = mSession->hostName(); + auto port = mSession->port(); + auto timeout = mSession->timeout() * 1000; + return KAsync::start([=](KAsync::Future &future) { + SinkTrace() << "Starting ping" << timeout; + auto guard = QPointer(new QObject); + auto socket = QSharedPointer::create(); + QObject::connect(socket.data(), &QTcpSocket::hostFound, guard, [guard, &future, socket]() { + SinkTrace() << "Ping succeeded"; + delete guard; + future.setFinished(); + }); + QObject::connect(socket.data(), static_cast(&QTcpSocket::error), guard, [guard, &future, socket](QTcpSocket::SocketError) { + SinkWarning() << "Ping failed: "; + delete guard; + future.setError(1, "Error during connect"); + }); + if (!guard) { + return; + } + socket->connectToHost(hostname, port); + QTimer::singleShot(timeout, guard, [guard, &future, hostname, port, socket]() { + if (guard) { + SinkWarning() << "Ping failed: " << hostname << port; + delete guard; + future.setError(1, "Couldn't lookup host"); + } + }); + }); + } KAsync::Job ImapServerProxy::login(const QString &username, const QString &password) @@ -118,7 +159,8 @@ KAsync::Job ImapServerProxy::login(const QString &username, const QString }); auto namespaceJob = new KIMAP::NamespaceJob(mSession); - return runJob(loginJob).then(runJob(capabilitiesJob)).then([this](){ + //FIXME The ping is only required because the login job doesn't fail after the configured timeout + return ping().then(runJob(loginJob)).then(runJob(capabilitiesJob)).then([this](){ SinkTrace() << "Supported capabilities: " << mCapabilities; QStringList requiredExtensions = QStringList() << "UIDPLUS" << "NAMESPACE"; for (const auto &requiredExtension : requiredExtensions) { -- cgit v1.2.3