diff options
author | Shane Kearns <shane.kearns@accenture.com> | 2011-04-04 16:32:20 (GMT) |
---|---|---|
committer | Shane Kearns <shane.kearns@accenture.com> | 2011-04-05 10:26:42 (GMT) |
commit | 108dbea7145c941ba39f3958596cd1f348a2a049 (patch) | |
tree | b386fd5a9f44083ed3370322689a92ef35edc24d /tests/auto/qhostinfo | |
parent | a04905c2bb5caac9c9c0a3d0928f6fdfb4600cb9 (diff) | |
download | Qt-108dbea7145c941ba39f3958596cd1f348a2a049.zip Qt-108dbea7145c941ba39f3958596cd1f348a2a049.tar.gz Qt-108dbea7145c941ba39f3958596cd1f348a2a049.tar.bz2 |
Add test case for thread safety of the QHostInfo async API
The existing threadSafety test only tested calling the blocking API
from multiple threads. As the asynchronous API may be implemented in
a different way, it should be tested separately.
Create 10 threads, each of which queues 10 async name lookups.
Reviewed-by: Markus Goetz
Diffstat (limited to 'tests/auto/qhostinfo')
-rw-r--r-- | tests/auto/qhostinfo/tst_qhostinfo.cpp | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/tests/auto/qhostinfo/tst_qhostinfo.cpp b/tests/auto/qhostinfo/tst_qhostinfo.cpp index 8be8dcb..7b2fc55 100644 --- a/tests/auto/qhostinfo/tst_qhostinfo.cpp +++ b/tests/auto/qhostinfo/tst_qhostinfo.cpp @@ -126,6 +126,7 @@ private slots: void raceCondition(); void threadSafety(); + void threadSafetyAsynchronousAPI(); void multipleSameLookups(); void multipleDifferentLookups_data(); @@ -419,7 +420,7 @@ protected: void tst_QHostInfo::threadSafety() { const int nattempts = 5; -#if defined(Q_OS_WINCE) +#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) const int runs = 10; #else const int runs = 100; @@ -433,6 +434,56 @@ void tst_QHostInfo::threadSafety() } } +class LookupReceiver : public QObject +{ + Q_OBJECT +public slots: + void start(); + void resultsReady(const QHostInfo&); +public: + QHostInfo result; + int numrequests; +}; + +void LookupReceiver::start() +{ + for (int i=0;i<numrequests;i++) + QHostInfo::lookupHost(QString("qt.nokia.com"), this, SLOT(resultsReady(const QHostInfo&))); +} + +void LookupReceiver::resultsReady(const QHostInfo &info) +{ + result = info; + numrequests--; + if (numrequests == 0 || info.error() != QHostInfo::NoError) + QThread::currentThread()->quit(); +} + +void tst_QHostInfo::threadSafetyAsynchronousAPI() +{ + const int nattempts = 10; + const int lookupsperthread = 10; + QList<QThread*> threads; + QList<LookupReceiver*> receivers; + for (int i = 0; i < nattempts; ++i) { + QThread* thread = new QThread; + LookupReceiver* receiver = new LookupReceiver; + receiver->numrequests = lookupsperthread; + receivers.append(receiver); + receiver->moveToThread(thread); + connect(thread, SIGNAL(started()), receiver, SLOT(start())); + thread->start(); + threads.append(thread); + } + for (int k = threads.count() - 1; k >= 0; --k) + QVERIFY(threads.at(k)->wait(60000)); + foreach (LookupReceiver* receiver, receivers) { + QCOMPARE(receiver->result.error(), QHostInfo::NoError); + QCOMPARE(receiver->result.addresses().at(0).toString(), QString("87.238.50.178")); + QCOMPARE(receiver->numrequests, 0); + } +} + // this test is for the multi-threaded QHostInfo rewrite. It is about getting results at all, // not about getting correct IPs void tst_QHostInfo::multipleSameLookups() |