Fix tst_QSocketNotifier on Mac OS X

Socket notifier behavior is very OS-dependent. QtNetwork uses non-
immediately (it will return -1 w/ errno=EINPROGRESS). We have to wait
with select(2) to indicate that the connection is ready, then call
connect(2) again. When this happens, we need another call to select(2)
to get notification on the listening socket so that we can call
accept(2) to complete the connection.

The mixingWithTimers() failure happens due to the test expecting a
single processEvents() call to be able to completely connect a TCP
socket. But as described above, this may not happen. The test should
QTRY_COMPARE() to give the test a chance to let all this happen.

The posixSockets() test can fail due to the same connect() behavior. The
test already has a comment about the write notifier behavior being very
OS dependent. This caused the first enterLoop() to return too early,
before the read notifier fired (which is what the test is checking for,
that the read notifier fired). Move creation of the write notifier to
where we expect it to fire, just before writing to the posix socket.

In the same test, the read notifier inside QTcpSocket may not fire after
the write notifier on the posix socket. Use the waitForReadyRead()
function to give the socket a chance to read the data written to the
posix socket.

Change-Id: I541e6ee9a39a92ce3acf6b9ffee51079febe43e4
Reviewed-by: Jonas Gastal <jgastal@profusion.mobi>
Reviewed-by: Shane Kearns <ext-shane.2.kearns@nokia.com>
This commit is contained in:
Bradley T. Hughes 2012-02-08 12:26:09 +01:00 committed by Qt by Nokia
parent 7bf37d966e
commit 3f74aea9bb
2 changed files with 12 additions and 9 deletions

View File

@ -6,5 +6,3 @@ SOURCES = tst_qsocketnotifier.cpp
requires(contains(QT_CONFIG,private_tests))
include(../../../network/socket/platformsocketengine/platformsocketengine.pri)
mac: CONFIG += insignificant_test # QTBUG-22746

View File

@ -237,7 +237,7 @@ void tst_QSocketNotifier::mixingWithTimers()
QCoreApplication::processEvents();
QCOMPARE(helper.timerActivated, true);
QCOMPARE(helper.socketActivated, true);
QTRY_COMPARE(helper.socketActivated, true);
}
void tst_QSocketNotifier::posixSockets()
@ -264,10 +264,7 @@ void tst_QSocketNotifier::posixSockets()
connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy readSpy(&rn, SIGNAL(activated(int)));
QVERIFY(readSpy.isValid());
QSocketNotifier wn(posixSocket, QSocketNotifier::Write);
connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy writeSpy(&wn, SIGNAL(activated(int)));
QVERIFY(writeSpy.isValid());
// No write notifier, some systems trigger write notification on socket creation, but not all
QSocketNotifier en(posixSocket, QSocketNotifier::Exception);
connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy errorSpy(&en, SIGNAL(activated(int)));
@ -278,19 +275,27 @@ void tst_QSocketNotifier::posixSockets()
QTestEventLoop::instance().enterLoop(3);
QCOMPARE(readSpy.count(), 1);
writeSpy.clear(); //depending on OS, write notifier triggers on creation or not.
QCOMPARE(errorSpy.count(), 0);
char buffer[100];
qt_safe_read(posixSocket, buffer, 100);
int r = qt_safe_read(posixSocket, buffer, 100);
QCOMPARE(r, 6);
QCOMPARE(buffer, "hello");
QSocketNotifier wn(posixSocket, QSocketNotifier::Write);
connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy writeSpy(&wn, SIGNAL(activated(int)));
QVERIFY(writeSpy.isValid());
qt_safe_write(posixSocket, "goodbye", 8);
QTestEventLoop::instance().enterLoop(3);
QCOMPARE(readSpy.count(), 1);
QCOMPARE(writeSpy.count(), 1);
QCOMPARE(errorSpy.count(), 0);
// Write notifier may have fired before the read notifier inside
// QTcpSocket, give QTcpSocket a chance to see the incoming data
passive->waitForReadyRead(100);
QCOMPARE(passive->readAll(), QByteArray("goodbye",8));
}
qt_safe_close(posixSocket);