File indexing completed on 2024-09-08 07:32:40
0001 /*************************************************************************** 0002 * Copyright (C) 2011 Matthias Fuchs <mat69@gmx.net> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * 0018 ***************************************************************************/ 0019 0020 #include "schedulertest.h" 0021 #include "../core/scheduler.h" 0022 #include "../settings.h" 0023 0024 #include <QVariant> 0025 #include <QtTest> 0026 0027 Q_DECLARE_METATYPE(Job::ErrorType) 0028 Q_DECLARE_METATYPE(Job::Status) 0029 Q_DECLARE_METATYPE(Job::Policy) 0030 Q_DECLARE_METATYPE(QList<Job::Status>) 0031 Q_DECLARE_METATYPE(QList<Job::Policy>) 0032 0033 const int SchedulerTest::NO_LIMIT = 0; 0034 0035 SettingsHelper::SettingsHelper(int limit) 0036 : m_oldLimit(Settings::maxConnections()) 0037 { 0038 Settings::setMaxConnections(limit); 0039 } 0040 0041 SettingsHelper::~SettingsHelper() 0042 { 0043 Settings::setMaxConnections(m_oldLimit); 0044 } 0045 0046 TestJob::TestJob(Scheduler *scheduler, JobQueue *parent) 0047 : Job(scheduler, parent) 0048 { 0049 } 0050 0051 void TestJob::start() 0052 { 0053 if (status() == Aborted || status() == Stopped) { 0054 setStatus(Running); 0055 } 0056 } 0057 0058 void TestJob::stop() 0059 { 0060 if ((status() == Running) || (status() == Aborted) || (status() == Moving)) { 0061 setStatus(Stopped); 0062 } 0063 } 0064 0065 int TestJob::elapsedTime() const 0066 { 0067 return 0; 0068 } 0069 0070 int TestJob::remainingTime() const 0071 { 0072 return 0; 0073 } 0074 0075 bool TestJob::isStalled() const 0076 { 0077 return false; 0078 } 0079 0080 bool TestJob::isWorking() const 0081 { 0082 return true; 0083 } 0084 0085 TestQueue::TestQueue(Scheduler *scheduler) 0086 : JobQueue(scheduler) 0087 { 0088 } 0089 0090 void TestQueue::appendPub(Job *job) 0091 { 0092 append(job); 0093 } 0094 0095 void SchedulerTest::testAppendJobs() 0096 { 0097 QFETCH(int, limit); 0098 QFETCH(QList<Job::Status>, status); 0099 QFETCH(QList<Job::Status>, finalStatus); 0100 0101 SettingsHelper helper(limit); 0102 0103 Scheduler scheduler; 0104 auto *queue = new TestQueue(&scheduler); 0105 scheduler.addQueue(queue); 0106 0107 // uses an own list instead of the iterators to make sure that the order stays the same 0108 QList<TestJob *> jobs; 0109 for (int i = 0; i < status.size(); ++i) { 0110 auto *job = new TestJob(&scheduler, queue); 0111 job->setStatus(status[i]); 0112 queue->appendPub(job); 0113 jobs << job; 0114 } 0115 0116 for (int i = 0; i < status.size(); ++i) { 0117 QCOMPARE(jobs[i]->status(), finalStatus[i]); 0118 } 0119 } 0120 0121 void SchedulerTest::testAppendJobs_data() 0122 { 0123 QTest::addColumn<int>("limit"); 0124 QTest::addColumn<QList<Job::Status>>("status"); 0125 QTest::addColumn<QList<Job::Status>>("finalStatus"); 0126 0127 QTest::newRow("limit 2, two finished, will third be started?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0128 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0129 QTest::newRow("limit 2, two finished, will third aborted be started?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Aborted) 0130 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0131 QTest::newRow("limit 2, will first two start while last will stay stopped?") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0132 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Stopped); 0133 QTest::newRow("limit 2, will first two start while last will be stopped?") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Running) 0134 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Stopped); 0135 QTest::newRow("no limit, two finished, will third be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0136 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0137 QTest::newRow("no limit, will all three be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0138 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running); 0139 QTest::newRow("no limit, will all three be started and one remain running?") 0140 << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Running << Job::Stopped << Job::Stopped) 0141 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running << Job::Running); 0142 } 0143 0144 void SchedulerTest::testCountRunningJobs() 0145 { 0146 QFETCH(int, limit); 0147 QFETCH(QList<Job::Status>, status); 0148 QFETCH(int, numRunningJobs); 0149 0150 SettingsHelper helper(limit); 0151 0152 Scheduler scheduler; 0153 auto *queue = new TestQueue(&scheduler); 0154 scheduler.addQueue(queue); 0155 0156 // uses an own list instead of the iterators to make sure that the order stays the same 0157 for (int i = 0; i < status.size(); ++i) { 0158 auto *job = new TestJob(&scheduler, queue); 0159 job->setStatus(status[i]); 0160 queue->appendPub(job); 0161 } 0162 0163 QCOMPARE(scheduler.countRunningJobs(), numRunningJobs); 0164 0165 const bool hasRunningJobs = numRunningJobs; 0166 QCOMPARE(scheduler.hasRunningJobs(), hasRunningJobs); 0167 } 0168 0169 void SchedulerTest::testCountRunningJobs_data() 0170 { 0171 QTest::addColumn<int>("limit"); 0172 QTest::addColumn<QList<Job::Status>>("status"); 0173 QTest::addColumn<int>("numRunningJobs"); 0174 0175 QTest::newRow("limit 2, two finished, will third be started?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) << 1; 0176 QTest::newRow("limit 2, two finished, will none be started?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished) << 0; 0177 QTest::newRow("limit 2, will first two start while last will stay stopped?") 0178 << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) << 2; 0179 QTest::newRow("limit 2, will first two start while last will be stopped?") 0180 << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Running) << 2; 0181 QTest::newRow("no limit, two finished, will third be started?") 0182 << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) << 1; 0183 QTest::newRow("no limit, two finished, will third be started and fourth stay running?") 0184 << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped << Job::Running) << 2; 0185 QTest::newRow("no limit, will all three be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) << 3; 0186 } 0187 0188 void SchedulerTest::testStopScheduler() 0189 { 0190 QFETCH(int, limit); 0191 QFETCH(QList<Job::Status>, status); 0192 0193 SettingsHelper helper(limit); 0194 0195 Scheduler scheduler; 0196 auto *queue = new TestQueue(&scheduler); 0197 scheduler.addQueue(queue); 0198 0199 // uses an own list instead of the iterators to make sure that the order stays the same 0200 for (int i = 0; i < status.size(); ++i) { 0201 auto *job = new TestJob(&scheduler, queue); 0202 job->setStatus(status[i]); 0203 queue->appendPub(job); 0204 } 0205 0206 scheduler.stop(); 0207 0208 QCOMPARE(scheduler.countRunningJobs(), 0); 0209 } 0210 0211 void SchedulerTest::testStopScheduler_data() 0212 { 0213 QTest::addColumn<int>("limit"); 0214 QTest::addColumn<QList<Job::Status>>("status"); 0215 0216 QTest::newRow("limit 2, two finished one stopped") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped); 0217 QTest::newRow("limit 2, two finished one running") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped); 0218 QTest::newRow("limit 2, three stopped") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped); 0219 QTest::newRow("limit 2, two stopped one running") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Running); 0220 QTest::newRow("no limit, two finished one stopped") << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped); 0221 QTest::newRow("no limit, three stopped") << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped); 0222 QTest::newRow("no limit, one running, three stopped") << NO_LIMIT << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Stopped << Job::Stopped); 0223 } 0224 0225 void SchedulerTest::testSchedulerStopStart() 0226 { 0227 QFETCH(int, limit); 0228 QFETCH(QList<Job::Status>, status); 0229 QFETCH(QList<Job::Status>, finalStatus); 0230 0231 SettingsHelper helper(limit); 0232 0233 Scheduler scheduler; 0234 auto *queue = new TestQueue(&scheduler); 0235 scheduler.addQueue(queue); 0236 0237 // uses an own list instead of the iterators to make sure that the order stays the same 0238 QList<TestJob *> jobs; 0239 for (int i = 0; i < status.size(); ++i) { 0240 auto *job = new TestJob(&scheduler, queue); 0241 job->setStatus(status[i]); 0242 queue->appendPub(job); 0243 jobs << job; 0244 } 0245 0246 scheduler.stop(); 0247 scheduler.start(); 0248 0249 for (int i = 0; i < status.size(); ++i) { 0250 QCOMPARE(jobs[i]->status(), finalStatus[i]); 0251 } 0252 } 0253 0254 void SchedulerTest::testSchedulerStopStart_data() 0255 { 0256 QTest::addColumn<int>("limit"); 0257 QTest::addColumn<QList<Job::Status>>("status"); 0258 QTest::addColumn<QList<Job::Status>>("finalStatus"); 0259 0260 QTest::newRow("limit 2, two finished, will third be started?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0261 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0262 QTest::newRow("limit 2, will first two start while last will stay stopped?") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0263 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Stopped); 0264 QTest::newRow("limit 2, will first two start while last will be stopped?") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Running) 0265 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Stopped); 0266 QTest::newRow("no limit, two finished, will third be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0267 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0268 QTest::newRow("no limit, will all three be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0269 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running); 0270 QTest::newRow("limit 2, two finished, will third stay running?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running) 0271 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0272 } 0273 0274 void SchedulerTest::testSuspendScheduler() 0275 { 0276 QFETCH(int, limit); 0277 QFETCH(QList<Job::Status>, status); 0278 QFETCH(QList<Job::Status>, finalStatus); 0279 0280 SettingsHelper helper(limit); 0281 0282 Scheduler scheduler; 0283 auto *queue = new TestQueue(&scheduler); 0284 scheduler.addQueue(queue); 0285 scheduler.setIsSuspended(true); 0286 0287 // uses an own list instead of the iterators to make sure that the order stays the same 0288 QList<TestJob *> jobs; 0289 for (int i = 0; i < status.size(); ++i) { 0290 auto *job = new TestJob(&scheduler, queue); 0291 job->setStatus(status[i]); 0292 queue->appendPub(job); 0293 jobs << job; 0294 } 0295 0296 // scheduler is suspended thus the status has to be same as start status 0297 for (int i = 0; i < status.size(); ++i) { 0298 QCOMPARE(jobs[i]->status(), status[i]); 0299 } 0300 scheduler.setIsSuspended(false); 0301 0302 for (int i = 0; i < status.size(); ++i) { 0303 QCOMPARE(jobs[i]->status(), finalStatus[i]); 0304 } 0305 } 0306 0307 void SchedulerTest::testSuspendScheduler_data() 0308 { 0309 QTest::addColumn<int>("limit"); 0310 QTest::addColumn<QList<Job::Status>>("status"); 0311 QTest::addColumn<QList<Job::Status>>("finalStatus"); 0312 0313 // NOTE Scheduler does not stop jobs, it just prevents new ones from being started 0314 QTest::newRow("limit 2, two finished, will third be started?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0315 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0316 QTest::newRow("limit 2, will first two start while last will stay stopped?") << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0317 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Stopped); 0318 QTest::newRow("limit 2, will first start and second not while last will stay running?") 0319 << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Running) 0320 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Stopped); 0321 QTest::newRow("no limit, two finished, will third be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0322 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0323 QTest::newRow("no limit, will all three be started?") << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0324 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running); 0325 QTest::newRow("limit 2, two finished, will third stay running?") << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running) 0326 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0327 } 0328 0329 void SchedulerTest::testJobQueueStopPolicy() 0330 { 0331 QFETCH(int, limit); 0332 QFETCH(QList<Job::Status>, status); 0333 QFETCH(QList<Job::Status>, finalStatus); 0334 QFETCH(QList<Job::Policy>, policy); 0335 0336 SettingsHelper helper(limit); 0337 0338 Scheduler scheduler; 0339 auto *queue = new TestQueue(&scheduler); 0340 queue->setStatus(JobQueue::Stopped); 0341 scheduler.addQueue(queue); 0342 0343 // uses an own list instead of the iterators to make sure that the order stays the same 0344 QList<TestJob *> jobs; 0345 for (int i = 0; i < status.size(); ++i) { 0346 auto *job = new TestJob(&scheduler, queue); 0347 job->setStatus(status[i]); 0348 job->setPolicy(policy[i]); 0349 queue->appendPub(job); 0350 jobs << job; 0351 } 0352 0353 for (int i = 0; i < status.size(); ++i) { 0354 QCOMPARE(jobs[i]->status(), finalStatus[i]); 0355 } 0356 } 0357 0358 void SchedulerTest::testJobQueueStopPolicy_data() 0359 { 0360 QTest::addColumn<int>("limit"); 0361 QTest::addColumn<QList<Job::Status>>("status"); 0362 QTest::addColumn<QList<Job::Status>>("finalStatus"); 0363 QTest::addColumn<QList<Job::Policy>>("policy"); 0364 0365 QTest::newRow("limit 2, two finished, will third not be started?") 0366 << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0367 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) << (QList<Job::Policy>() << Job::None << Job::None << Job::None); 0368 QTest::newRow("limit 2, will first start while rest will stay stopped?") 0369 << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Stopped) 0370 << (QList<Job::Policy>() << Job::Start << Job::Stop << Job::None); 0371 QTest::newRow("limit 2, will first and third start while rest will stay stopped?") 0372 << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped << Job::Stopped) 0373 << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Running << Job::Stopped) 0374 << (QList<Job::Policy>() << Job::Start << Job::Stop << Job::Start << Job::None); 0375 QTest::newRow("no limit, two finished, will third be started?") 0376 << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0377 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running) << (QList<Job::Policy>() << Job::Start << Job::None << Job::Start); 0378 QTest::newRow("no limit, will all three be started?") 0379 << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0380 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running) << (QList<Job::Policy>() << Job::Start << Job::Start << Job::Start); 0381 } 0382 0383 void SchedulerTest::testJobQueueStopStartPolicy() 0384 { 0385 QFETCH(int, limit); 0386 QFETCH(QList<Job::Status>, status); 0387 QFETCH(QList<Job::Status>, intermediateStatus); 0388 QFETCH(QList<Job::Policy>, policy); 0389 QFETCH(QList<Job::Status>, finalStatus); 0390 0391 SettingsHelper helper(limit); 0392 0393 Scheduler scheduler; 0394 auto *queue = new TestQueue(&scheduler); 0395 queue->setStatus(JobQueue::Stopped); 0396 scheduler.addQueue(queue); 0397 0398 // uses an own list instead of the iterators to make sure that the order stays the same 0399 QList<TestJob *> jobs; 0400 for (int i = 0; i < status.size(); ++i) { 0401 auto *job = new TestJob(&scheduler, queue); 0402 job->setStatus(status[i]); 0403 job->setPolicy(policy[i]); 0404 queue->appendPub(job); 0405 jobs << job; 0406 } 0407 0408 for (int i = 0; i < status.size(); ++i) { 0409 QCOMPARE(jobs[i]->status(), intermediateStatus[i]); 0410 } 0411 0412 queue->setStatus(JobQueue::Running); 0413 0414 for (int i = 0; i < status.size(); ++i) { 0415 QCOMPARE(jobs[i]->status(), finalStatus[i]); 0416 } 0417 } 0418 0419 void SchedulerTest::testJobQueueStopStartPolicy_data() 0420 { 0421 QTest::addColumn<int>("limit"); 0422 QTest::addColumn<QList<Job::Status>>("status"); 0423 QTest::addColumn<QList<Job::Status>>("intermediateStatus"); 0424 QTest::addColumn<QList<Job::Policy>>("policy"); 0425 QTest::addColumn<QList<Job::Status>>("finalStatus"); 0426 0427 QTest::newRow("limit 2, two finished, will third be started?") 0428 << 2 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0429 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) << (QList<Job::Policy>() << Job::None << Job::None << Job::None) 0430 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0431 QTest::newRow("limit 2, will first and last start while rest will stay stopped?") 0432 << 2 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Stopped) 0433 << (QList<Job::Policy>() << Job::Start << Job::Stop << Job::None) << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Running); 0434 QTest::newRow("limit 3, will first, third and last start while rest will stay stopped?") 0435 << 3 << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped << Job::Stopped) 0436 << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Running << Job::Stopped) 0437 << (QList<Job::Policy>() << Job::Start << Job::Stop << Job::Start << Job::None) 0438 << (QList<Job::Status>() << Job::Running << Job::Stopped << Job::Running << Job::Running); 0439 QTest::newRow("no limit, two finished, will third be started?") 0440 << NO_LIMIT << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Stopped) 0441 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running) << (QList<Job::Policy>() << Job::Start << Job::Start << Job::Start) 0442 << (QList<Job::Status>() << Job::Finished << Job::Finished << Job::Running); 0443 QTest::newRow("no limit, will all three be started?") 0444 << NO_LIMIT << (QList<Job::Status>() << Job::Stopped << Job::Stopped << Job::Stopped) 0445 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running) << (QList<Job::Policy>() << Job::Start << Job::Start << Job::Start) 0446 << (QList<Job::Status>() << Job::Running << Job::Running << Job::Running); 0447 } 0448 0449 void SchedulerTest::testJobErrorType() 0450 { 0451 QFETCH(bool, jobQueueRunning); 0452 QFETCH(Job::Policy, policy); 0453 QFETCH(Job::ErrorType, errorType); 0454 QFETCH(Job::Status, finalStatus); 0455 0456 SettingsHelper helper(NO_LIMIT); 0457 0458 Scheduler scheduler; 0459 auto *queue = new TestQueue(&scheduler); 0460 queue->setStatus(jobQueueRunning ? JobQueue::Running : JobQueue::Stopped); 0461 scheduler.addQueue(queue); 0462 0463 auto *job = new TestJob(&scheduler, queue); 0464 job->setPolicy(policy); 0465 job->setError(QString(), QString(), errorType); 0466 queue->appendPub(job); 0467 0468 QCOMPARE(job->status(), finalStatus); 0469 } 0470 0471 void SchedulerTest::testJobErrorType_data() 0472 { 0473 QTest::addColumn<bool>("jobQueueRunning"); 0474 QTest::addColumn<Job::Policy>("policy"); 0475 QTest::addColumn<Job::ErrorType>("errorType"); 0476 QTest::addColumn<Job::Status>("finalStatus"); 0477 0478 QTest::newRow("queue running, job::none, autoretry, running") << true << Job::None << Job::AutomaticRetry << Job::Running; 0479 QTest::newRow("queue running, job::start, autoretry, running") << true << Job::Start << Job::AutomaticRetry << Job::Running; 0480 QTest::newRow("queue running, job::stop, autoretry, aborted") << true << Job::Stop << Job::AutomaticRetry << Job::Aborted; 0481 QTest::newRow("queue running, job::none, manualsolve, aborted") << true << Job::None << Job::ManualSolve << Job::Aborted; 0482 QTest::newRow("queue running, job::start, manualsolve, aborted") << true << Job::Start << Job::ManualSolve << Job::Aborted; 0483 QTest::newRow("queue running, job::stop, manualsolve, aborted") << true << Job::Stop << Job::ManualSolve << Job::Aborted; 0484 QTest::newRow("queue running, job::none, notsolveable, aborted") << true << Job::None << Job::NotSolveable << Job::Aborted; 0485 QTest::newRow("queue running, job::start, notsolveable, aborted") << true << Job::Start << Job::NotSolveable << Job::Aborted; 0486 QTest::newRow("queue running, job::stop, notsolveable, aborted") << true << Job::Stop << Job::NotSolveable << Job::Aborted; 0487 QTest::newRow("queue stopped, job::none, autoretry, aborted") << false << Job::None << Job::AutomaticRetry << Job::Aborted; 0488 QTest::newRow("queue stopped, job::start, autoretry, running") << false << Job::Start << Job::AutomaticRetry << Job::Running; 0489 QTest::newRow("queue stopped, job::stop, autoretry, aborted") << false << Job::Stop << Job::AutomaticRetry << Job::Aborted; 0490 QTest::newRow("queue stopped, job::none, manualsolve, aborted") << false << Job::None << Job::ManualSolve << Job::Aborted; 0491 QTest::newRow("queue stopped, job::start, manualsolve, aborted") << false << Job::Start << Job::ManualSolve << Job::Aborted; 0492 QTest::newRow("queue stopped, job::stop, manualsolve, aborted") << false << Job::Stop << Job::ManualSolve << Job::Aborted; 0493 QTest::newRow("queue stopped, job::none, notsolveable, aborted") << false << Job::None << Job::NotSolveable << Job::Aborted; 0494 QTest::newRow("queue stopped, job::start, notsolveable, aborted") << false << Job::Start << Job::NotSolveable << Job::Aborted; 0495 QTest::newRow("queue stopped, job::stop, notsolveable, aborted") << false << Job::Stop << Job::NotSolveable << Job::Aborted; 0496 } 0497 0498 void SchedulerTest::testGettingNetworkConnection() 0499 { 0500 QFETCH(Job::Policy, policy); 0501 QFETCH(Job::Status, finalStatus); 0502 0503 SettingsHelper helper(NO_LIMIT); 0504 0505 Scheduler scheduler; 0506 auto *queue = new TestQueue(&scheduler); 0507 scheduler.addQueue(queue); 0508 scheduler.setHasNetworkConnection(false); 0509 0510 auto *job = new TestJob(&scheduler, queue); 0511 job->setPolicy(policy); 0512 queue->appendPub(job); 0513 0514 scheduler.setHasNetworkConnection(true); 0515 0516 QCOMPARE(job->policy(), policy); // the policy should remain unchanged 0517 QCOMPARE(job->status(), finalStatus); 0518 } 0519 0520 void SchedulerTest::testGettingNetworkConnection_data() 0521 { 0522 QTest::addColumn<Job::Policy>("policy"); 0523 QTest::addColumn<Job::Status>("finalStatus"); 0524 0525 QTest::newRow("Start, Running") << Job::Start << Job::Running; 0526 QTest::newRow("Stop, Stopped") << Job::Stop << Job::Stopped; 0527 QTest::newRow("None, Running") << Job::None << Job::Running; 0528 } 0529 0530 void SchedulerTest::testLosingNetworkConnection() 0531 { 0532 QFETCH(Job::Policy, policy); 0533 0534 SettingsHelper helper(NO_LIMIT); 0535 0536 Scheduler scheduler; 0537 auto *queue = new TestQueue(&scheduler); 0538 scheduler.addQueue(queue); 0539 scheduler.setHasNetworkConnection(true); 0540 0541 auto *job = new TestJob(&scheduler, queue); 0542 job->setPolicy(policy); 0543 queue->appendPub(job); 0544 0545 scheduler.setHasNetworkConnection(false); 0546 0547 QCOMPARE(job->policy(), policy); // the policy should remain unchanged 0548 QCOMPARE(job->status(), Job::Stopped); 0549 } 0550 0551 void SchedulerTest::testLosingNetworkConnection_data() 0552 { 0553 QTest::addColumn<Job::Policy>("policy"); 0554 0555 QTest::newRow("Start") << Job::Start; 0556 QTest::newRow("Stop") << Job::Stop; 0557 QTest::newRow("None") << Job::None; 0558 } 0559 0560 void SchedulerTest::testShouldUpdate() 0561 { 0562 QFETCH(bool, isSuspended); 0563 QFETCH(bool, hasNetworkConnection); 0564 QFETCH(bool, shouldUpdate); 0565 0566 SettingsHelper helper(NO_LIMIT); 0567 0568 Scheduler scheduler; 0569 auto *queue = new TestQueue(&scheduler); 0570 scheduler.addQueue(queue); 0571 0572 QVERIFY(scheduler.shouldUpdate()); // should be true by default 0573 0574 scheduler.setIsSuspended(isSuspended); 0575 scheduler.setHasNetworkConnection(hasNetworkConnection); 0576 0577 QCOMPARE(scheduler.shouldUpdate(), shouldUpdate); 0578 } 0579 0580 void SchedulerTest::testShouldUpdate_data() 0581 { 0582 QTest::addColumn<bool>("isSuspended"); 0583 QTest::addColumn<bool>("hasNetworkConnection"); 0584 QTest::addColumn<bool>("shouldUpdate"); 0585 0586 QTest::newRow("false, true, true") << false << true << true; 0587 QTest::newRow("true, true, false") << true << true << false; 0588 QTest::newRow("false, false, false") << false << false << false; 0589 } 0590 0591 QTEST_MAIN(SchedulerTest) 0592 0593 #include "moc_schedulertest.cpp"