tests/auto/qhostinfo/tst_qhostinfo.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
parent 4 3b1da2848fc7
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
    70 #include <unistd.h>
    70 #include <unistd.h>
    71 #include <signal.h>
    71 #include <signal.h>
    72 #endif
    72 #endif
    73 
    73 
    74 #include <qhostinfo.h>
    74 #include <qhostinfo.h>
       
    75 #include "private/qhostinfo_p.h"
    75 
    76 
    76 #if !defined(QT_NO_GETADDRINFO)
    77 #if !defined(QT_NO_GETADDRINFO)
    77 # if !defined(Q_OS_WINCE)
    78 # if !defined(Q_OS_WINCE)
    78 #  include <sys/types.h>
    79 #  include <sys/types.h>
    79 # else
    80 # else
   106 
   107 
   107 
   108 
   108 public slots:
   109 public slots:
   109     void init();
   110     void init();
   110     void cleanup();
   111     void cleanup();
       
   112     void initTestCase();
       
   113 
   111 private slots:
   114 private slots:
   112     void getSetCheck();
   115     void getSetCheck();
   113     void staticInformation();
   116     void staticInformation();
   114     void initTestCase();
       
   115     void lookupIPv4_data();
   117     void lookupIPv4_data();
   116     void lookupIPv4();
   118     void lookupIPv4();
   117     void lookupIPv6_data();
   119     void lookupIPv6_data();
   118     void lookupIPv6();
   120     void lookupIPv6();
   119     void reverseLookup_data();
   121     void reverseLookup_data();
   125     void raceCondition();
   127     void raceCondition();
   126     void threadSafety();
   128     void threadSafety();
   127 
   129 
   128     void multipleSameLookups();
   130     void multipleSameLookups();
   129     void multipleDifferentLookups();
   131     void multipleDifferentLookups();
       
   132 
       
   133     void cache();
   130 
   134 
   131 protected slots:
   135 protected slots:
   132     void resultsReady(const QHostInfo &);
   136     void resultsReady(const QHostInfo &);
   133 
   137 
   134 private:
   138 private:
   203     QTcpServer server;
   207     QTcpServer server;
   204     if (server.listen(QHostAddress("::1"))) {
   208     if (server.listen(QHostAddress("::1"))) {
   205         // We have IPv6 support
   209         // We have IPv6 support
   206         ipv6Available = true;
   210         ipv6Available = true;
   207     }
   211     }
       
   212 
       
   213 
       
   214     // run each testcase with and without test enabled
       
   215     QTest::addColumn<bool>("cache");
       
   216     QTest::newRow("WithCache") << true;
       
   217     QTest::newRow("WithoutCache") << false;
   208 }
   218 }
   209 
   219 
   210 void tst_QHostInfo::init()
   220 void tst_QHostInfo::init()
   211 {
   221 {
       
   222     // delete the cache so inidividual testcase results are independant from each other
       
   223     qt_qhostinfo_clear_cache();
       
   224 
       
   225     QFETCH_GLOBAL(bool, cache);
       
   226     qt_qhostinfo_enable_cache(cache);
   212 }
   227 }
   213 
   228 
   214 void tst_QHostInfo::cleanup()
   229 void tst_QHostInfo::cleanup()
   215 {
   230 {
   216 }
   231 }
   456     // spin two seconds more to see if it is not more than expected
   471     // spin two seconds more to see if it is not more than expected
   457     QTestEventLoop::instance().enterLoop(2);
   472     QTestEventLoop::instance().enterLoop(2);
   458     QTRY_VERIFY(lookupsDoneCounter == COUNT);
   473     QTRY_VERIFY(lookupsDoneCounter == COUNT);
   459 }
   474 }
   460 
   475 
       
   476 void tst_QHostInfo::cache()
       
   477 {
       
   478     QFETCH_GLOBAL(bool, cache);
       
   479     if (!cache)
       
   480         return; // test makes only sense when cache enabled
       
   481 
       
   482     // reset slot counter
       
   483     lookupsDoneCounter = 0;
       
   484 
       
   485     // lookup once, wait in event loop, result should not come directly.
       
   486     bool valid = true;
       
   487     int id = -1;
       
   488     QHostInfo result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
       
   489     QTestEventLoop::instance().enterLoop(5);
       
   490     QVERIFY(!QTestEventLoop::instance().timeout());
       
   491     QVERIFY(valid == false);
       
   492     QVERIFY(result.addresses().isEmpty());
       
   493 
       
   494     // loopkup second time, result should come directly
       
   495     valid = false;
       
   496     result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
       
   497     QVERIFY(valid == true);
       
   498     QVERIFY(!result.addresses().isEmpty());
       
   499 
       
   500     // clear the cache
       
   501     qt_qhostinfo_clear_cache();
       
   502 
       
   503     // lookup third time, result should not come directly.
       
   504     valid = true;
       
   505     result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
       
   506     QTestEventLoop::instance().enterLoop(5);
       
   507     QVERIFY(!QTestEventLoop::instance().timeout());
       
   508     QVERIFY(valid == false);
       
   509     QVERIFY(result.addresses().isEmpty());
       
   510 
       
   511     // the slot should have been called 2 times.
       
   512     QVERIFY(lookupsDoneCounter == 2);
       
   513 }
       
   514 
   461 void tst_QHostInfo::resultsReady(const QHostInfo &hi)
   515 void tst_QHostInfo::resultsReady(const QHostInfo &hi)
   462 {
   516 {
   463     lookupDone = true;
   517     lookupDone = true;
   464     lookupResults = hi;
   518     lookupResults = hi;
   465     lookupsDoneCounter++;
   519     lookupsDoneCounter++;