TimedJobTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-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\TimedJob;
  10. class TestTimedJobNew extends TimedJob {
  11. public bool $ran = false;
  12. public function __construct(ITimeFactory $timeFactory) {
  13. parent::__construct($timeFactory);
  14. $this->setInterval(10);
  15. }
  16. public function run($argument) {
  17. $this->ran = true;
  18. }
  19. }
  20. class TimedJobTest extends \Test\TestCase {
  21. private DummyJobList $jobList;
  22. private ITimeFactory $time;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->jobList = new DummyJobList();
  26. $this->time = \OCP\Server::get(ITimeFactory::class);
  27. }
  28. public function testShouldRunAfterIntervalNew() {
  29. $job = new TestTimedJobNew($this->time);
  30. $job->setId(42);
  31. $this->jobList->add($job);
  32. $job->setLastRun(time() - 12);
  33. $job->start($this->jobList);
  34. $this->assertTrue($job->ran);
  35. }
  36. public function testShouldNotRunWithinIntervalNew() {
  37. $job = new TestTimedJobNew($this->time);
  38. $job->setId(42);
  39. $this->jobList->add($job);
  40. $job->setLastRun(time() - 5);
  41. $job->start($this->jobList);
  42. $this->assertFalse($job->ran);
  43. }
  44. public function testShouldNotTwiceNew() {
  45. $job = new TestTimedJobNew($this->time);
  46. $job->setId(42);
  47. $this->jobList->add($job);
  48. $job->setLastRun(time() - 15);
  49. $job->start($this->jobList);
  50. $this->assertTrue($job->ran);
  51. $job->ran = false;
  52. $job->start($this->jobList);
  53. $this->assertFalse($job->ran);
  54. }
  55. }