JobListTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $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 [
  55. [null],
  56. [false],
  57. ['foobar'],
  58. [12],
  59. [[
  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. protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
  120. if ($lastChecked === 0) {
  121. $lastChecked = time();
  122. }
  123. $query = $this->connection->getQueryBuilder();
  124. $query->insert('jobs')
  125. ->values([
  126. 'class' => $query->createNamedParameter($class),
  127. 'argument' => $query->createNamedParameter($argument),
  128. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  129. 'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
  130. 'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
  131. ]);
  132. $query->execute();
  133. }
  134. public function testGetNext() {
  135. $job = new TestJob();
  136. $this->createTempJob(get_class($job), 1, 0, 12345);
  137. $this->createTempJob(get_class($job), 2, 0, 12346);
  138. $jobs = $this->getAllSorted();
  139. $savedJob1 = $jobs[0];
  140. $this->timeFactory->expects($this->atLeastOnce())
  141. ->method('getTime')
  142. ->willReturn(123456789);
  143. $nextJob = $this->instance->getNext();
  144. $this->assertEquals($savedJob1, $nextJob);
  145. }
  146. public function testGetNextSkipReserved() {
  147. $job = new TestJob();
  148. $this->createTempJob(get_class($job), 1, 123456789, 12345);
  149. $this->createTempJob(get_class($job), 2, 0, 12346);
  150. $this->timeFactory->expects($this->atLeastOnce())
  151. ->method('getTime')
  152. ->willReturn(123456789);
  153. $nextJob = $this->instance->getNext();
  154. $this->assertEquals(get_class($job), get_class($nextJob));
  155. $this->assertEquals(2, $nextJob->getArgument());
  156. }
  157. public function testGetNextSkipNonExisting() {
  158. $job = new TestJob();
  159. $this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
  160. $this->createTempJob(get_class($job), 2, 0, 12346);
  161. $this->timeFactory->expects($this->atLeastOnce())
  162. ->method('getTime')
  163. ->willReturn(123456789);
  164. $nextJob = $this->instance->getNext();
  165. $this->assertEquals(get_class($job), get_class($nextJob));
  166. $this->assertEquals(2, $nextJob->getArgument());
  167. }
  168. /**
  169. * @dataProvider argumentProvider
  170. * @param $argument
  171. */
  172. public function testGetById($argument) {
  173. $job = new TestJob();
  174. $this->instance->add($job, $argument);
  175. $jobs = $this->getAllSorted();
  176. $addedJob = $jobs[count($jobs) - 1];
  177. $this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
  178. }
  179. public function testSetLastRun() {
  180. $job = new TestJob();
  181. $this->instance->add($job);
  182. $jobs = $this->getAllSorted();
  183. $addedJob = $jobs[count($jobs) - 1];
  184. $timeStart = time();
  185. $this->instance->setLastRun($addedJob);
  186. $timeEnd = time();
  187. $addedJob = $this->instance->getById($addedJob->getId());
  188. $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
  189. $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
  190. }
  191. }