You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimedJobTest.php 1.6KB

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