TimedJobTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class TestTimedJob extends \OC\BackgroundJob\TimedJob {
  11. /** @var bool */
  12. public $ran = false;
  13. public function __construct() {
  14. $this->setInterval(10);
  15. }
  16. public function run($argument) {
  17. $this->ran = true;
  18. }
  19. }
  20. class TestTimedJobNew extends \OCP\BackgroundJob\TimedJob {
  21. /** @var bool */
  22. public $ran = false;
  23. public function __construct(ITimeFactory $timeFactory) {
  24. parent::__construct($timeFactory);
  25. $this->setInterval(10);
  26. }
  27. public function run($argument) {
  28. $this->ran = true;
  29. }
  30. }
  31. class TimedJobTest extends \Test\TestCase {
  32. /** @var DummyJobList $jobList */
  33. private $jobList;
  34. /** @var ITimeFactory */
  35. private $time;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->jobList = new DummyJobList();
  39. $this->time = \OC::$server->query(ITimeFactory::class);
  40. }
  41. public function testShouldRunAfterInterval() {
  42. $job = new TestTimedJob();
  43. $this->jobList->add($job);
  44. $job->setLastRun(time() - 12);
  45. $job->execute($this->jobList);
  46. $this->assertTrue($job->ran);
  47. }
  48. public function testShouldNotRunWithinInterval() {
  49. $job = new TestTimedJob();
  50. $this->jobList->add($job);
  51. $job->setLastRun(time() - 5);
  52. $job->execute($this->jobList);
  53. $this->assertFalse($job->ran);
  54. }
  55. public function testShouldNotTwice() {
  56. $job = new TestTimedJob();
  57. $this->jobList->add($job);
  58. $job->setLastRun(time() - 15);
  59. $job->execute($this->jobList);
  60. $this->assertTrue($job->ran);
  61. $job->ran = false;
  62. $job->execute($this->jobList);
  63. $this->assertFalse($job->ran);
  64. }
  65. public function testShouldRunAfterIntervalNew() {
  66. $job = new TestTimedJobNew($this->time);
  67. $job->setId(42);
  68. $this->jobList->add($job);
  69. $job->setLastRun(time() - 12);
  70. $job->execute($this->jobList);
  71. $this->assertTrue($job->ran);
  72. }
  73. public function testShouldNotRunWithinIntervalNew() {
  74. $job = new TestTimedJobNew($this->time);
  75. $job->setId(42);
  76. $this->jobList->add($job);
  77. $job->setLastRun(time() - 5);
  78. $job->execute($this->jobList);
  79. $this->assertFalse($job->ran);
  80. }
  81. public function testShouldNotTwiceNew() {
  82. $job = new TestTimedJobNew($this->time);
  83. $job->setId(42);
  84. $this->jobList->add($job);
  85. $job->setLastRun(time() - 15);
  86. $job->execute($this->jobList);
  87. $this->assertTrue($job->ran);
  88. $job->ran = false;
  89. $job->execute($this->jobList);
  90. $this->assertFalse($job->ran);
  91. }
  92. }