diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-14 00:27:06 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-14 00:27:34 +0200 |
commit | 7db013780b5318cb383c49caef4f77d39d440ea9 (patch) | |
tree | b090e6634fb6f4cddddec0ae5e106d8931dc4555 /examples/imapresource/imapserverproxy.cpp | |
parent | a3c3fb61e08523a563ce3cba86dc50c9c5fc2c5d (diff) | |
download | sink-7db013780b5318cb383c49caef4f77d39d440ea9.tar.gz sink-7db013780b5318cb383c49caef4f77d39d440ea9.zip |
Ping the server first to make sure it's available.
This significantely reduces the time required to execute the tests.
Diffstat (limited to 'examples/imapresource/imapserverproxy.cpp')
-rw-r--r-- | examples/imapresource/imapserverproxy.cpp | 46 |
1 files changed, 44 insertions, 2 deletions
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 @@ | |||
20 | 20 | ||
21 | #include <QDir> | 21 | #include <QDir> |
22 | #include <QFile> | 22 | #include <QFile> |
23 | #include <QTcpSocket> | ||
24 | #include <QTimer> | ||
23 | #include <KIMAP/KIMAP/LoginJob> | 25 | #include <KIMAP/KIMAP/LoginJob> |
24 | #include <kimap/namespacejob.h> | 26 | #include <kimap/namespacejob.h> |
25 | #include <KIMAP/KIMAP/SelectJob> | 27 | #include <KIMAP/KIMAP/SelectJob> |
@@ -36,6 +38,7 @@ | |||
36 | #include <KCoreAddons/KJob> | 38 | #include <KCoreAddons/KJob> |
37 | 39 | ||
38 | #include "log.h" | 40 | #include "log.h" |
41 | #include "test.h" | ||
39 | 42 | ||
40 | SINK_DEBUG_AREA("imapserverproxy") | 43 | SINK_DEBUG_AREA("imapserverproxy") |
41 | 44 | ||
@@ -97,7 +100,45 @@ class SessionUiProxy : public KIMAP::SessionUiProxy { | |||
97 | ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port) : mSession(new KIMAP::Session(serverUrl, qint16(port))) | 100 | ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port) : mSession(new KIMAP::Session(serverUrl, qint16(port))) |
98 | { | 101 | { |
99 | mSession->setUiProxy(SessionUiProxy::Ptr(new SessionUiProxy)); | 102 | mSession->setUiProxy(SessionUiProxy::Ptr(new SessionUiProxy)); |
100 | mSession->setTimeout(10); | 103 | if (Sink::Test::testModeEnabled()) { |
104 | mSession->setTimeout(1); | ||
105 | } else { | ||
106 | mSession->setTimeout(10); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | KAsync::Job<void> ImapServerProxy::ping() | ||
111 | { | ||
112 | auto hostname = mSession->hostName(); | ||
113 | auto port = mSession->port(); | ||
114 | auto timeout = mSession->timeout() * 1000; | ||
115 | return KAsync::start<void>([=](KAsync::Future<void> &future) { | ||
116 | SinkTrace() << "Starting ping" << timeout; | ||
117 | auto guard = QPointer<QObject>(new QObject); | ||
118 | auto socket = QSharedPointer<QTcpSocket>::create(); | ||
119 | QObject::connect(socket.data(), &QTcpSocket::hostFound, guard, [guard, &future, socket]() { | ||
120 | SinkTrace() << "Ping succeeded"; | ||
121 | delete guard; | ||
122 | future.setFinished(); | ||
123 | }); | ||
124 | QObject::connect(socket.data(), static_cast<void(QTcpSocket::*)(QTcpSocket::SocketError)>(&QTcpSocket::error), guard, [guard, &future, socket](QTcpSocket::SocketError) { | ||
125 | SinkWarning() << "Ping failed: "; | ||
126 | delete guard; | ||
127 | future.setError(1, "Error during connect"); | ||
128 | }); | ||
129 | if (!guard) { | ||
130 | return; | ||
131 | } | ||
132 | socket->connectToHost(hostname, port); | ||
133 | QTimer::singleShot(timeout, guard, [guard, &future, hostname, port, socket]() { | ||
134 | if (guard) { | ||
135 | SinkWarning() << "Ping failed: " << hostname << port; | ||
136 | delete guard; | ||
137 | future.setError(1, "Couldn't lookup host"); | ||
138 | } | ||
139 | }); | ||
140 | }); | ||
141 | |||
101 | } | 142 | } |
102 | 143 | ||
103 | KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password) | 144 | KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password) |
@@ -118,7 +159,8 @@ KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString | |||
118 | }); | 159 | }); |
119 | auto namespaceJob = new KIMAP::NamespaceJob(mSession); | 160 | auto namespaceJob = new KIMAP::NamespaceJob(mSession); |
120 | 161 | ||
121 | return runJob(loginJob).then(runJob(capabilitiesJob)).then<void>([this](){ | 162 | //FIXME The ping is only required because the login job doesn't fail after the configured timeout |
163 | return ping().then(runJob(loginJob)).then(runJob(capabilitiesJob)).then<void>([this](){ | ||
122 | SinkTrace() << "Supported capabilities: " << mCapabilities; | 164 | SinkTrace() << "Supported capabilities: " << mCapabilities; |
123 | QStringList requiredExtensions = QStringList() << "UIDPLUS" << "NAMESPACE"; | 165 | QStringList requiredExtensions = QStringList() << "UIDPLUS" << "NAMESPACE"; |
124 | for (const auto &requiredExtension : requiredExtensions) { | 166 | for (const auto &requiredExtension : requiredExtensions) { |