summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2023-07-20 13:31:51 +0200
committerbackportbot-nextcloud[bot] <backportbot-nextcloud[bot]@users.noreply.github.com>2023-07-28 11:44:39 +0000
commited84fce879abb641029699c20c953d03bf2e6982 (patch)
tree3a49285b986a9f036c66c1d4090a29f35a04369a /tests
parent6d9cf9e2996ee3ffcd9eba824df3d1557f1b8f3f (diff)
downloadnextcloud-server-ed84fce879abb641029699c20c953d03bf2e6982.tar.gz
nextcloud-server-ed84fce879abb641029699c20c953d03bf2e6982.zip
Add test for ParallelAwareJob
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/BackgroundJob/JobListTest.php56
-rw-r--r--tests/lib/BackgroundJob/TestParallelAwareJob.php38
2 files changed, 89 insertions, 5 deletions
diff --git a/tests/lib/BackgroundJob/JobListTest.php b/tests/lib/BackgroundJob/JobListTest.php
index c15e556d5f7..32ee1e79a5e 100644
--- a/tests/lib/BackgroundJob/JobListTest.php
+++ b/tests/lib/BackgroundJob/JobListTest.php
@@ -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() {
diff --git a/tests/lib/BackgroundJob/TestParallelAwareJob.php b/tests/lib/BackgroundJob/TestParallelAwareJob.php
new file mode 100644
index 00000000000..24b883d2f7a
--- /dev/null
+++ b/tests/lib/BackgroundJob/TestParallelAwareJob.php
@@ -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);
+ }
+}