|
|
@@ -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; |
|
|
|
} |