2014-02-12 16:52:13 +04:00
|
|
|
<?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;
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
use OCP\BackgroundJob\IJob;
|
2015-11-03 03:52:41 +03:00
|
|
|
use Test\TestCase;
|
2015-11-19 11:25:34 +03:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
|
|
|
* Class JobList
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
* @package Test\BackgroundJob
|
|
|
|
*/
|
|
|
|
class JobList extends TestCase {
|
2015-12-18 11:47:54 +03:00
|
|
|
/** @var \OC\BackgroundJob\JobList */
|
2014-02-12 16:52:13 +04:00
|
|
|
protected $instance;
|
|
|
|
|
2015-12-18 11:47:54 +03:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2014-02-12 16:52:13 +04:00
|
|
|
protected $config;
|
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-12-18 11:47:54 +03:00
|
|
|
$connection = \OC::$server->getDatabaseConnection();
|
2014-02-12 16:52:13 +04:00
|
|
|
$this->config = $this->getMock('\OCP\IConfig');
|
2015-12-18 11:47:54 +03:00
|
|
|
$this->instance = new \OC\BackgroundJob\JobList($connection, $this->config);
|
2014-02-12 16:52:13 +04:00
|
|
|
}
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
protected function getAllSorted() {
|
|
|
|
$jobs = $this->instance->getAll();
|
|
|
|
|
|
|
|
usort($jobs, function (IJob $job1, IJob $job2) {
|
|
|
|
return $job1->getId() - $job2->getId();
|
|
|
|
});
|
|
|
|
|
|
|
|
return $jobs;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:52:13 +04:00
|
|
|
public function argumentProvider() {
|
|
|
|
return array(
|
|
|
|
array(null),
|
|
|
|
array(false),
|
|
|
|
array('foobar'),
|
|
|
|
array(12),
|
|
|
|
array(array(
|
|
|
|
'asd' => 5,
|
|
|
|
'foo' => 'bar'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testAddRemove($argument) {
|
2015-11-19 11:25:34 +03:00
|
|
|
$existingJobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
|
|
|
|
$this->assertCount(count($existingJobs) + 1, $jobs);
|
|
|
|
$addedJob = $jobs[count($jobs) - 1];
|
|
|
|
$this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
|
|
|
|
$this->assertEquals($argument, $addedJob->getArgument());
|
|
|
|
|
|
|
|
$this->instance->remove($job, $argument);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
$this->assertEquals($existingJobs, $jobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testRemoveDifferentArgument($argument) {
|
2015-11-19 11:25:34 +03:00
|
|
|
$existingJobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
$this->instance->remove($job, 10);
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs2 = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
|
|
|
|
$this->assertEquals($jobs, $jobs2);
|
|
|
|
|
|
|
|
$this->instance->remove($job, $argument);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
$this->assertEquals($existingJobs, $jobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testHas($argument) {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->assertFalse($this->instance->has($job, $argument));
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->has($job, $argument));
|
|
|
|
|
|
|
|
$this->instance->remove($job, $argument);
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->has($job, $argument));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testHasDifferentArgument($argument) {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->has($job, 10));
|
|
|
|
|
|
|
|
$this->instance->remove($job, $argument);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLastJob() {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('backgroundjob', 'lastjob', 0)
|
|
|
|
->will($this->returnValue(15));
|
|
|
|
|
|
|
|
$this->assertEquals(15, $this->instance->getLastJob());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNext() {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, 1);
|
|
|
|
$this->instance->add($job, 2);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
|
|
|
|
$savedJob1 = $jobs[count($jobs) - 2];
|
|
|
|
$savedJob2 = $jobs[count($jobs) - 1];
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('backgroundjob', 'lastjob', 0)
|
|
|
|
->will($this->returnValue($savedJob1->getId()));
|
|
|
|
|
|
|
|
$nextJob = $this->instance->getNext();
|
|
|
|
|
|
|
|
$this->assertEquals($savedJob2, $nextJob);
|
|
|
|
|
|
|
|
$this->instance->remove($job, 1);
|
|
|
|
$this->instance->remove($job, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNextWrapAround() {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, 1);
|
|
|
|
$this->instance->add($job, 2);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
|
|
|
|
$savedJob2 = $jobs[count($jobs) - 1];
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('backgroundjob', 'lastjob', 0)
|
|
|
|
->will($this->returnValue($savedJob2->getId()));
|
|
|
|
|
|
|
|
$nextJob = $this->instance->getNext();
|
|
|
|
|
|
|
|
$this->assertEquals($jobs[0], $nextJob);
|
|
|
|
|
|
|
|
$this->instance->remove($job, 1);
|
|
|
|
$this->instance->remove($job, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testGetById($argument) {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
|
|
|
|
$addedJob = $jobs[count($jobs) - 1];
|
|
|
|
|
|
|
|
$this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
|
|
|
|
|
|
|
|
$this->instance->remove($job, $argument);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetLastRun() {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 16:52:13 +04:00
|
|
|
|
|
|
|
$addedJob = $jobs[count($jobs) - 1];
|
|
|
|
|
|
|
|
$timeStart = time();
|
|
|
|
$this->instance->setLastRun($addedJob);
|
|
|
|
$timeEnd = time();
|
|
|
|
|
|
|
|
$addedJob = $this->instance->getById($addedJob->getId());
|
|
|
|
|
|
|
|
$this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
|
|
|
|
$this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
|
|
|
|
|
|
|
|
$this->instance->remove($job);
|
|
|
|
}
|
2015-08-19 15:16:05 +03:00
|
|
|
|
|
|
|
public function testGetNextNonExisting() {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, 1);
|
|
|
|
$this->instance->add('\OC\Non\Existing\Class');
|
|
|
|
$this->instance->add($job, 2);
|
|
|
|
|
2015-11-19 11:25:34 +03:00
|
|
|
$jobs = $this->getAllSorted();
|
2015-08-19 15:16:05 +03:00
|
|
|
|
|
|
|
$savedJob1 = $jobs[count($jobs) - 2];
|
|
|
|
$savedJob2 = $jobs[count($jobs) - 1];
|
|
|
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('backgroundjob', 'lastjob', 0)
|
|
|
|
->will($this->returnValue($savedJob1->getId()));
|
|
|
|
|
|
|
|
$this->instance->getNext();
|
|
|
|
|
|
|
|
$nextJob = $this->instance->getNext();
|
|
|
|
|
|
|
|
$this->assertEquals($savedJob2, $nextJob);
|
|
|
|
|
|
|
|
$this->instance->remove($job, 1);
|
|
|
|
$this->instance->remove($job, 2);
|
|
|
|
}
|
2014-02-12 16:52:13 +04:00
|
|
|
}
|