1
0

JobListTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\BackgroundJob;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\IJob;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\IConfig;
  13. use Test\TestCase;
  14. /**
  15. * Class JobList
  16. *
  17. * @group DB
  18. * @package Test\BackgroundJob
  19. */
  20. class JobListTest extends TestCase {
  21. /** @var \OC\BackgroundJob\JobList */
  22. protected $instance;
  23. /** @var \OCP\IDBConnection */
  24. protected $connection;
  25. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $config;
  27. /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $timeFactory;
  29. protected function setUp() {
  30. parent::setUp();
  31. $this->connection = \OC::$server->getDatabaseConnection();
  32. $this->clearJobsList();
  33. $this->config = $this->createMock(IConfig::class);
  34. $this->timeFactory = $this->createMock(ITimeFactory::class);
  35. $this->instance = new \OC\BackgroundJob\JobList(
  36. $this->connection,
  37. $this->config,
  38. $this->timeFactory
  39. );
  40. }
  41. protected function clearJobsList() {
  42. $query = $this->connection->getQueryBuilder();
  43. $query->delete('jobs');
  44. $query->execute();
  45. }
  46. protected function getAllSorted() {
  47. $jobs = $this->instance->getAll();
  48. usort($jobs, function (IJob $job1, IJob $job2) {
  49. return $job1->getId() - $job2->getId();
  50. });
  51. return $jobs;
  52. }
  53. public function argumentProvider() {
  54. return array(
  55. array(null),
  56. array(false),
  57. array('foobar'),
  58. array(12),
  59. array(array(
  60. 'asd' => 5,
  61. 'foo' => 'bar'
  62. ))
  63. );
  64. }
  65. /**
  66. * @dataProvider argumentProvider
  67. * @param $argument
  68. */
  69. public function testAddRemove($argument) {
  70. $existingJobs = $this->getAllSorted();
  71. $job = new TestJob();
  72. $this->instance->add($job, $argument);
  73. $jobs = $this->getAllSorted();
  74. $this->assertCount(count($existingJobs) + 1, $jobs);
  75. $addedJob = $jobs[count($jobs) - 1];
  76. $this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
  77. $this->assertEquals($argument, $addedJob->getArgument());
  78. $this->instance->remove($job, $argument);
  79. $jobs = $this->getAllSorted();
  80. $this->assertEquals($existingJobs, $jobs);
  81. }
  82. /**
  83. * @dataProvider argumentProvider
  84. * @param $argument
  85. */
  86. public function testRemoveDifferentArgument($argument) {
  87. $existingJobs = $this->getAllSorted();
  88. $job = new TestJob();
  89. $this->instance->add($job, $argument);
  90. $jobs = $this->getAllSorted();
  91. $this->instance->remove($job, 10);
  92. $jobs2 = $this->getAllSorted();
  93. $this->assertEquals($jobs, $jobs2);
  94. $this->instance->remove($job, $argument);
  95. $jobs = $this->getAllSorted();
  96. $this->assertEquals($existingJobs, $jobs);
  97. }
  98. /**
  99. * @dataProvider argumentProvider
  100. * @param $argument
  101. */
  102. public function testHas($argument) {
  103. $job = new TestJob();
  104. $this->assertFalse($this->instance->has($job, $argument));
  105. $this->instance->add($job, $argument);
  106. $this->assertTrue($this->instance->has($job, $argument));
  107. $this->instance->remove($job, $argument);
  108. $this->assertFalse($this->instance->has($job, $argument));
  109. }
  110. /**
  111. * @dataProvider argumentProvider
  112. * @param $argument
  113. */
  114. public function testHasDifferentArgument($argument) {
  115. $job = new TestJob();
  116. $this->instance->add($job, $argument);
  117. $this->assertFalse($this->instance->has($job, 10));
  118. }
  119. public function testGetLastJob() {
  120. $this->config->expects($this->once())
  121. ->method('getAppValue')
  122. ->with('backgroundjob', 'lastjob', 0)
  123. ->will($this->returnValue(15));
  124. $this->assertEquals(15, $this->instance->getLastJob());
  125. }
  126. protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
  127. if ($lastChecked === 0) {
  128. $lastChecked = time();
  129. }
  130. $query = $this->connection->getQueryBuilder();
  131. $query->insert('jobs')
  132. ->values([
  133. 'class' => $query->createNamedParameter($class),
  134. 'argument' => $query->createNamedParameter($argument),
  135. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  136. 'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
  137. 'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
  138. ]);
  139. $query->execute();
  140. }
  141. public function testGetNext() {
  142. $job = new TestJob();
  143. $this->createTempJob(get_class($job), 1, 0, 12345);
  144. $this->createTempJob(get_class($job), 2, 0, 12346);
  145. $jobs = $this->getAllSorted();
  146. $savedJob1 = $jobs[0];
  147. $this->timeFactory->expects($this->atLeastOnce())
  148. ->method('getTime')
  149. ->willReturn(123456789);
  150. $nextJob = $this->instance->getNext();
  151. $this->assertEquals($savedJob1, $nextJob);
  152. }
  153. public function testGetNextSkipReserved() {
  154. $job = new TestJob();
  155. $this->createTempJob(get_class($job), 1, 123456789, 12345);
  156. $this->createTempJob(get_class($job), 2, 0, 12346);
  157. $this->timeFactory->expects($this->atLeastOnce())
  158. ->method('getTime')
  159. ->willReturn(123456789);
  160. $nextJob = $this->instance->getNext();
  161. $this->assertEquals(get_class($job), get_class($nextJob));
  162. $this->assertEquals(2, $nextJob->getArgument());
  163. }
  164. public function testGetNextSkipNonExisting() {
  165. $job = new TestJob();
  166. $this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
  167. $this->createTempJob(get_class($job), 2, 0, 12346);
  168. $this->timeFactory->expects($this->atLeastOnce())
  169. ->method('getTime')
  170. ->willReturn(123456789);
  171. $nextJob = $this->instance->getNext();
  172. $this->assertEquals(get_class($job), get_class($nextJob));
  173. $this->assertEquals(2, $nextJob->getArgument());
  174. }
  175. /**
  176. * @dataProvider argumentProvider
  177. * @param $argument
  178. */
  179. public function testGetById($argument) {
  180. $job = new TestJob();
  181. $this->instance->add($job, $argument);
  182. $jobs = $this->getAllSorted();
  183. $addedJob = $jobs[count($jobs) - 1];
  184. $this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
  185. }
  186. public function testSetLastRun() {
  187. $job = new TestJob();
  188. $this->instance->add($job);
  189. $jobs = $this->getAllSorted();
  190. $addedJob = $jobs[count($jobs) - 1];
  191. $timeStart = time();
  192. $this->instance->setLastRun($addedJob);
  193. $timeEnd = time();
  194. $addedJob = $this->instance->getById($addedJob->getId());
  195. $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
  196. $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
  197. }
  198. }