File indexing completed on 2024-05-12 05:52:46

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 #include "account/actions_p.h"
0006 
0007 #include "../test-utils/job.h"
0008 #include "../test-utils/spy.h"
0009 
0010 #include <QSignalSpy>
0011 #include <QTest>
0012 #include <QtDebug>
0013 
0014 class NextTest: public QObject
0015 {
0016     Q_OBJECT
0017 private Q_SLOTS:
0018     void coldNext(void);
0019 };
0020 
0021 void NextTest::coldNext(void)
0022 {
0023     test::JobSignals *jobSignals = new test::JobSignals(this);
0024 
0025     QThread *thread = new QThread(this);
0026     QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
0027     QObject::connect(jobSignals, &test::JobSignals::quit, thread, &QThread::quit);
0028 
0029     QSignalSpy threadStarted(thread, &QThread::started);
0030     QSignalSpy threadFinished(thread, &QThread::finished);
0031     QSignalSpy threadCleaned(thread, &QThread::destroyed);
0032 
0033     accounts::Dispatcher *uut = new accounts::Dispatcher(thread);
0034     QSignalSpy dispatcherDispatched(uut, &accounts::Dispatcher::dispatch);
0035 
0036     test::TestJob *firstJob = new test::TestJob();
0037     QSignalSpy firstJobRunning(firstJob, &test::TestJob::running);
0038     QSignalSpy firstJobFinished(firstJob, &test::TestJob::finished);
0039     QSignalSpy firstJobCleaned(firstJob, &test::TestJob::destroyed);
0040 
0041     uut->queueAndProceed(firstJob, [firstJob, thread, jobSignals](void) -> void
0042     {
0043         QCOMPARE(firstJob->thread(), thread);
0044         QObject::connect(jobSignals, &test::JobSignals::first, firstJob, &test::TestJob::finish);
0045     });
0046 
0047     test::TestJob *secondJob = new test::TestJob();
0048     QSignalSpy secondJobRunning(secondJob, &test::TestJob::running);
0049     QSignalSpy secondJobFinished(secondJob, &test::TestJob::finished);
0050     QSignalSpy secondJobCleaned(secondJob, &test::TestJob::destroyed);
0051 
0052     uut->queueAndProceed(secondJob, [secondJob, thread, jobSignals](void) -> void
0053     {
0054         QCOMPARE(secondJob->thread(), thread);
0055         QObject::connect(jobSignals, &test::JobSignals::second, secondJob, &test::TestJob::finish);
0056     });
0057 
0058     thread->start();
0059 
0060     QVERIFY2(test::signal_eventually_emitted_once(threadStarted), "worker thread should have started by now");
0061 
0062     QVERIFY2(test::signal_eventually_emitted_once(dispatcherDispatched), "dispatcher should have dispatched the first job by now");
0063 
0064     QVERIFY2(test::signal_eventually_emitted_once(firstJobRunning), "first job should be running by now");
0065     QCOMPARE(secondJobRunning.count(), 0); // and second job should not yet be
0066 
0067     Q_EMIT jobSignals->first();
0068     QCoreApplication::processEvents(QEventLoop::AllEvents, 500);
0069 
0070     QVERIFY2(test::signal_eventually_emitted_once(firstJobFinished), "first job should be finished by now");
0071     QVERIFY2(test::signal_eventually_emitted_once(firstJobCleaned), "first job should be disposed of by now");
0072 
0073     QVERIFY2(test::signal_eventually_emitted_twice(dispatcherDispatched), "dispatcher should have dispatched the second job by now");
0074     QVERIFY2(test::signal_eventually_emitted_once(secondJobRunning), "second job should be dispatched and running by now");
0075 
0076     Q_EMIT jobSignals->second();
0077     QCoreApplication::processEvents(QEventLoop::AllEvents, 500);
0078 
0079     QVERIFY2(test::signal_eventually_emitted_once(secondJobFinished), "second job should be finished by now");
0080     QVERIFY2(test::signal_eventually_emitted_once(secondJobCleaned), "second job should be disposed of by now");
0081 
0082     Q_EMIT jobSignals->quit();
0083     QCoreApplication::processEvents(QEventLoop::AllEvents, 500);
0084 
0085     QVERIFY2(test::signal_eventually_emitted_once(threadFinished), "worker thread should be finished by now");
0086     QVERIFY2(test::signal_eventually_emitted_once(threadCleaned), "thread should be disposed of by now");
0087 
0088     uut->deleteLater();
0089     jobSignals->deleteLater();
0090 }
0091 
0092 QTEST_MAIN(NextTest)
0093 
0094 #include "dispatcher-next.moc"