tests/auto/qsemaphore/tst_qsemaphore.cpp
changeset 33 3e2da88830cd
parent 18 2f34d5167611
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    61 private slots:
    61 private slots:
    62     void acquire();
    62     void acquire();
    63     void tryAcquire();
    63     void tryAcquire();
    64     void tryAcquireWithTimeout_data();
    64     void tryAcquireWithTimeout_data();
    65     void tryAcquireWithTimeout();
    65     void tryAcquireWithTimeout();
       
    66     void tryAcquireWithTimeoutStarvation();
    66     void release();
    67     void release();
    67     void available();
    68     void available();
    68     void producerConsumer();
    69     void producerConsumer();
    69 };
    70 };
    70 
    71 
   230 
   231 
   231 void tst_QSemaphore::tryAcquireWithTimeout_data()
   232 void tst_QSemaphore::tryAcquireWithTimeout_data()
   232 {
   233 {
   233     QTest::addColumn<int>("timeout");
   234     QTest::addColumn<int>("timeout");
   234 
   235 
   235     QTest::newRow("") << 1000;
   236     QTest::newRow("1s") << 1000;
   236     QTest::newRow("") << 10000;
   237     QTest::newRow("10s") << 10000;
   237 }
   238 }
   238 
   239 
   239 void tst_QSemaphore::tryAcquireWithTimeout()
   240 void tst_QSemaphore::tryAcquireWithTimeout()
   240 {
   241 {
   241     QFETCH(int, timeout);
   242     QFETCH(int, timeout);
   312 
   313 
   313     time.start();
   314     time.start();
   314     QVERIFY(!semaphore.tryAcquire(10, timeout));
   315     QVERIFY(!semaphore.tryAcquire(10, timeout));
   315     QVERIFY(time.elapsed() >= timeout);
   316     QVERIFY(time.elapsed() >= timeout);
   316     QCOMPARE(semaphore.available(), 0);
   317     QCOMPARE(semaphore.available(), 0);
       
   318 }
       
   319 
       
   320 void tst_QSemaphore::tryAcquireWithTimeoutStarvation()
       
   321 {
       
   322     class Thread : public QThread
       
   323     {
       
   324     public:
       
   325         QSemaphore startup;
       
   326         QSemaphore *semaphore;
       
   327         int amountToConsume, timeout;
       
   328 
       
   329         void run()
       
   330         {
       
   331             startup.release();
       
   332             forever {
       
   333                 if (!semaphore->tryAcquire(amountToConsume, timeout))
       
   334                     break;
       
   335                 semaphore->release(amountToConsume);
       
   336             }
       
   337         }
       
   338     };
       
   339 
       
   340     QSemaphore semaphore;
       
   341     semaphore.release(1);
       
   342 
       
   343     Thread consumer;
       
   344     consumer.semaphore = &semaphore;
       
   345     consumer.amountToConsume = 1;
       
   346     consumer.timeout = 1000;
       
   347 
       
   348     // start the thread and wait for it to start consuming
       
   349     consumer.start();
       
   350     consumer.startup.acquire();
       
   351 
       
   352     // try to consume more than the thread we started is, and provide a longer
       
   353     // timeout... we should timeout, not wait indefinitely
       
   354     QVERIFY(!semaphore.tryAcquire(consumer.amountToConsume * 2, consumer.timeout * 2));
       
   355 
       
   356     // the consumer should still be running
       
   357     QVERIFY(consumer.isRunning() && !consumer.isFinished());
       
   358 
       
   359     // acquire, and wait for smallConsumer to timeout
       
   360     semaphore.acquire();
       
   361     QVERIFY(consumer.wait());
   317 }
   362 }
   318 
   363 
   319 void tst_QSemaphore::release()
   364 void tst_QSemaphore::release()
   320 { DEPENDS_ON("acquire"); }
   365 { DEPENDS_ON("acquire"); }
   321 
   366