Add test for ParallelAwareJob

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
pull/39473/head
Marcel Klehr 11 months ago
parent f3bdcfd427
commit 449a731375

@ -250,18 +250,64 @@ class JobListTest extends TestCase {
public function testHasReservedJobs() {
$this->clearJobsList();
$this->timeFactory->expects($this->atLeastOnce())
->method('getTime')
->willReturn(123456789);
$job = new TestJob($this->timeFactory, $this, function () {
$this->assertTrue($this->instance->hasReservedJob());
$this->assertTrue($this->instance->hasReservedJob(TestJob::class));
});
$this->instance->add($job);
$job2 = new TestJob($this->timeFactory, $this, function () {
});
$this->instance->add($job, 1);
$this->instance->add($job2, 2);
$this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
$this->assertFalse($this->instance->hasReservedJob());
$this->assertFalse($this->instance->hasReservedJob(TestJob::class));
$job->start($this->instance);
$job = $this->instance->getNext();
$this->assertNotNull($job);
$this->assertTrue($this->instance->hasReservedJob());
$this->assertTrue($this->instance->hasReservedJob(TestJob::class));
$job = $this->instance->getNext();
$this->assertNotNull($job);
$this->assertTrue($this->instance->hasReservedJob());
$this->assertTrue($this->instance->hasReservedJob(TestJob::class));
}
$this->assertTrue($this->ran);
public function testHasReservedJobsAndParallelAwareJob() {
$this->clearJobsList();
$this->timeFactory->expects($this->atLeastOnce())
->method('getTime')
->willReturnCallback(function () use (&$time) {
return time();
});
$job = new TestParallelAwareJob($this->timeFactory, $this, function () {
});
$job2 = new TestParallelAwareJob($this->timeFactory, $this, function () {
});
$this->instance->add($job, 1);
$this->instance->add($job2, 2);
$this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
$this->assertFalse($this->instance->hasReservedJob());
$this->assertFalse($this->instance->hasReservedJob(TestParallelAwareJob::class));
$job = $this->instance->getNext();
$this->assertNotNull($job);
$this->assertTrue($this->instance->hasReservedJob());
$this->assertTrue($this->instance->hasReservedJob(TestParallelAwareJob::class));
$job = $this->instance->getNext();
$this->assertNull($job); // Job doesn't allow parallel runs
}
public function markRun() {

@ -0,0 +1,38 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
use Test\BackgroundJob\JobTest;
class TestParallelAwareJob extends \OCP\BackgroundJob\Job {
private $testCase;
/**
* @var callable $callback
*/
private $callback;
/**
* @param JobTest $testCase
* @param callable $callback
*/
public function __construct(ITimeFactory $time = null, $testCase = null, $callback = null) {
parent::__construct($time ?? \OC::$server->get(ITimeFactory::class));
$this->setAllowParallelRuns(false);
$this->testCase = $testCase;
$this->callback = $callback;
}
public function run($argument) {
$this->testCase->markRun();
$callback = $this->callback;
$callback($argument);
}
}
Loading…
Cancel
Save