2013-04-20 17:27:46 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2013-04-20 17:27:46 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\BackgroundJob;
|
|
|
|
|
|
2018-10-09 09:25:56 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2018-10-09 09:25:56 -04:00
|
|
|
|
|
|
|
|
class TimedJobTest extends \Test\TestCase {
|
2024-02-08 05:52:40 -05:00
|
|
|
private DummyJobList $jobList;
|
|
|
|
|
private ITimeFactory $time;
|
2013-12-04 10:28:27 -05:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2013-04-20 17:27:46 -04:00
|
|
|
$this->jobList = new DummyJobList();
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->time = Server::get(ITimeFactory::class);
|
2013-04-20 17:27:46 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testShouldRunAfterIntervalNew(): void {
|
2018-10-09 09:25:56 -04:00
|
|
|
$job = new TestTimedJobNew($this->time);
|
|
|
|
|
$job->setId(42);
|
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 12);
|
2024-02-08 05:52:40 -05:00
|
|
|
$job->start($this->jobList);
|
2018-10-09 09:25:56 -04:00
|
|
|
$this->assertTrue($job->ran);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testShouldNotRunWithinIntervalNew(): void {
|
2018-10-09 09:25:56 -04:00
|
|
|
$job = new TestTimedJobNew($this->time);
|
|
|
|
|
$job->setId(42);
|
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 5);
|
2024-02-08 05:52:40 -05:00
|
|
|
$job->start($this->jobList);
|
2018-10-09 09:25:56 -04:00
|
|
|
$this->assertFalse($job->ran);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testShouldNotTwiceNew(): void {
|
2018-10-09 09:25:56 -04:00
|
|
|
$job = new TestTimedJobNew($this->time);
|
|
|
|
|
$job->setId(42);
|
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 15);
|
2024-02-08 05:52:40 -05:00
|
|
|
$job->start($this->jobList);
|
2018-10-09 09:25:56 -04:00
|
|
|
$this->assertTrue($job->ran);
|
|
|
|
|
$job->ran = false;
|
2024-02-08 05:52:40 -05:00
|
|
|
$job->start($this->jobList);
|
2018-10-09 09:25:56 -04:00
|
|
|
$this->assertFalse($job->ran);
|
2013-04-20 17:27:46 -04:00
|
|
|
}
|
|
|
|
|
}
|