diff options
author | Øystein Heskestad <oystein.heskestad@qt.io> | 2022-05-30 17:35:53 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2022-06-04 00:22:55 +0000 |
commit | 782fbe0f63af5aeb583f84c28b7d0ff482bd28e4 (patch) | |
tree | ce65c2f181de2bc9a92d5e7b143087bfe6f55736 /tests/auto/network | |
parent | 8b262335733a25a41a9625bfc5e29d41e7e25481 (diff) |
The new signal pendingConnectionAvailable is added to QTcpServer
The new signal pendingConnnectionAvailable is emitted after a new
connection has been added to the pending connections queue. Connect
to this signal and call nextPendingConnection to handle incoming
connections.
The existing unchanged newConnection signal is emitted after the
overridable function incomingConnection is called, regardless of whether
a new connection is added to the pending connections queue in the
incomingConnection function or not.
If a subclass that overrides incomingConnection either decides to not
add all incoming connections to the pending connections queue, or to
postpone adding the connection until a handshake is successfully
completed, the pendingConnectionAvailable signal should be to used,
because this signal directly corresponds to insertions to the pending
connections queue.
[ChangeLog][QtNetwork][QTcpServer] New signal pendingConnectionAvailable
is emitted when a new connection is added
Task-number: QTBUG-100823
Change-Id: I00c76761389065f68271553e69e6c45c393a2fa8
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'tests/auto/network')
-rw-r--r-- | tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp index 23f9cbdafc..52c7375a80 100644 --- a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp +++ b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp @@ -94,6 +94,9 @@ private slots: void pauseAccepting(); + void pendingConnectionAvailable_data(); + void pendingConnectionAvailable(); + private: bool shouldSkipIpv6TestsForBrokenGetsockopt(); #ifdef SHOULD_CHECK_SYSCALL_SUPPORT @@ -1058,5 +1061,73 @@ void tst_QTcpServer::pauseAccepting() QCOMPARE(spy.count(), 6); } + +// Only adds the socket to the pending connections list after emitNextSocket is +// called. It's very artificial, but it allows us to test the behavior of +// the pendingConnectionAvailable signal when a server doesn't add the socket +// during the incomingConnection virtual function. +class DerivedServer : public QTcpServer +{ +public: + explicit DerivedServer(QObject *parent = nullptr) + : QTcpServer(parent) + { + } + + void emitNextSocket() + { + if (m_socketDescriptors.isEmpty()) + return; + auto *socket = new QTcpSocket(this); + socket->setSocketDescriptor(m_socketDescriptors.back()); + m_socketDescriptors.pop_back(); + addPendingConnection(socket); + } +protected: + void incomingConnection(qintptr socketDescriptor) override + { + m_socketDescriptors.push_back(socketDescriptor); + } +private: + QList<qintptr> m_socketDescriptors; +}; + +void tst_QTcpServer::pendingConnectionAvailable_data() +{ + QTest::addColumn<bool>("useDerivedServer"); + QTest::newRow("QTcpServer") << false; + QTest::newRow("DerivedServer") << true; +} + +void tst_QTcpServer::pendingConnectionAvailable() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + QSKIP("This feature does not differentiate with or without proxy"); + QFETCH(bool, useDerivedServer); + + QTcpServer *server = useDerivedServer ? new DerivedServer : new QTcpServer; + if (!server->listen(QHostAddress::LocalHost, 0)) { + qWarning() << "Server failed to listen:" << server->errorString(); + QSKIP("Server failed to listen"); + } + QSignalSpy newConnectionSpy(server, &QTcpServer::newConnection); + QSignalSpy pendingConnectionSpy(server, &QTcpServer::pendingConnectionAvailable); + + QTcpSocket socket; + socket.connectToHost(QHostAddress::LocalHost, server->serverPort()); + + QVERIFY(newConnectionSpy.wait()); + QVERIFY(socket.waitForConnected()); + QCOMPARE(socket.state(), QTcpSocket::ConnectedState); + + int expectedPendingConnections = useDerivedServer ? 0 : 1; + QCOMPARE(pendingConnectionSpy.count(), expectedPendingConnections); + + if (useDerivedServer) + static_cast<DerivedServer *>(server)->emitNextSocket(); + QCOMPARE(pendingConnectionSpy.count(), 1); +} + QTEST_MAIN(tst_QTcpServer) #include "tst_qtcpserver.moc" |