-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
CleanRunningJobsTest.php
103 lines (88 loc) · 3.59 KB
/
CleanRunningJobsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace EthanYehuda\CronjobManager\Test\Integration;
use EthanYehuda\CronjobManager\Model\Clock;
use EthanYehuda\CronjobManager\Model\ErrorNotification;
use EthanYehuda\CronjobManager\Test\Util\FakeClock;
use Magento\Cron\Model\Schedule;
use Magento\Framework\ObjectManager\ObjectManager;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
use Magento\Framework\Event;
/**
* @magentoAppIsolation enabled
* @magentoAppArea crontab
*/
class CleanRunningJobsTest extends TestCase
{
const NOW = '2019-02-09 18:33:00';
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var Event\ManagerInterface
*/
private $eventManager;
/**
* @var FakeClock
*/
private $clock;
const DEAD_PID = 99999999;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->objectManager->configure(['preferences' => [Clock::class => FakeClock::class]]);
$this->objectManager->addSharedInstance($this->createMock(ErrorNotification::class), ErrorNotification::class);
$this->clock = $this->objectManager->get(Clock::class);
$this->clock->setTimestamp(strtotime(self::NOW));
$this->eventManager = $this->objectManager->get(Event\ManagerInterface::class);
}
public function testDeadRunningJobsAreCleaned()
{
$this->givenRunningScheduleWithInactiveProcess($schedule);
$this->whenEventIsDispatched('process_cron_queue_before');
$this->thenScheduleHasStatus($schedule, Schedule::STATUS_ERROR);
$this->andScheduleHasMessage($schedule, 'Process went away at ' . self::NOW);
}
public function testActiveRunningJobsAreNotCleaned()
{
$this->givenRunningScheduleWithActiveProcess($schedule);
$this->whenEventIsDispatched('process_cron_queue_before');
$this->thenScheduleHasStatus($schedule, Schedule::STATUS_RUNNING);
}
private function givenRunningScheduleWithInactiveProcess(&$schedule)
{
/** @var Schedule $schedule */
$schedule = $this->objectManager->create(Schedule::class);
$schedule->setStatus(Schedule::STATUS_RUNNING);
$schedule->setData('pid', self::DEAD_PID);
$schedule->save();
}
private function givenRunningScheduleWithActiveProcess(&$schedule)
{
/** @var Schedule $schedule */
$schedule = $this->objectManager->create(Schedule::class);
$schedule->setStatus(Schedule::STATUS_RUNNING);
$schedule->setData('pid', \getmypid());
$schedule->save();
}
private function whenEventIsDispatched($eventName)
{
$this->eventManager->dispatch($eventName);
}
private function thenScheduleHasStatus(Schedule $schedule, $expectedStatus)
{
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
$scheduleResource = $this->objectManager->get(\Magento\Cron\Model\ResourceModel\Schedule::class);
$scheduleResource->load($schedule, $schedule->getId());
$this->assertEquals($expectedStatus, $schedule->getStatus(), 'Schedule should have expected status');
}
private function andScheduleHasMessage(Schedule $schedule, $expectedMessage)
{
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
$scheduleResource = $this->objectManager->get(\Magento\Cron\Model\ResourceModel\Schedule::class);
$scheduleResource->load($schedule, $schedule->getId());
$this->assertEquals($expectedMessage, $schedule->getMessages(), 'Schedule should have expected message');
}
}