瀏覽代碼

test(BackgroundJobs): Add tests for allowParallelRuns and hasReservedJobs

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
tags/v27.0.0beta1
Marcel Klehr 1 年之前
父節點
當前提交
1296f3612e

+ 1
- 1
lib/private/BackgroundJob/JobList.php 查看文件

@@ -384,7 +384,7 @@ class JobList implements IJobList {
$query->executeStatement();
}

public function hasReservedJob(?string $className): bool {
public function hasReservedJob(?string $className = null): bool {
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('jobs')

+ 13
- 0
tests/lib/BackgroundJob/DummyJobList.php 查看文件

@@ -21,6 +21,11 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
*/
private array $jobs = [];

/**
* @var bool[]
*/
private array $reserved = [];

private int $last = 0;

public function __construct() {
@@ -135,6 +140,14 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
$job->setLastRun(time());
}

public function hasReservedJob(?string $className = null): bool {
return $this->reserved[$className];
}

public function setHasReservedJob(?string $className, bool $hasReserved): void {
$this->reserved[$className] = $hasReserved;
}

public function setExecutionTime(IJob $job, $timeTaken): void {
}


+ 13
- 0
tests/lib/BackgroundJob/JobListTest.php 查看文件

@@ -244,4 +244,17 @@ class JobListTest extends TestCase {
$this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
$this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
}

public function testHasReservedJobs() {
$job = new TestJob();
$this->instance->add($job);

$this->assertFalse($this->instance->hasReservedJob());
$this->assertFalse($this->instance->hasReservedJob(TestJob::class));

$job->start($this->instance);

$this->assertTrue($this->instance->hasReservedJob());
$this->assertTrue($this->instance->hasReservedJob(TestJob::class));
}
}

+ 74
- 2
tests/lib/BackgroundJob/JobTest.php 查看文件

@@ -8,20 +8,23 @@

namespace Test\BackgroundJob;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ILogger;

class JobTest extends \Test\TestCase {
private $run = false;
private ITimeFactory $timeFactory;

protected function setUp(): void {
parent::setUp();
$this->run = false;
$this->timeFactory = \OC::$server->get(ITimeFactory::class);
}

public function testRemoveAfterException() {
$jobList = new DummyJobList();
$e = new \Exception();
$job = new TestJob($this, function () use ($e) {
$job = new TestJob($this->timeFactory, $this, function () use ($e) {
throw $e;
});
$jobList->add($job);
@@ -41,7 +44,7 @@ class JobTest extends \Test\TestCase {

public function testRemoveAfterError() {
$jobList = new DummyJobList();
$job = new TestJob($this, function () {
$job = new TestJob($this->timeFactory, $this, function () {
$test = null;
$test->someMethod();
});
@@ -60,6 +63,75 @@ class JobTest extends \Test\TestCase {
$this->assertCount(1, $jobList->getAll());
}

public function testRemoveAfterError() {
$jobList = new DummyJobList();
$job = new TestJob($this->timeFactory, $this, function () {
$test = null;
$test->someMethod();
});
$jobList->add($job);

$logger = $this->getMockBuilder(ILogger::class)
->disableOriginalConstructor()
->getMock();
$logger->expects($this->once())
->method('logException')
->with($this->isInstanceOf(\Throwable::class));

$this->assertCount(1, $jobList->getAll());
$job->execute($jobList, $logger);
$this->assertTrue($this->run);
$this->assertCount(1, $jobList->getAll());
}

public function testDisallowParallelRunsWithNoOtherJobs() {
$jobList = new DummyJobList();
$job = new TestJob($this->timeFactory, $this);
$job->setAllowParallelRuns(false);
$jobList->add($job);

$jobList->setHasReservedJob(null, false);
$jobList->setHasReservedJob(TestJob::class, false);
$job->start($jobList);
$this->assertTrue($this->run);
}

public function testAllowParallelRunsWithNoOtherJobs() {
$jobList = new DummyJobList();
$job = new TestJob($this->timeFactory, $this);
$job->setAllowParallelRuns(true);
$jobList->add($job);

$jobList->setHasReservedJob(null, false);
$jobList->setHasReservedJob(TestJob::class, false);
$job->start($jobList);
$this->assertTrue($this->run);
}

public function testAllowParallelRunsWithOtherJobs() {
$jobList = new DummyJobList();
$job = new TestJob($this->timeFactory, $this);
$job->setAllowParallelRuns(true);
$jobList->add($job);

$jobList->setHasReservedJob(null, true);
$jobList->setHasReservedJob(TestJob::class, true);
$job->start($jobList);
$this->assertTrue($this->run);
}

public function testDisallowParallelRunsWithOtherJobs() {
$jobList = new DummyJobList();
$job = new TestJob($this->timeFactory, $this);
$job->setAllowParallelRuns(false);
$jobList->add($job);

$jobList->setHasReservedJob(null, true);
$jobList->setHasReservedJob(TestJob::class, true);
$job->start($jobList);
$this->assertFalse($this->run);
}

public function markRun() {
$this->run = true;
}

+ 5
- 2
tests/lib/BackgroundJob/TestJob.php 查看文件

@@ -8,7 +8,9 @@

namespace Test\BackgroundJob;

class TestJob extends \OC\BackgroundJob\Job {
use OCP\AppFramework\Utility\ITimeFactory;

class TestJob extends \OCP\BackgroundJob\Job {
private $testCase;

/**
@@ -20,7 +22,8 @@ class TestJob extends \OC\BackgroundJob\Job {
* @param JobTest $testCase
* @param callable $callback
*/
public function __construct($testCase = null, $callback = null) {
public function __construct(ITimeFactory $time = null, $testCase = null, $callback = null) {
parent::__construct($time ?? \OC::$server->get(ITimeFactory::class));
$this->testCase = $testCase;
$this->callback = $callback;
}

Loading…
取消
儲存