2013-04-21 01:27:46 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 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;
|
|
|
|
|
2018-10-09 16:25:56 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
|
2013-04-21 01:27:46 +04:00
|
|
|
class TestTimedJob extends \OC\BackgroundJob\TimedJob {
|
2018-10-09 16:25:56 +03:00
|
|
|
/** @var bool */
|
|
|
|
public $ran = false;
|
2013-12-04 19:28:27 +04:00
|
|
|
|
2018-10-09 16:25:56 +03:00
|
|
|
public function __construct() {
|
2013-04-21 01:27:46 +04:00
|
|
|
$this->setInterval(10);
|
|
|
|
}
|
|
|
|
|
2013-04-21 01:51:58 +04:00
|
|
|
public function run($argument) {
|
2018-10-09 16:25:56 +03:00
|
|
|
$this->ran = true;
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:25:56 +03:00
|
|
|
class TestTimedJobNew extends \OCP\BackgroundJob\TimedJob {
|
|
|
|
/** @var bool */
|
|
|
|
public $ran = false;
|
2013-04-21 01:27:46 +04:00
|
|
|
|
2018-10-09 16:25:56 +03:00
|
|
|
public function __construct(ITimeFactory $timeFactory) {
|
|
|
|
parent::__construct($timeFactory);
|
|
|
|
$this->setInterval(10);
|
|
|
|
}
|
2013-12-04 19:28:27 +04:00
|
|
|
|
2018-10-09 16:25:56 +03:00
|
|
|
public function run($argument) {
|
|
|
|
$this->ran = true;
|
2013-12-04 19:28:27 +04:00
|
|
|
}
|
2018-10-09 16:25:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class TimedJobTest extends \Test\TestCase {
|
|
|
|
/** @var DummyJobList $jobList */
|
|
|
|
private $jobList;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $time;
|
2013-12-04 19:28:27 +04:00
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
protected function setup() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2013-04-21 01:27:46 +04:00
|
|
|
$this->jobList = new DummyJobList();
|
2018-10-09 16:25:56 +03:00
|
|
|
$this->time = \OC::$server->query(ITimeFactory::class);
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldRunAfterInterval() {
|
2018-10-09 16:25:56 +03:00
|
|
|
$job = new TestTimedJob();
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 12);
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertTrue($job->ran);
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldNotRunWithinInterval() {
|
2018-10-09 16:25:56 +03:00
|
|
|
$job = new TestTimedJob();
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 5);
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertFalse($job->ran);
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldNotTwice() {
|
2018-10-09 16:25:56 +03:00
|
|
|
$job = new TestTimedJob();
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 15);
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertTrue($job->ran);
|
|
|
|
$job->ran = false;
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertFalse($job->ran);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testShouldRunAfterIntervalNew() {
|
|
|
|
$job = new TestTimedJobNew($this->time);
|
|
|
|
$job->setId(42);
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 12);
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertTrue($job->ran);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldNotRunWithinIntervalNew() {
|
|
|
|
$job = new TestTimedJobNew($this->time);
|
|
|
|
$job->setId(42);
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 5);
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertFalse($job->ran);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldNotTwiceNew() {
|
|
|
|
$job = new TestTimedJobNew($this->time);
|
|
|
|
$job->setId(42);
|
|
|
|
$this->jobList->add($job);
|
|
|
|
|
|
|
|
$job->setLastRun(time() - 15);
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertTrue($job->ran);
|
|
|
|
$job->ran = false;
|
|
|
|
$job->execute($this->jobList);
|
|
|
|
$this->assertFalse($job->ran);
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|
|
|
|
}
|