From 7347a5f99df2da154acb1bdc538d4bc771cab066 Mon Sep 17 00:00:00 2001 From: David Faure Date: Thu, 18 Aug 2011 17:48:35 +0200 Subject: Make the DBus timeout configurable in QDBusAbstractInterface. Merge-request: 1253 Reviewed-by: Thiago Reviewed-by: Frederik Gladhorn (cherry picked from commit e58a402fbee2fc8af8cd651acafdc28525ed1314) Change-Id: I4246047b149193e510f2984a0b1a1fae655b9a51 Reviewed-on: http://codereview.qt.nokia.com/3580 Reviewed-by: Qt Sanity Bot Reviewed-by: Frederik Gladhorn --- .../com.trolltech.QtDBus.Pinger.xml | 4 + tests/auto/qdbusabstractinterface/interface.cpp | 14 ++++ tests/auto/qdbusabstractinterface/interface.h | 1 + tests/auto/qdbusabstractinterface/pinger.h | 7 ++ .../tst_qdbusabstractinterface.cpp | 92 ++++++++++++++++++++++ 5 files changed, 118 insertions(+) (limited to 'tests') diff --git a/tests/auto/qdbusabstractinterface/com.trolltech.QtDBus.Pinger.xml b/tests/auto/qdbusabstractinterface/com.trolltech.QtDBus.Pinger.xml index 1667591708..d945ec9b43 100644 --- a/tests/auto/qdbusabstractinterface/com.trolltech.QtDBus.Pinger.xml +++ b/tests/auto/qdbusabstractinterface/com.trolltech.QtDBus.Pinger.xml @@ -15,6 +15,10 @@ + + + + diff --git a/tests/auto/qdbusabstractinterface/interface.cpp b/tests/auto/qdbusabstractinterface/interface.cpp index 0326177fc0..849db93084 100644 --- a/tests/auto/qdbusabstractinterface/interface.cpp +++ b/tests/auto/qdbusabstractinterface/interface.cpp @@ -40,9 +40,23 @@ ****************************************************************************/ #include "interface.h" +#include Interface::Interface() { } +// Export the sleep function +// TODO QT5: remove this class, QThread::msleep is now public +class FriendlySleepyThread : public QThread { +public: + using QThread::msleep; +}; + +int Interface::sleepMethod(int msec) +{ + FriendlySleepyThread::msleep(msec); + return 42; +} + #include "moc_interface.cpp" diff --git a/tests/auto/qdbusabstractinterface/interface.h b/tests/auto/qdbusabstractinterface/interface.h index b840a38e8e..0fb15fe6c5 100644 --- a/tests/auto/qdbusabstractinterface/interface.h +++ b/tests/auto/qdbusabstractinterface/interface.h @@ -101,6 +101,7 @@ public: public slots: Q_SCRIPTABLE void voidMethod() {} + Q_SCRIPTABLE int sleepMethod(int); Q_SCRIPTABLE QString stringMethod() { return "Hello, world"; } Q_SCRIPTABLE RegisteredType complexMethod() { return RegisteredType("Hello, world"); } Q_SCRIPTABLE QString multiOutMethod(int &value) { value = 42; return "Hello, world"; } diff --git a/tests/auto/qdbusabstractinterface/pinger.h b/tests/auto/qdbusabstractinterface/pinger.h index 6245a5a02f..739a14229f 100644 --- a/tests/auto/qdbusabstractinterface/pinger.h +++ b/tests/auto/qdbusabstractinterface/pinger.h @@ -117,6 +117,13 @@ public Q_SLOTS: // METHODS return reply; } + inline QDBusPendingReply sleepMethod(int in0) + { + QList argumentList; + argumentList << qVariantFromValue(in0); + return asyncCallWithArgumentList(QLatin1String("sleepMethod"), argumentList); + } + inline QDBusPendingReply stringMethod() { QList argumentList; diff --git a/tests/auto/qdbusabstractinterface/tst_qdbusabstractinterface.cpp b/tests/auto/qdbusabstractinterface/tst_qdbusabstractinterface.cpp index 00e3a76d15..994df058fa 100644 --- a/tests/auto/qdbusabstractinterface/tst_qdbusabstractinterface.cpp +++ b/tests/auto/qdbusabstractinterface/tst_qdbusabstractinterface.cpp @@ -111,6 +111,8 @@ private slots: void makeAsyncComplexCallPeer(); void makeAsyncMultiOutCallPeer(); + void callWithTimeout(); + void stringPropRead(); void stringPropWrite(); void variantPropRead(); @@ -458,6 +460,96 @@ void tst_QDBusAbstractInterface::makeAsyncMultiOutCallPeer() QCoreApplication::instance()->processEvents(); } +static const char server_serviceName[] = "com.trolltech.autotests.dbusserver"; +static const char server_objectPath[] = "/com/trolltech/server"; +static const char server_interfaceName[] = "com.trolltech.QtDBus.Pinger"; + +class DBusServerThread : public QThread +{ +public: + DBusServerThread() { + start(); + m_ready.acquire(); + } + ~DBusServerThread() { + quit(); + wait(); + } + + void run() + { + QDBusConnection con = QDBusConnection::connectToBus(QDBusConnection::SessionBus, "ThreadConnection"); + if (!con.isConnected()) + qWarning("Error registering to DBus"); + if (!con.registerService(server_serviceName)) + qWarning("Error registering service name"); + Interface targetObj; + con.registerObject(server_objectPath, &targetObj, QDBusConnection::ExportScriptableContents); + m_ready.release(); + exec(); + + QDBusConnection::disconnectFromBus( con.name() ); + } +private: + QSemaphore m_ready; +}; + +void tst_QDBusAbstractInterface::callWithTimeout() +{ + QDBusConnection con = QDBusConnection::sessionBus(); + QVERIFY2(con.isConnected(), "Not connected to D-Bus"); + + DBusServerThread serverThread; + + QDBusMessage msg = QDBusMessage::createMethodCall(server_serviceName, + server_objectPath, server_interfaceName, "sleepMethod"); + msg << 100; + + { + // Call with no timeout -> works + QDBusMessage reply = con.call(msg); + QCOMPARE((int)reply.type(), (int)QDBusMessage::ReplyMessage); + QCOMPARE(reply.arguments().at(0).toInt(), 42); + } + + { + // Call with 1 sec timeout -> fails + QDBusMessage reply = con.call(msg, QDBus::Block, 1); + QCOMPARE(reply.type(), QDBusMessage::ErrorMessage); + } + + // Now using QDBusInterface + + QDBusInterface iface(server_serviceName, server_objectPath, server_interfaceName, con); + { + // Call with no timeout + QDBusMessage reply = iface.call("sleepMethod", 100); + QCOMPARE(reply.type(), QDBusMessage::ReplyMessage); + QCOMPARE(reply.arguments().at(0).toInt(), 42); + } + { + // Call with 1 sec timeout -> fails + iface.setTimeout(1); + QDBusMessage reply = iface.call("sleepMethod", 100); + QCOMPARE(reply.type(), QDBusMessage::ErrorMessage); + } + + // Now using generated code + com::trolltech::QtDBus::Pinger p(server_serviceName, server_objectPath, QDBusConnection::sessionBus()); + { + // Call with no timeout + QDBusReply reply = p.sleepMethod(100); + QVERIFY(reply.isValid()); + QCOMPARE(int(reply), 42); + } + { + // Call with 1 sec timeout -> fails + p.setTimeout(1); + QDBusReply reply = p.sleepMethod(100); + QVERIFY(!reply.isValid()); + } +} + void tst_QDBusAbstractInterface::stringPropRead() { Pinger p = getPinger(); -- cgit v1.2.3