ScheduleTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cron\Model;
  7. use Magento\Framework\Stdlib\DateTime\DateTime;
  8. use \Magento\TestFramework\Helper\Bootstrap;
  9. /**
  10. * Test \Magento\Cron\Model\Schedule
  11. *
  12. * @magentoDbIsolation enabled
  13. */
  14. class ScheduleTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var ScheduleFactory
  18. */
  19. private $scheduleFactory;
  20. /**
  21. * @var DateTime
  22. */
  23. protected $dateTime;
  24. public function setUp()
  25. {
  26. $this->dateTime = Bootstrap::getObjectManager()->create(DateTime::class);
  27. $this->scheduleFactory = Bootstrap::getObjectManager()->create(ScheduleFactory::class);
  28. }
  29. /**
  30. * If there are no currently locked jobs, locking one of them should succeed
  31. */
  32. public function testTryLockJobNoLockedJobsSucceeds()
  33. {
  34. for ($i = 1; $i < 6; $i++) {
  35. $this->createSchedule("test_job", Schedule::STATUS_PENDING, 60 * $i);
  36. }
  37. $schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
  38. $this->assertTrue($schedule->tryLockJob());
  39. }
  40. /**
  41. * If the job is already locked, attempting to lock it again should fail
  42. */
  43. public function testTryLockJobAlreadyLockedFails()
  44. {
  45. $schedule = $this->createSchedule("test_job", Schedule::STATUS_RUNNING);
  46. $this->assertFalse($schedule->tryLockJob());
  47. }
  48. /**
  49. * If the job is already locked but lock time less than 1 day ago, attempting to lock it again should fail
  50. */
  51. public function testTryLockJobAlreadyLockedSucceeds()
  52. {
  53. $offsetInThePast = 2*24*60*60;
  54. $oldSchedule = $this->scheduleFactory->create()
  55. ->setCronExpr("* * * * *")
  56. ->setJobCode("test_job")
  57. ->setStatus(Schedule::STATUS_RUNNING)
  58. ->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp() - $offsetInThePast))
  59. ->setScheduledAt(strftime('%Y-%m-%d %H:%M', $this->dateTime->gmtTimestamp() - $offsetInThePast + 60))
  60. ->setExecutedAt(strftime('%Y-%m-%d %H:%M', $this->dateTime->gmtTimestamp() - $offsetInThePast + 61));
  61. $oldSchedule->save();
  62. $schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
  63. $this->assertTrue($schedule->tryLockJob());
  64. }
  65. /**
  66. * If there's a job already locked, should not be able to lock another job
  67. */
  68. public function testTryLockJobOtherLockedFails()
  69. {
  70. $this->createSchedule("test_job", Schedule::STATUS_RUNNING);
  71. $schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING, 60);
  72. $this->assertFalse($schedule->tryLockJob());
  73. }
  74. /**
  75. * Should be able to lock a job if a job with a different code is locked
  76. */
  77. public function testTryLockJobDifferentJobLocked()
  78. {
  79. $this->createSchedule("test_job_other", Schedule::STATUS_RUNNING);
  80. $schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
  81. $this->assertTrue($schedule->tryLockJob());
  82. }
  83. /**
  84. * Creates a schedule with the given job code, status, and schedule time offset
  85. *
  86. * @param string $jobCode
  87. * @param string $status
  88. * @param int $timeOffset
  89. * @return Schedule
  90. */
  91. private function createSchedule($jobCode, $status, $timeOffset = 0)
  92. {
  93. $schedule = $this->scheduleFactory->create()
  94. ->setCronExpr("* * * * *")
  95. ->setJobCode($jobCode)
  96. ->setStatus($status)
  97. ->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
  98. ->setScheduledAt(strftime('%Y-%m-%d %H:%M', $this->dateTime->gmtTimestamp() + $timeOffset));
  99. $schedule->save();
  100. return $schedule;
  101. }
  102. }