JobListTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Psr\Log\LoggerInterface;
  14. use Test\TestCase;
  15. /**
  16. * Class JobList
  17. *
  18. * @group DB
  19. * @package Test\BackgroundJob
  20. */
  21. class JobListTest extends TestCase {
  22. /** @var \OC\BackgroundJob\JobList */
  23. protected $instance;
  24. /** @var \OCP\IDBConnection */
  25. protected $connection;
  26. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  27. protected $config;
  28. /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $timeFactory;
  30. private bool $ran = false;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->connection = \OC::$server->getDatabaseConnection();
  34. $this->clearJobsList();
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->timeFactory = $this->createMock(ITimeFactory::class);
  37. $this->instance = new \OC\BackgroundJob\JobList(
  38. $this->connection,
  39. $this->config,
  40. $this->timeFactory,
  41. \OC::$server->get(LoggerInterface::class),
  42. );
  43. }
  44. protected function clearJobsList() {
  45. $query = $this->connection->getQueryBuilder();
  46. $query->delete('jobs');
  47. $query->execute();
  48. }
  49. protected function getAllSorted() {
  50. $iterator = $this->instance->getJobsIterator(null, null, 0);
  51. $jobs = [];
  52. foreach ($iterator as $job) {
  53. $jobs[] = clone $job;
  54. }
  55. usort($jobs, function (IJob $job1, IJob $job2) {
  56. return $job1->getId() - $job2->getId();
  57. });
  58. return $jobs;
  59. }
  60. public function argumentProvider() {
  61. return [
  62. [null],
  63. [false],
  64. ['foobar'],
  65. [12],
  66. [[
  67. 'asd' => 5,
  68. 'foo' => 'bar'
  69. ]]
  70. ];
  71. }
  72. /**
  73. * @dataProvider argumentProvider
  74. * @param $argument
  75. */
  76. public function testAddRemove($argument) {
  77. $existingJobs = $this->getAllSorted();
  78. $job = new TestJob();
  79. $this->instance->add($job, $argument);
  80. $jobs = $this->getAllSorted();
  81. $this->assertCount(count($existingJobs) + 1, $jobs);
  82. $addedJob = $jobs[count($jobs) - 1];
  83. $this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
  84. $this->assertEquals($argument, $addedJob->getArgument());
  85. $this->instance->remove($job, $argument);
  86. $jobs = $this->getAllSorted();
  87. $this->assertEquals($existingJobs, $jobs);
  88. }
  89. /**
  90. * @dataProvider argumentProvider
  91. * @param $argument
  92. */
  93. public function testRemoveDifferentArgument($argument) {
  94. $existingJobs = $this->getAllSorted();
  95. $job = new TestJob();
  96. $this->instance->add($job, $argument);
  97. $jobs = $this->getAllSorted();
  98. $this->instance->remove($job, 10);
  99. $jobs2 = $this->getAllSorted();
  100. $this->assertEquals($jobs, $jobs2);
  101. $this->instance->remove($job, $argument);
  102. $jobs = $this->getAllSorted();
  103. $this->assertEquals($existingJobs, $jobs);
  104. }
  105. /**
  106. * @dataProvider argumentProvider
  107. * @param $argument
  108. */
  109. public function testHas($argument) {
  110. $job = new TestJob();
  111. $this->assertFalse($this->instance->has($job, $argument));
  112. $this->instance->add($job, $argument);
  113. $this->assertTrue($this->instance->has($job, $argument));
  114. $this->instance->remove($job, $argument);
  115. $this->assertFalse($this->instance->has($job, $argument));
  116. }
  117. /**
  118. * @dataProvider argumentProvider
  119. * @param $argument
  120. */
  121. public function testHasDifferentArgument($argument) {
  122. $job = new TestJob();
  123. $this->instance->add($job, $argument);
  124. $this->assertFalse($this->instance->has($job, 10));
  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. public function testHasReservedJobs() {
  199. $this->clearJobsList();
  200. $this->timeFactory->expects($this->atLeastOnce())
  201. ->method('getTime')
  202. ->willReturn(123456789);
  203. $job = new TestJob($this->timeFactory, $this, function () {
  204. });
  205. $job2 = new TestJob($this->timeFactory, $this, function () {
  206. });
  207. $this->instance->add($job, 1);
  208. $this->instance->add($job2, 2);
  209. $this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
  210. $this->assertFalse($this->instance->hasReservedJob());
  211. $this->assertFalse($this->instance->hasReservedJob(TestJob::class));
  212. $job = $this->instance->getNext();
  213. $this->assertNotNull($job);
  214. $this->assertTrue($this->instance->hasReservedJob());
  215. $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
  216. $job = $this->instance->getNext();
  217. $this->assertNotNull($job);
  218. $this->assertTrue($this->instance->hasReservedJob());
  219. $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
  220. }
  221. public function testHasReservedJobsAndParallelAwareJob() {
  222. $this->clearJobsList();
  223. $this->timeFactory->expects($this->atLeastOnce())
  224. ->method('getTime')
  225. ->willReturnCallback(function () use (&$time) {
  226. return time();
  227. });
  228. $job = new TestParallelAwareJob($this->timeFactory, $this, function () {
  229. });
  230. $job2 = new TestParallelAwareJob($this->timeFactory, $this, function () {
  231. });
  232. $this->instance->add($job, 1);
  233. $this->instance->add($job2, 2);
  234. $this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
  235. $this->assertFalse($this->instance->hasReservedJob());
  236. $this->assertFalse($this->instance->hasReservedJob(TestParallelAwareJob::class));
  237. $job = $this->instance->getNext();
  238. $this->assertNotNull($job);
  239. $this->assertTrue($this->instance->hasReservedJob());
  240. $this->assertTrue($this->instance->hasReservedJob(TestParallelAwareJob::class));
  241. $job = $this->instance->getNext();
  242. $this->assertNull($job); // Job doesn't allow parallel runs
  243. }
  244. public function markRun() {
  245. $this->ran = true;
  246. }
  247. }