mudlet/test/functional_tests/TelnetServerStub.cpp
Vadim Peretokin 2e4a3b2ef0
infrastructure: stop the telnet test stub losing data sent before a client connects (#9862)
#### Brief overview of PR changes/additions
- TelnetServerStub::sendRaw() now queues bytes sent before the server
has accepted the client connection and flushes them on accept, instead
of dropping them with only a warning
- Between sessions it still warns and drops, so a dead session's bytes
cannot leak into the next connection; the destructor reports bytes that
were never delivered
- The flush checks the write() return like the adjacent welcome-message
path

#### Motivation for adding to Mudlet
Tests only wait on the client-side connected signal, which can fire
before the stub's server side accepts - on fast runners the first
payload was lost, failing TelnetStringSequenceRecoveryTest twice in a
row on macOS arm64 in #9861's CI. The stub is shared by ~50 functional
tests.

#### Other info (issues closed, discussion etc)
First validated on #9861 (run 31674918225 failed twice on arm64; run
31687605776 with the fix went green on all four platforms); it will be
dropped from that PR once this merges.

**Test case:** TelnetStringSequenceRecoveryTest,
TriggerSameLineMatchTest and UndoServerWrapTest pass (run twice each
locally with the final version).

Assisted-by: Claude:claude-fable-5
2026-08-13 13:20:12 +02:00

118 lines
4.5 KiB
C++

/***************************************************************************
* Copyright (C) 2025 by Nicolas Keita - nicolaskeita2@@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QPointer>
#include <QTcpSocket>
#include <QTimer>
#include <QHostAddress>
#include <QDebug>
#include "TelnetServerStub.h"
#include "utils.h"
#include <chrono>
using namespace std::chrono_literals;
TelnetServerStub::TelnetServerStub(QObject* parent)
: QTcpServer(parent)
{
connect(this, &QTcpServer::newConnection, this, &TelnetServerStub::onNewConnection);
}
void TelnetServerStub::start(const QString& host, quint16 port)
{
Q_UNUSED(host)
const QHostAddress addr = QHostAddress::LocalHost;
if (listen(addr, port)) {
qInfo().noquote() << qsl("✅ TelnetServerStub listening on %1:%2").arg(addr.toString()).arg(serverPort());
} else {
qCritical().noquote() << qsl("❌ Failed to start TelnetServerStub: %1").arg(errorString());
}
}
TelnetServerStub::~TelnetServerStub()
{
if (!mPendingData.isEmpty()) {
qWarning().noquote() << qsl("⚠️ TelnetServerStub destroyed with %1 undelivered queued bytes - no client was ever accepted.").arg(mPendingData.size());
}
}
void TelnetServerStub::sendRaw(const QByteArray& data)
{
if (!mpClient) {
if (mHadClient) {
// Between sessions there is no race to absorb, so queuing here
// would leak a dead session's bytes into the next connection:
qWarning() << "⚠️ sendRaw called without a connected client.";
return;
}
// Tests wait on the client-side connected signal, which can fire
// before this server side has accepted the connection - dropping the
// bytes here loses the payload on fast runners, so hold them until
// onNewConnection():
mPendingData.append(data);
return;
}
mpClient->write(data);
mpClient->flush();
}
void TelnetServerStub::onNewConnection()
{
QTcpSocket* client = nextPendingConnection();
if (!client) {
qWarning() << "⚠️ onNewConnection called but no pending connection.";
return;
}
mpClient = client;
mHadClient = true;
qInfo().noquote() << qsl("🔌 Client connected: %1").arg(client->peerAddress().toString());
if (!mPendingData.isEmpty()) {
const auto bytesWritten = client->write(mPendingData);
client->flush();
if (bytesWritten <= 0) {
qWarning().noquote() << qsl("⚠️ Failed to deliver %1 queued bytes to %2").arg(QString::number(mPendingData.size()), client->peerAddress().toString());
}
mPendingData.clear();
}
QPointer<QTcpSocket> safeClient = client;
QTimer::singleShot(100ms, [safeClient, welcomeMessage = mpWelcomeMessage]() {
if (!safeClient) {
return;
}
const auto bytesWritten = safeClient->write(welcomeMessage.toUtf8() + "\r\n");
safeClient->flush();
if (bytesWritten <= 0) {
qWarning().noquote() << qsl("⚠️ Failed to send welcome message to %1").arg(safeClient->peerAddress().toString());
}
});
connect(client, &QTcpSocket::disconnected, [safeClient]() {
if (!safeClient) {
return;
}
qInfo().noquote() << qsl("Client disconnected: %1").arg(safeClient->peerAddress().toString());
safeClient->deleteLater();
});
}