Commit e6bd47a0 authored by Volker Krause's avatar Volker Krause
Browse files

Remove blocking session API

It's only needed for unit tests, and makes reasoning about the session
state code harder.
parent e5bdb244
Pipeline #80438 passed with stage
in 4 minutes and 45 seconds
......@@ -61,14 +61,25 @@ void SmtpTest::testHello()
fakeServer.startAndWait();
KSmtp::Session session(QStringLiteral("127.0.0.1"), 5989);
session.setCustomHostname(hostname);
session.openAndWait();
QEventLoop loop;
connect(&session, &KSmtp::Session::stateChanged, this, [&loop](auto state) {
qDebug() << state;
if (state == KSmtp::Session::NotAuthenticated || state == KSmtp::Session::Disconnected) {
loop.quit();
}
});
session.open();
loop.exec();
qDebug() << "### Session state is:" << session.state();
QEXPECT_FAIL("ERROR", "Expected failure if HELO command not recognized", Continue);
QVERIFY2(session.state() == KSmtp::Session::NotAuthenticated, "Handshake failed");
session.quitAndWait();
session.quit();
if (session.state() != KSmtp::Session::Disconnected) {
loop.exec();
}
QVERIFY(fakeServer.isAllScenarioDone());
fakeServer.quit();
......@@ -136,7 +147,14 @@ void SmtpTest::testLoginJob()
fakeServer.startAndWait();
KSmtp::Session session(QStringLiteral("127.0.0.1"), 5989);
session.setCustomHostname(QStringLiteral("127.0.0.1"));
session.openAndWait();
QEventLoop loop;
connect(&session, &KSmtp::Session::stateChanged, this, [&loop](auto state) {
if (state == KSmtp::Session::NotAuthenticated || state == KSmtp::Session::Disconnected) {
loop.quit();
}
});
session.open();
loop.exec();
auto login = new KSmtp::LoginJob(&session);
login->setPreferedAuthMode(mode);
......@@ -152,7 +170,8 @@ void SmtpTest::testLoginJob()
QEXPECT_FAIL("Wrong password", "Expected failure if wrong password", Continue);
QVERIFY2(session.state() == KSmtp::Session::Authenticated, "Authentication failed");
session.quitAndWait();
session.quit();
loop.exec();
QVERIFY(fakeServer.isAllScenarioDone());
......@@ -202,7 +221,14 @@ void SmtpTest::testSendJob()
fakeServer.startAndWait();
KSmtp::Session session(QStringLiteral("127.0.0.1"), 5989);
session.setCustomHostname(QStringLiteral("127.0.0.1"));
session.openAndWait();
QEventLoop loop;
connect(&session, &KSmtp::Session::stateChanged, this, [&loop](auto state) {
if (state == KSmtp::Session::NotAuthenticated || state == KSmtp::Session::Disconnected) {
loop.quit();
}
});
session.open();
loop.exec();
auto send = new KSmtp::SendJob(&session);
send->setData("From: foo@bar.com\r\nTo: bar@foo.com\r\nHello world.\r\n.\r\n.\r\n..\r\n.Foo\r\nEnd");
......@@ -213,7 +239,8 @@ void SmtpTest::testSendJob()
// Checking job error code:
QVERIFY2(send->error() == errorCode, qPrintable(QStringLiteral("Unexpected LoginJob error: ") + send->errorString()));
session.quitAndWait();
session.quit();
loop.exec();
QVERIFY(fakeServer.isAllScenarioDone());
fakeServer.quit();
......
......@@ -15,7 +15,6 @@
#include "session_p.h"
#include "sessionthread_p.h"
#include <QEventLoop>
#include <QHostAddress>
#include <QHostInfo>
#include <QPointer>
......@@ -191,15 +190,6 @@ void Session::open()
d->startSocketTimer();
}
void Session::openAndWait()
{
QEventLoop loop(nullptr);
d->m_startLoop = &loop;
open();
d->m_startLoop->exec();
d->m_startLoop = nullptr;
}
void Session::quit()
{
if (d->m_state == Session::Disconnected) {
......@@ -210,23 +200,6 @@ void Session::quit()
d->sendData("QUIT");
}
void Session::quitAndWait()
{
if (d->m_state == Session::Disconnected) {
return;
}
QEventLoop loop;
connect(this, &Session::stateChanged, this, [&](Session::State state) {
if (state == Session::Disconnected) {
loop.quit();
}
});
d->setState(Quitting);
d->sendData("QUIT");
loop.exec();
}
void SessionPrivate::setState(Session::State s)
{
if (m_state == s) {
......@@ -235,11 +208,6 @@ void SessionPrivate::setState(Session::State s)
m_state = s;
Q_EMIT q->stateChanged(m_state);
// After a handshake success or failure, exit the startup event loop if any
if (m_startLoop && (m_state == Session::NotAuthenticated || m_state == Session::Disconnected)) {
m_startLoop->quit();
}
}
void SessionPrivate::sendData(const QByteArray &data)
......
......@@ -38,8 +38,8 @@ public:
/**
Creates a new SMTP session to the specified host and port.
After creating the session, call setUseNetworkProxy() if necessary
and then either open() or openAndWait() to open the connection.
@sa open(), openAndWait()
and then either open() to open the connection.
@sa open()
*/
explicit Session(const QString &hostName, quint16 port, QObject *parent = nullptr);
~Session() override;
......@@ -104,43 +104,20 @@ public:
You should connect to stateChanged() before calling this method, and wait until the session's
state is NotAuthenticated (Session is ready for a LoginJob) or Disconnected (connecting to the
server failed)
@sa openAndWait()
*/
void open();
/**
Opens the connection to the server and blocks the execution until the Session is in the
NotAuthenticated state (ready for a LoginJob) or Disconnected (connecting to the server failed)
@sa open()
*/
void openAndWait();
/**
Requests the server to quit the connection.
This sends a "QUIT" command to the server and will not close the connection until
it receives a response. That means you should not delete this object right after
calling close, instead wait for stateChanged() to change to Disconnected, or use
quitAndWait().
calling close, instead wait for stateChanged() to change to Disconnected.
See RFC 821, Chapter 4.1.1, "QUIT".
@sa quitAndWait()
*/
void quit();
/**
Requests the server to quit the connection and blocks the execution until the
server replies and closes the connection.
See RFC 821, Chapter 4.1.1, "QUIT".
@sa quit()
*/
void quitAndWait();
Q_SIGNALS:
void stateChanged(KSmtp::Session::State state);
void connectionError(const QString &error);
......
......@@ -17,7 +17,6 @@
#include <QSslSocket>
class KJob;
class QEventLoop;
namespace KSmtp
{
......@@ -73,7 +72,6 @@ private:
SessionUiProxy::Ptr m_uiProxy;
int m_socketTimerInterval = 0;
QTimer m_socketTimer;
QEventLoop *m_startLoop = nullptr;
QSsl::SslProtocol m_sslVersion;
// Jobs
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment