1
0

JobListTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\BackgroundJob;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\IJob;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IConfig;
  12. use Psr\Log\LoggerInterface;
  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. private bool $ran = false;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->connection = \OC::$server->getDatabaseConnection();
  33. $this->clearJobsList();
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->timeFactory = $this->createMock(ITimeFactory::class);
  36. $this->instance = new \OC\BackgroundJob\JobList(
  37. $this->connection,
  38. $this->config,
  39. $this->timeFactory,
  40. \OC::$server->get(LoggerInterface::class),
  41. );
  42. }
  43. protected function clearJobsList() {
  44. $query = $this->connection->getQueryBuilder();
  45. $query->delete('jobs');
  46. $query->execute();
  47. }
  48. protected function getAllSorted() {
  49. $iterator = $this->instance->getJobsIterator(null, null, 0);
  50. $jobs = [];
  51. foreach ($iterator as $job) {
  52. $jobs[] = clone $job;
  53. }
  54. usort($jobs, function (IJob $job1, IJob $job2) {
  55. return $job1->getId() - $job2->getId();
  56. });
  57. return $jobs;
  58. }
  59. public function argumentProvider() {
  60. return [
  61. [null],
  62. [false],
  63. ['foobar'],
  64. [12],
  65. [[
  66. 'asd' => 5,
  67. 'foo' => 'bar'
  68. ]]
  69. ];
  70. }
  71. /**
  72. * @dataProvider argumentProvider
  73. * @param $argument
  74. */
  75. public function testAddRemove($argument) {
  76. $existingJobs = $this->getAllSorted();
  77. $job = new TestJob();
  78. $this->instance->add($job, $argument);
  79. $jobs = $this->getAllSorted();
  80. $this->assertCount(count($existingJobs) + 1, $jobs);
  81. $addedJob = $jobs[count($jobs) - 1];
  82. $this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
  83. $this->assertEquals($argument, $addedJob->getArgument());
  84. $this->instance->remove($job, $argument);
  85. $jobs = $this->getAllSorted();
  86. $this->assertEquals($existingJobs, $jobs);
  87. }
  88. /**
  89. * @dataProvider argumentProvider
  90. * @param $argument
  91. */
  92. public function testRemoveDifferentArgument($argument) {
  93. $existingJobs = $this->getAllSorted();
  94. $job = new TestJob();
  95. $this->instance->add($job, $argument);
  96. $jobs = $this->getAllSorted();
  97. $this->instance->remove($job, 10);
  98. $jobs2 = $this->getAllSorted();
  99. $this->assertEquals($jobs, $jobs2);
  100. $this->instance->remove($job, $argument);
  101. $jobs = $this->getAllSorted();
  102. $this->assertEquals($existingJobs, $jobs);
  103. }
  104. /**
  105. * @dataProvider argumentProvider
  106. * @param $argument
  107. */
  108. public function testHas($argument) {
  109. $job = new TestJob();
  110. $this->assertFalse($this->instance->has($job, $argument));
  111. $this->instance->add($job, $argument);
  112. $this->assertTrue($this->instance->has($job, $argument));
  113. $this->instance->remove($job, $argument);
  114. $this->assertFalse($this->instance->has($job, $argument));
  115. }
  116. /**
  117. * @dataProvider argumentProvider
  118. * @param $argument
  119. */
  120. public function testHasDifferentArgument($argument) {
  121. $job = new TestJob();
  122. $this->instance->add($job, $argument);
  123. $this->assertFalse($this->instance->has($job, 10));
  124. }
  125. protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
  126. if ($lastChecked === 0) {
  127. $lastChecked = time();
  128. }
  129. $query = $this->connection->getQueryBuilder();
  130. $query->insert('jobs')
  131. ->values([
  132. 'class' => $query->createNamedParameter($class),
  133. 'argument' => $query->createNamedParameter($argument),
  134. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  135. 'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
  136. 'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
  137. ]);
  138. $query->execute();
  139. }
  140. public function testGetNext() {
  141. $job = new TestJob();
  142. $this->createTempJob(get_class($job), 1, 0, 12345);
  143. $this->createTempJob(get_class($job), 2, 0, 12346);
  144. $jobs = $this->getAllSorted();
  145. $savedJob1 = $jobs[0];
  146. $this->timeFactory->expects($this->atLeastOnce())
  147. ->method('getTime')
  148. ->willReturn(123456789);
  149. $nextJob = $this->instance->getNext();
  150. $this->assertEquals($savedJob1, $nextJob);
  151. }
  152. public function testGetNextSkipReserved() {
  153. $job = new TestJob();
  154. $this->createTempJob(get_class($job), 1, 123456789, 12345);
  155. $this->createTempJob(get_class($job), 2, 0, 12346);
  156. $this->timeFactory->expects($this->atLeastOnce())
  157. ->method('getTime')
  158. ->willReturn(123456789);
  159. $nextJob = $this->instance->getNext();
  160. $this->assertEquals(get_class($job), get_class($nextJob));
  161. $this->assertEquals(2, $nextJob->getArgument());
  162. }
  163. public function testGetNextSkipNonExisting() {
  164. $job = new TestJob();
  165. $this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
  166. $this->createTempJob(get_class($job), 2, 0, 12346);
  167. $this->timeFactory->expects($this->atLeastOnce())
  168. ->method('getTime')
  169. ->willReturn(123456789);
  170. $nextJob = $this->instance->getNext();
  171. $this->assertEquals(get_class($job), get_class($nextJob));
  172. $this->assertEquals(2, $nextJob->getArgument());
  173. }
  174. /**
  175. * @dataProvider argumentProvider
  176. * @param $argument
  177. */
  178. public function testGetById($argument) {
  179. $job = new TestJob();
  180. $this->instance->add($job, $argument);
  181. $jobs = $this->getAllSorted();
  182. $addedJob = $jobs[count($jobs) - 1];
  183. $this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
  184. }
  185. public function testSetLastRun() {
  186. $job = new TestJob();
  187. $this->instance->add($job);
  188. $jobs = $this->getAllSorted();
  189. $addedJob = $jobs[count($jobs) - 1];
  190. $timeStart = time();
  191. $this->instance->setLastRun($addedJob);
  192. $timeEnd = time();
  193. $addedJob = $this->instance->getById($addedJob->getId());
  194. $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
  195. $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
  196. }
  197. public function testHasReservedJobs() {
  198. $this->clearJobsList();
  199. $this->timeFactory->expects($this->atLeastOnce())
  200. ->method('getTime')
  201. ->willReturn(123456789);
  202. $job = new TestJob($this->timeFactory, $this, function () {
  203. });
  204. $job2 = new TestJob($this->timeFactory, $this, function () {
  205. });
  206. $this->instance->add($job, 1);
  207. $this->instance->add($job2, 2);
  208. $this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
  209. $this->assertFalse($this->instance->hasReservedJob());
  210. $this->assertFalse($this->instance->hasReservedJob(TestJob::class));
  211. $job = $this->instance->getNext();
  212. $this->assertNotNull($job);
  213. $this->assertTrue($this->instance->hasReservedJob());
  214. $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
  215. $job = $this->instance->getNext();
  216. $this->assertNotNull($job);
  217. $this->assertTrue($this->instance->hasReservedJob());
  218. $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
  219. }
  220. public function testHasReservedJobsAndParallelAwareJob() {
  221. $this->clearJobsList();
  222. $this->timeFactory->expects($this->atLeastOnce())
  223. ->method('getTime')
  224. ->willReturnCallback(function () use (&$time) {
  225. return time();
  226. });
  227. $job = new TestParallelAwareJob($this->timeFactory, $this, function () {
  228. });
  229. $job2 = new TestParallelAwareJob($this->timeFactory, $this, function () {
  230. });
  231. $this->instance->add($job, 1);
  232. $this->instance->add($job2, 2);
  233. $this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
  234. $this->assertFalse($this->instance->hasReservedJob());
  235. $this->assertFalse($this->instance->hasReservedJob(TestParallelAwareJob::class));
  236. $job = $this->instance->getNext();
  237. $this->assertNotNull($job);
  238. $this->assertTrue($this->instance->hasReservedJob());
  239. $this->assertTrue($this->instance->hasReservedJob(TestParallelAwareJob::class));
  240. $job = $this->instance->getNext();
  241. $this->assertNull($job); // Job doesn't allow parallel runs
  242. }
  243. public function markRun() {
  244. $this->ran = true;
  245. }
  246. }