JobListTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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(): void {
  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. $iterator = $this->instance->getJobsIterator(null, null, 0);
  48. $jobs = [];
  49. foreach ($iterator as $job) {
  50. $jobs[] = clone $job;
  51. }
  52. usort($jobs, function (IJob $job1, IJob $job2) {
  53. return $job1->getId() - $job2->getId();
  54. });
  55. return $jobs;
  56. }
  57. public function argumentProvider() {
  58. return [
  59. [null],
  60. [false],
  61. ['foobar'],
  62. [12],
  63. [[
  64. 'asd' => 5,
  65. 'foo' => 'bar'
  66. ]]
  67. ];
  68. }
  69. /**
  70. * @dataProvider argumentProvider
  71. * @param $argument
  72. */
  73. public function testAddRemove($argument) {
  74. $existingJobs = $this->getAllSorted();
  75. $job = new TestJob();
  76. $this->instance->add($job, $argument);
  77. $jobs = $this->getAllSorted();
  78. $this->assertCount(count($existingJobs) + 1, $jobs);
  79. $addedJob = $jobs[count($jobs) - 1];
  80. $this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
  81. $this->assertEquals($argument, $addedJob->getArgument());
  82. $this->instance->remove($job, $argument);
  83. $jobs = $this->getAllSorted();
  84. $this->assertEquals($existingJobs, $jobs);
  85. }
  86. /**
  87. * @dataProvider argumentProvider
  88. * @param $argument
  89. */
  90. public function testRemoveDifferentArgument($argument) {
  91. $existingJobs = $this->getAllSorted();
  92. $job = new TestJob();
  93. $this->instance->add($job, $argument);
  94. $jobs = $this->getAllSorted();
  95. $this->instance->remove($job, 10);
  96. $jobs2 = $this->getAllSorted();
  97. $this->assertEquals($jobs, $jobs2);
  98. $this->instance->remove($job, $argument);
  99. $jobs = $this->getAllSorted();
  100. $this->assertEquals($existingJobs, $jobs);
  101. }
  102. /**
  103. * @dataProvider argumentProvider
  104. * @param $argument
  105. */
  106. public function testHas($argument) {
  107. $job = new TestJob();
  108. $this->assertFalse($this->instance->has($job, $argument));
  109. $this->instance->add($job, $argument);
  110. $this->assertTrue($this->instance->has($job, $argument));
  111. $this->instance->remove($job, $argument);
  112. $this->assertFalse($this->instance->has($job, $argument));
  113. }
  114. /**
  115. * @dataProvider argumentProvider
  116. * @param $argument
  117. */
  118. public function testHasDifferentArgument($argument) {
  119. $job = new TestJob();
  120. $this->instance->add($job, $argument);
  121. $this->assertFalse($this->instance->has($job, 10));
  122. }
  123. protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
  124. if ($lastChecked === 0) {
  125. $lastChecked = time();
  126. }
  127. $query = $this->connection->getQueryBuilder();
  128. $query->insert('jobs')
  129. ->values([
  130. 'class' => $query->createNamedParameter($class),
  131. 'argument' => $query->createNamedParameter($argument),
  132. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  133. 'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
  134. 'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
  135. ]);
  136. $query->execute();
  137. }
  138. public function testGetNext() {
  139. $job = new TestJob();
  140. $this->createTempJob(get_class($job), 1, 0, 12345);
  141. $this->createTempJob(get_class($job), 2, 0, 12346);
  142. $jobs = $this->getAllSorted();
  143. $savedJob1 = $jobs[0];
  144. $this->timeFactory->expects($this->atLeastOnce())
  145. ->method('getTime')
  146. ->willReturn(123456789);
  147. $nextJob = $this->instance->getNext();
  148. $this->assertEquals($savedJob1, $nextJob);
  149. }
  150. public function testGetNextSkipReserved() {
  151. $job = new TestJob();
  152. $this->createTempJob(get_class($job), 1, 123456789, 12345);
  153. $this->createTempJob(get_class($job), 2, 0, 12346);
  154. $this->timeFactory->expects($this->atLeastOnce())
  155. ->method('getTime')
  156. ->willReturn(123456789);
  157. $nextJob = $this->instance->getNext();
  158. $this->assertEquals(get_class($job), get_class($nextJob));
  159. $this->assertEquals(2, $nextJob->getArgument());
  160. }
  161. public function testGetNextSkipNonExisting() {
  162. $job = new TestJob();
  163. $this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
  164. $this->createTempJob(get_class($job), 2, 0, 12346);
  165. $this->timeFactory->expects($this->atLeastOnce())
  166. ->method('getTime')
  167. ->willReturn(123456789);
  168. $nextJob = $this->instance->getNext();
  169. $this->assertEquals(get_class($job), get_class($nextJob));
  170. $this->assertEquals(2, $nextJob->getArgument());
  171. }
  172. /**
  173. * @dataProvider argumentProvider
  174. * @param $argument
  175. */
  176. public function testGetById($argument) {
  177. $job = new TestJob();
  178. $this->instance->add($job, $argument);
  179. $jobs = $this->getAllSorted();
  180. $addedJob = $jobs[count($jobs) - 1];
  181. $this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
  182. }
  183. public function testSetLastRun() {
  184. $job = new TestJob();
  185. $this->instance->add($job);
  186. $jobs = $this->getAllSorted();
  187. $addedJob = $jobs[count($jobs) - 1];
  188. $timeStart = time();
  189. $this->instance->setLastRun($addedJob);
  190. $timeEnd = time();
  191. $addedJob = $this->instance->getById($addedJob->getId());
  192. $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
  193. $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
  194. }
  195. }