diff options
author | Peter Kümmel <syntheticpp@gmx.net> | 2015-08-24 18:34:40 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@theqtcompany.com> | 2015-09-04 11:52:06 +0000 |
commit | e8335d48aa8c6323a6b89d29c76f2b340afd1be4 (patch) | |
tree | 58e30e1e8ac5cd45acc23289753d447a7d3172de /tests/auto/websockets | |
parent | a01c1455afecc2a98598a574f69ce682586357ff (diff) | |
download | qtwebsockets-e8335d48aa8c6323a6b89d29c76f2b340afd1be4.tar.gz |
Set parent of internal socket objects
After moving the websocket into another thread
current code doesn't work because then the QTcpSocket/QSslSocket
objects reside in a different thread, for instance:
"QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread"
QObject::moveToThread(QThread*) also moves QObjects's children,
therefore their internal socket objects need to be children of QWebSocket.
QWebSocket has ownership of the internal socket, and the
smart pointer is not needed any more.
Change of cleanup code to prevent crashes with clang/msvc builds:
QWebSocketPrivate is a scoped member of QObject (not QWebSocket)
and is destroyed after QObject destructor body was executed,
and so m_pSocket&co had already been destroyed (being children)
when the destructor of QWebSocketPrivate is called via the
scoped pointer.
Analogous to 64927e04f202d33b9a9a1f94141ef692c0b513ac
Change-Id: I1ade6cda3fa793c30332cc5e103025e2dda3c78c
Reviewed-by: Luca Niccoli <lultimouomo@gmail.com>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'tests/auto/websockets')
-rw-r--r-- | tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp b/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp index aca25d0..cc3bca6 100644 --- a/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp +++ b/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp @@ -140,6 +140,8 @@ private Q_SLOTS: void tst_sendTextMessage(); void tst_sendBinaryMessage(); void tst_errorString(); + void tst_moveToThread(); + void tst_moveToThreadNoWarning(); #ifndef QT_NO_NETWORKPROXY void tst_setProxy(); #endif @@ -581,6 +583,107 @@ void tst_QWebSocket::tst_errorString() QCOMPARE(socket.errorString(), QStringLiteral("Host not found")); } +class WebSocket : public QWebSocket +{ + Q_OBJECT + +public: + explicit WebSocket() + { + connect(this, SIGNAL(triggerClose()), SLOT(onClose()), Qt::QueuedConnection); + connect(this, SIGNAL(triggerOpen(QUrl)), SLOT(onOpen(QUrl)), Qt::QueuedConnection); + connect(this, SIGNAL(triggerSendTextMessage(QString)), SLOT(onSendTextMessage(QString)), Qt::QueuedConnection); + connect(this, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)), Qt::QueuedConnection); + } + + void asyncClose() { triggerClose(); } + void asyncOpen(const QUrl &url) { triggerOpen(url); } + void asyncSendTextMessage(const QString &msg) { triggerSendTextMessage(msg); } + + QString receivedMessage; + +Q_SIGNALS: + void triggerClose(); + void triggerOpen(const QUrl &); + void triggerSendTextMessage(const QString &); + void done(); + +private Q_SLOTS: + void onClose() { close(); } + void onOpen(const QUrl &url) { open(url); } + void onSendTextMessage(const QString &msg) { sendTextMessage(msg); } + void onTextMessageReceived(const QString &msg) { receivedMessage = msg; done(); } +}; + +struct Warned +{ + static QtMessageHandler origHandler; + static bool warned; + static void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& str) + { + if (type == QtWarningMsg) { + warned = true; + } + if (origHandler) + origHandler(type, context, str); + } +}; +QtMessageHandler Warned::origHandler = 0; +bool Warned::warned = false; + + +void tst_QWebSocket::tst_moveToThread() +{ + Warned::origHandler = qInstallMessageHandler(&Warned::messageHandler); + + EchoServer echoServer; + + QThread* thread = new QThread; + thread->start(); + + WebSocket* socket = new WebSocket; + socket->moveToThread(thread); + + const QString textMessage = QStringLiteral("Hello world!"); + QSignalSpy socketConnectedSpy(socket, SIGNAL(connected())); + QUrl url = QUrl(QStringLiteral("ws://") + echoServer.hostAddress().toString() + + QStringLiteral(":") + QString::number(echoServer.port())); + url.setPath("/segment/with spaces"); + url.addQueryItem("queryitem", "with encoded characters"); + + socket->asyncOpen(url); + if (socketConnectedSpy.count() == 0) + QVERIFY(socketConnectedSpy.wait(500)); + + socket->asyncSendTextMessage(textMessage); + + QTimer timer; + timer.setInterval(1000); + timer.start(); + QEventLoop loop; + connect(socket, SIGNAL(done()), &loop, SLOT(quit())); + connect(socket, SIGNAL(done()), &timer, SLOT(stop())); + connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + loop.exec(); + + socket->asyncClose(); + + QCOMPARE(timer.isActive(), false); + QCOMPARE(socket->receivedMessage, textMessage); + + socket->deleteLater(); + thread->quit(); + thread->deleteLater(); +} + +void tst_QWebSocket::tst_moveToThreadNoWarning() +{ + // check for warnings in tst_moveToThread() + // couldn't done there because warnings are processed after the test run + QCOMPARE(Warned::warned, false); +} + + #ifndef QT_NO_NETWORKPROXY void tst_QWebSocket::tst_setProxy() { |