JobTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright (c) 2013 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\ILogger;
  11. class JobTest extends \Test\TestCase {
  12. private $run = false;
  13. private ITimeFactory $timeFactory;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->run = false;
  17. $this->timeFactory = \OC::$server->get(ITimeFactory::class);
  18. }
  19. public function testRemoveAfterException() {
  20. $jobList = new DummyJobList();
  21. $e = new \Exception();
  22. $job = new TestJob($this->timeFactory, $this, function () use ($e) {
  23. throw $e;
  24. });
  25. $jobList->add($job);
  26. $logger = $this->getMockBuilder(ILogger::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $logger->expects($this->once())
  30. ->method('error');
  31. $this->assertCount(1, $jobList->getAll());
  32. $job->execute($jobList, $logger);
  33. $this->assertTrue($this->run);
  34. $this->assertCount(1, $jobList->getAll());
  35. }
  36. public function testRemoveAfterError() {
  37. $jobList = new DummyJobList();
  38. $job = new TestJob($this->timeFactory, $this, function () {
  39. $test = null;
  40. $test->someMethod();
  41. });
  42. $jobList->add($job);
  43. $logger = $this->getMockBuilder(ILogger::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $logger->expects($this->once())
  47. ->method('error');
  48. $this->assertCount(1, $jobList->getAll());
  49. $job->execute($jobList, $logger);
  50. $this->assertTrue($this->run);
  51. $this->assertCount(1, $jobList->getAll());
  52. }
  53. public function testDisallowParallelRunsWithNoOtherJobs() {
  54. $jobList = new DummyJobList();
  55. $job = new TestJob($this->timeFactory, $this, function () {
  56. });
  57. $job->setAllowParallelRuns(false);
  58. $jobList->add($job);
  59. $jobList->setHasReservedJob(null, false);
  60. $jobList->setHasReservedJob(TestJob::class, false);
  61. $job->start($jobList);
  62. $this->assertTrue($this->run);
  63. }
  64. public function testAllowParallelRunsWithNoOtherJobs() {
  65. $jobList = new DummyJobList();
  66. $job = new TestJob($this->timeFactory, $this, function () {
  67. });
  68. $job->setAllowParallelRuns(true);
  69. $jobList->add($job);
  70. $jobList->setHasReservedJob(null, false);
  71. $jobList->setHasReservedJob(TestJob::class, false);
  72. $job->start($jobList);
  73. $this->assertTrue($this->run);
  74. }
  75. public function testAllowParallelRunsWithOtherJobs() {
  76. $jobList = new DummyJobList();
  77. $job = new TestJob($this->timeFactory, $this, function () {
  78. });
  79. $job->setAllowParallelRuns(true);
  80. $jobList->add($job);
  81. $jobList->setHasReservedJob(null, true);
  82. $jobList->setHasReservedJob(TestJob::class, true);
  83. $job->start($jobList);
  84. $this->assertTrue($this->run);
  85. }
  86. public function testDisallowParallelRunsWithOtherJobs() {
  87. $jobList = new DummyJobList();
  88. $job = new TestJob($this->timeFactory, $this, function () {
  89. });
  90. $job->setAllowParallelRuns(false);
  91. $jobList->add($job);
  92. $jobList->setHasReservedJob(null, true);
  93. $jobList->setHasReservedJob(TestJob::class, true);
  94. $job->start($jobList);
  95. $this->assertFalse($this->run);
  96. }
  97. public function markRun() {
  98. $this->run = true;
  99. }
  100. }