From c1b6fbfb734063edb9315c109f227c90bd69fbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 26 Jun 2019 22:18:08 +0200 Subject: Investigate tst_handshakeTimeout test flakiness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove "localhost" in favor of "127.0.0.1" to avoid any name resolution. And wait 5 seconds for all websockets to timeout together, while printing how many have not timed-out in case of failure. Credits to MÃ¥rten Nordheim for coding this. With this patch, I can not reproduce the flakiness any longer. Lets see if this is also the case in our CI. Fixes: QTBUG-76572 Change-Id: Ibdbafbd7786c87138bdc84ebc690177211f23f5b Reviewed-by: Timur Pocheptsov --- .../qwebsocketserver/tst_qwebsocketserver.cpp | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/auto/websockets/qwebsocketserver/tst_qwebsocketserver.cpp b/tests/auto/websockets/qwebsocketserver/tst_qwebsocketserver.cpp index 1224790..442196b 100644 --- a/tests/auto/websockets/qwebsocketserver/tst_qwebsocketserver.cpp +++ b/tests/auto/websockets/qwebsocketserver/tst_qwebsocketserver.cpp @@ -689,20 +689,26 @@ struct SocketSpy { } }; -static void openManyConnections(QList *sockets, quint16 port) +static void openManyConnections(QList *sockets, quint16 port, int numConnections) { - /* do some incomplete connections */ - for (int i = 0; i < 50; i++) { + for (int i = 0; i < numConnections; i++) { QTcpSocket *c = new QTcpSocket; QSignalSpy *spy = new QSignalSpy(c, &QTcpSocket::disconnected); - QString hostname = "localhost"; - c->connectToHost("localhost", port); + c->connectToHost("127.0.0.1", port); sockets->append(new SocketSpy{c, spy}); } } +// Sum the counts together, for better output on failure (e.g. "FAIL: Actual: 49, Expected: 50") +static int sumSocketSpyCount(const QList &sockets) +{ + return std::accumulate(sockets.cbegin(), sockets.cend(), 0, [](int c, SocketSpy *s) { + return c + s->disconnectSpy->count(); + }); +} + void tst_QWebSocketServer::tst_handshakeTimeout() { { // No Timeout @@ -729,22 +735,30 @@ void tst_QWebSocketServer::tst_handshakeTimeout() QVERIFY(plainServer.listen()); + /* QTcpServer has a default of 30 pending connections. The test checks + * whether, when that list is full, the connections are dropped after + * a timeout and later pending connections are processed. */ + const int numConnections = 50; QList sockets; auto cleaner = qScopeGuard([&sockets]() { qDeleteAll(sockets); }); - openManyConnections(&sockets, plainServer.serverPort()); + openManyConnections(&sockets, plainServer.serverPort(), numConnections); QCoreApplication::processEvents(); + + /* We have 50 plain TCP connections open, that are not proper websockets. */ QCOMPARE(plainServerConnectionSpy.count(), 0); QWebSocket socket; socket.open(plainServer.serverUrl().toString()); + /* Check that a real websocket will be processed after some non-websocket + * TCP connections timeout. */ QTRY_COMPARE(plainServerConnectionSpy.count(), 1); QScopedPointer plainServerSocket(plainServer.nextPendingConnection()); QVERIFY(!plainServerSocket.isNull()); - for (auto s : sockets) - QTRY_COMPARE(s->disconnectSpy->count(), 1); + /* Check that all non websocket connections eventually timeout. */ + QTRY_COMPARE(sumSocketSpyCount(sockets), numConnections); plainServer.close(); } @@ -761,9 +775,10 @@ void tst_QWebSocketServer::tst_handshakeTimeout() QVERIFY(secureServer.listen()); + const int numConnections = 50; QList sockets; auto cleaner = qScopeGuard([&sockets]() { qDeleteAll(sockets); }); - openManyConnections(&sockets, secureServer.serverPort()); + openManyConnections(&sockets, secureServer.serverPort(), numConnections); QCoreApplication::processEvents(); QCOMPARE(secureServerConnectionSpy.count(), 0); @@ -779,8 +794,7 @@ void tst_QWebSocketServer::tst_handshakeTimeout() QScopedPointer serverSocket(secureServer.nextPendingConnection()); QVERIFY(!serverSocket.isNull()); - for (auto s : sockets) - QTRY_COMPARE(s->disconnectSpy->count(), 1); + QTRY_COMPARE(sumSocketSpyCount(sockets), numConnections); secureServer.close(); } -- cgit v1.2.1