tests/auto/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    46 
    46 
    47 #ifndef QT_NO_CONCURRENT_TEST
    47 #ifndef QT_NO_CONCURRENT_TEST
    48 
    48 
    49 using namespace QtConcurrent;
    49 using namespace QtConcurrent;
    50 
    50 
    51 class tst_threadengine: public QObject
    51 class tst_QtConcurrentThreadEngine: public QObject
    52 {
    52 {
    53     Q_OBJECT
    53     Q_OBJECT
    54 public:
    54 public:
    55     void threadCount();
    55     void threadCount();
    56 private slots:
    56 private slots:
    77         QTest::qSleep(100);
    77         QTest::qSleep(100);
    78         return ThreadFinished;
    78         return ThreadFinished;
    79     }
    79     }
    80 };
    80 };
    81 
    81 
    82 void tst_threadengine::runDirectly()
    82 void tst_QtConcurrentThreadEngine::runDirectly()
    83 {
    83 {
    84     {
    84     {
    85         PrintUser engine;
    85         PrintUser engine;
    86         engine.startSingleThreaded();
    86         engine.startSingleThreaded();
    87         engine.startBlocking();
    87         engine.startBlocking();
   118     }
   118     }
   119     QString foo;
   119     QString foo;
   120     bool done;
   120     bool done;
   121 };
   121 };
   122 
   122 
   123 void tst_threadengine::result()
   123 void tst_QtConcurrentThreadEngine::result()
   124 {
   124 {
   125     StringResultUser engine;
   125     StringResultUser engine;
   126     QCOMPARE(*engine.startBlocking(), QString("Foo"));
   126     QCOMPARE(*engine.startBlocking(), QString("Foo"));
   127 }
   127 }
   128 
   128 
   145         return 0;
   145         return 0;
   146     }
   146     }
   147     bool done;
   147     bool done;
   148 };
   148 };
   149 
   149 
   150 void tst_threadengine::runThroughStarter()
   150 void tst_QtConcurrentThreadEngine::runThroughStarter()
   151 {
   151 {
   152     {
   152     {
   153         ThreadEngineStarter<QString> starter = startThreadEngine(new StringResultUser());
   153         ThreadEngineStarter<QString> starter = startThreadEngine(new StringResultUser());
   154         QFuture<QString>  f = starter.startAsynchronously();
   154         QFuture<QString>  f = starter.startAsynchronously();
   155         QCOMPARE(f.result(), QString("Foo"));
   155         QCOMPARE(f.result(), QString("Foo"));
   178         }
   178         }
   179         return ThreadFinished;
   179         return ThreadFinished;
   180     }
   180     }
   181 };
   181 };
   182 
   182 
   183 void tst_threadengine::cancel()
   183 void tst_QtConcurrentThreadEngine::cancel()
   184 {
   184 {
   185     {
   185     {
   186         CancelUser *engine = new CancelUser();
   186         CancelUser *engine = new CancelUser();
   187         QFuture<void> f = engine->startAsynchronously();
   187         QFuture<void> f = engine->startAsynchronously();
   188         f.cancel();
   188         f.cancel();
   232 };
   232 };
   233 
   233 
   234 // Test that a user task with a thread function that always
   234 // Test that a user task with a thread function that always
   235 // want to be throttled still completes. The thread engine
   235 // want to be throttled still completes. The thread engine
   236 // should make keep one thread running at all times.
   236 // should make keep one thread running at all times.
   237 void tst_threadengine::throttle()
   237 void tst_QtConcurrentThreadEngine::throttle()
   238 {
   238 {
   239     const int repeats = 10;
   239     const int repeats = 10;
   240     for (int i = 0; i < repeats; ++i) {
   240     for (int i = 0; i < repeats; ++i) {
   241         QFuture<void> f = (new ThrottleAlwaysUser())->startAsynchronously();
   241         QFuture<void> f = (new ThrottleAlwaysUser())->startAsynchronously();
   242         f.waitForFinished();
   242         f.waitForFinished();
   278     }
   278     }
   279 
   279 
   280     bool finishing;
   280     bool finishing;
   281 };
   281 };
   282 
   282 
   283 void tst_threadengine::threadCount()
   283 void tst_QtConcurrentThreadEngine::threadCount()
   284 {
   284 {
   285     const int repeats = 10;
   285     const int repeats = 10;
   286     for (int i = 0; i < repeats; ++i) {
   286     for (int i = 0; i < repeats; ++i) {
   287         ThreadCountUser t;
   287         ThreadCountUser t;
   288         t.startBlocking();
   288         t.startBlocking();
   318         return ThreadFinished;
   318         return ThreadFinished;
   319     }
   319     }
   320 };
   320 };
   321 
   321 
   322 
   322 
   323 void tst_threadengine::multipleResults()
   323 void tst_QtConcurrentThreadEngine::multipleResults()
   324 {
   324 {
   325     MultipleResultsUser *engine =  new MultipleResultsUser();
   325     MultipleResultsUser *engine =  new MultipleResultsUser();
   326     QFuture<int> f = engine->startAsynchronously();
   326     QFuture<int> f = engine->startAsynchronously();
   327     QCOMPARE(f.results().count() , 10);
   327     QCOMPARE(f.results().count() , 10);
   328     QCOMPARE(f.resultAt(0), 0);
   328     QCOMPARE(f.resultAt(0), 0);
   349     {
   349     {
   350         return 0;
   350         return 0;
   351     }
   351     }
   352 };
   352 };
   353 
   353 
   354 void tst_threadengine::stresstest()
   354 void tst_QtConcurrentThreadEngine::stresstest()
   355 {
   355 {
   356     const int times = 20000;
   356     const int times = 20000;
   357 
   357 
   358     for (int i = 0; i < times; ++i) {
   358     for (int i = 0; i < times; ++i) {
   359         VoidResultUser *engine = new VoidResultUser();
   359         VoidResultUser *engine = new VoidResultUser();
   377 public:
   377 public:
   378     bool shouldStartThread() { return false; }
   378     bool shouldStartThread() { return false; }
   379     ThreadFunctionResult threadFunction() { QTest::qSleep(sleepTime); return ThreadFinished; }
   379     ThreadFunctionResult threadFunction() { QTest::qSleep(sleepTime); return ThreadFinished; }
   380 };
   380 };
   381 
   381 
   382 void tst_threadengine::cancelQueuedSlowUser()
   382 void tst_QtConcurrentThreadEngine::cancelQueuedSlowUser()
   383 {
   383 {
   384     const int times = 100;
   384     const int times = 100;
   385 
   385 
   386     QTime t;
   386     QTime t;
   387     t.start();
   387     t.start();
   434         return ThreadFinished;
   434         return ThreadFinished;
   435     }
   435     }
   436     QThread *blockThread;
   436     QThread *blockThread;
   437 };
   437 };
   438 
   438 
   439 void tst_threadengine::exceptions()
   439 void tst_QtConcurrentThreadEngine::exceptions()
   440 {
   440 {
   441     // Asynchronous mode:
   441     // Asynchronous mode:
   442     {
   442     {
   443         bool caught = false;
   443         bool caught = false;
   444         try  {
   444         try  {
   525     }
   525     }
   526 }
   526 }
   527 
   527 
   528 #endif
   528 #endif
   529 
   529 
   530 QTEST_MAIN(tst_threadengine)
   530 QTEST_MAIN(tst_QtConcurrentThreadEngine)
   531 
   531 
   532 #include "tst_qtconcurrentthreadengine.moc"
   532 #include "tst_qtconcurrentthreadengine.moc"
   533 
   533 
   534 #else
   534 #else
   535 QTEST_NOOP_MAIN
   535 QTEST_NOOP_MAIN