Git commit 6d9ec8b36f44cdb99b6c0da9dde4cf1f87a8f7cd by Michael Leupold. Committed on 10/08/2010 at 15:53. Pushed by vrusu into branch 'ksecretsservice'. Add a unit-test for the QueuedJob/JobQueue classes and fix a small bug while doing so. svn path=/trunk/playground/base/ksecretservice/; revision=1161638 M +1 -0 jobqueue.cpp A +132 -0 tests/queuedjobtest.cpp [License: GPL (v2/3)] A +62 -0 tests/queuedjobtest.h [License: GPL (v2/3)] M +3 -3 queuedjob.h M +2 -0 CMakeLists.txt A +9 -0 tests/CMakeLists.txt http://commits.kde.org/kdelibs/6d9ec8b36f44cdb99b6c0da9dde4cf1f87a8f7cd diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a28533..194c614 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,3 +6,5 @@ SET (ksecretservice_lib_SRCS KDE4_ADD_LIBRARY (ksecretservicelib STATIC ${ksecretservice_lib_SRCS}) TARGET_LINK_LIBRARIES (ksecretservicelib ${QT_QTCORE_LIBRARY}) + +ADD_SUBDIRECTORY (tests) \ No newline at end of file diff --git a/jobqueue.cpp b/jobqueue.cpp index 5cdee0c..e0bfb0a 100644 --- a/jobqueue.cpp +++ b/jobqueue.cpp @@ -66,6 +66,7 @@ void JobQueuePrivate::process() connect(m_currentJob.data(), SIGNAL(result(QueuedJob*)), SLOT(jobFinished(QueuedJob*))); + m_currentJob->start(); } void JobQueuePrivate::jobFinished(QueuedJob *job) diff --git a/queuedjob.h b/queuedjob.h index b0941b2..59dcb75 100644 --- a/queuedjob.h +++ b/queuedjob.h @@ -81,9 +81,9 @@ public: * not involve an event-loop and it should only be available if the job * advertised that it can be called immediately. * - * @note This method may emit the result but it doesn't have to. If it - * doesn't call emitResult() it has to ensure that the object gets - * deleted using deleteLater() by itself. + * @note This method has to call emitResult() before returning. Even if + * noone is interested in the signal it's used to set mark this + * job as finished. */ virtual void exec() = 0; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3b4d00a --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,9 @@ +INCLUDE_DIRECTORIES (${CMAKE_CURRENT_BINARY_DIR}) + +KDE4_ADD_EXECUTABLE (ksecretservice_lib_queuedjobtest queuedjobtest.cpp) +TARGET_LINK_LIBRARIES (ksecretservice_lib_queuedjobtest + ksecretservicelib + ${QT_QTTEST_LIBRARIES} +) + +ADD_TEST (QueuedJobTest ksecretservice_lib_queuedjobtest) diff --git a/tests/queuedjobtest.cpp b/tests/queuedjobtest.cpp new file mode 100644 index 0000000..a61ecec --- /dev/null +++ b/tests/queuedjobtest.cpp @@ -0,0 +1,132 @@ +/* + * Copyright 2010, Michael Leupold + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "queuedjobtest.h" +#include +#include +#include +#include + +TestJob::TestJob(JobQueue *queue) : QueuedJob(queue) +{ +} + +TestJob::~TestJob() +{ +} + +bool TestJob::isImmediate() const +{ + return true; +} + +void TestJob::exec() +{ + doWork(); +} + +void TestJob::start() +{ + QTimer::singleShot(0, this, SLOT(doWork())); +} + +void TestJob::doWork() +{ + // nothing to do really + emitResult(); +} + +void QueuedJobTest::initTestCase() +{ + m_queue = new JobQueue; +} + +void QueuedJobTest::testSync() +{ + QPointer job = new TestJob(m_queue); + QVERIFY(job->isImmediate()); + QVERIFY(!job->isFinished()); + job->exec(); + QVERIFY(job->isFinished()); + + // make sure the job gets deleted + QEventLoop loop; + QTimer::singleShot(0, &loop, SLOT(quit())); + loop.exec(); + QVERIFY(job.isNull()); +} + +void QueuedJobTest::testAsync() +{ + QPointer job = new TestJob(m_queue); + QEventLoop loop; + loop.connect(job, SIGNAL(result(QueuedJob*)), SLOT(quit())); + QVERIFY(!job->isFinished()); + job->enqueue(); + loop.exec(); + // if we end up here, the job will already be deleted + QVERIFY(job.isNull() || job->isFinished()); + if (!job.isNull()) { + // if the job wasn't deleted yet, make sure it's getting deleted + QTimer::singleShot(0, &loop, SLOT(quit())); + loop.exec(); + QVERIFY(job.isNull()); + } +} + +void QueuedJobTest::testAsyncOrder() +{ + QPointer job1 = new TestJob(m_queue); + QPointer job2 = new TestJob(m_queue); + QEventLoop loop1; + QEventLoop loop2; + loop1.connect(job1, SIGNAL(result(QueuedJob*)), SLOT(quit())); + loop2.connect(job2, SIGNAL(result(QueuedJob*)), SLOT(quit())); + job1->enqueue(); + job2->enqueue(); + loop1.exec(); + loop2.exec(); + QVERIFY(job1.isNull() || job1->isFinished()); + QVERIFY(job2.isNull() || job2->isFinished()); +} + +void QueuedJobTest::testAsyncOrderInFront() +{ + QPointer job1 = new TestJob(m_queue); + QPointer job2 = new TestJob(m_queue); + QEventLoop loop1; + QEventLoop loop2; + loop1.connect(job1, SIGNAL(result(QueuedJob*)), SLOT(quit())); + loop2.connect(job2, SIGNAL(result(QueuedJob*)), SLOT(quit())); + job1->enqueue(); + job2->enqueue(true); + loop2.exec(); + loop1.exec(); + QVERIFY(job1.isNull() || job1->isFinished()); + QVERIFY(job2.isNull() || job2->isFinished()); +} + +void QueuedJobTest::cleanupTestCase() +{ + delete m_queue; +} + +QTEST_MAIN(QueuedJobTest) +#include "queuedjobtest.moc" diff --git a/tests/queuedjobtest.h b/tests/queuedjobtest.h new file mode 100644 index 0000000..3729666 --- /dev/null +++ b/tests/queuedjobtest.h @@ -0,0 +1,62 @@ +/* + * Copyright 2010, Michael Leupold + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef QUEUEDJOBTEST_H +#define QUEUEDJOBTEST_H + +#include +#include + +class JobQueue; + +class TestJob : public QueuedJob +{ + Q_OBJECT + +public: + TestJob(JobQueue *queue); + virtual ~TestJob(); + virtual bool isImmediate() const; + virtual void exec(); + virtual void start(); + +protected Q_SLOTS: + void doWork(); +}; + +class QueuedJobTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + + void testSync(); + void testAsync(); + void testAsyncOrder(); + void testAsyncOrderInFront(); + + void cleanupTestCase(); + +private: + JobQueue *m_queue; +}; + +#endif // QUEUEDJOBTEST_H