Skip to content

Commit da76bf2

Browse files
author
Eugene Tulika
committed
MAGETWO-34526: Process GitHub PR#1052
1 parent f41e015 commit da76bf2

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

app/code/Magento/Cron/Model/Observer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,8 @@ protected function _cleanup($groupId)
359359
$now = time();
360360
/** @var Schedule $record */
361361
foreach ($history as $record) {
362-
$checkTime = strtotime($record->getExecutedAt() ? $record->getExecutedAt() :
363-
(int)$record->getScheduledAt() + $scheduleLifetime
364-
);
362+
$checkTime = $record->getExecutedAt() ? strtotime($record->getExecutedAt()) :
363+
strtotime($record->getScheduledAt()) + $scheduleLifetime;
365364
if ($checkTime < $now - $historyLifetimes[$record->getStatus()]) {
366365
$record->delete();
367366
}

app/code/Magento/Cron/Test/Unit/Model/ObserverTest.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Cron\Test\Unit\Model;
7-
7+
use Magento\Cron\Model\Schedule;
88
/**
99
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
1010
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -576,6 +576,9 @@ public function testDispatchCleanup()
576576
$this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
577577
$this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
578578

579+
//XML_PATH_HISTORY_CLEANUP_EVERY
580+
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
581+
//XML_PATH_SCHEDULE_LIFETIME
579582
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
580583

581584
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
@@ -600,28 +603,67 @@ public function testDispatchCleanup()
600603

601604
public function testMissedJobsCleanedInTime()
602605
{
606+
/* 1. Initialize dependencies of _generate() method which is called first */
603607
$jobConfig = [
604608
'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']],
605609
];
606610

607-
$schedule = $this->getMockBuilder(
611+
// This item was scheduled 2 days ago
612+
$schedule1 = $this->getMockBuilder(
608613
'Magento\Cron\Model\Schedule'
609614
)->disableOriginalConstructor()->setMethods(
610615
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
611616
)->getMock();
612-
$schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
613-
$schedule->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day'));
614-
$schedule->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
615-
616-
$this->_collection->addItem($schedule);
617-
617+
$schedule1->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
618+
$schedule1->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-2 day -1 hour'));
619+
$schedule1->expects($this->any())->method('getStatus')->will(
620+
$this->returnValue(Schedule::STATUS_MISSED));
621+
//we expect this job be deleted from the list
622+
$schedule1->expects($this->once())->method('delete')->will(
623+
$this->returnValue(true));
624+
625+
// This item was scheduled 1 day ago
626+
$schedule2 = $this->getMockBuilder(
627+
'Magento\Cron\Model\Schedule'
628+
)->disableOriginalConstructor()->setMethods(
629+
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
630+
)->getMock();
631+
$schedule2->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
632+
$schedule2->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day'));
633+
$schedule2->expects($this->any())->method('getStatus')->will(
634+
$this->returnValue(Schedule::STATUS_MISSED));
635+
//we don't expect this job be deleted from the list
636+
$schedule2->expects($this->never())->method('delete')->will(
637+
$this->returnValue(true));
638+
639+
$this->_collection->addItem($schedule1);
618640
$this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));
619641

642+
//get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()"
620643
$this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
644+
//get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()"
621645
$this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
622646

623-
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
624-
647+
$this->_scopeConfig->expects($this->at(0))->method('getValue')
648+
->with($this->equalTo('system/cron/test_group/use_separate_process'))
649+
->will($this->returnValue(0));
650+
$this->_scopeConfig->expects($this->at(1))->method('getValue')
651+
->with($this->equalTo('system/cron/test_group/schedule_generate_every'))
652+
->will($this->returnValue(0));
653+
$this->_scopeConfig->expects($this->at(2))->method('getValue')
654+
->with($this->equalTo('system/cron/test_group/history_cleanup_every'))
655+
->will($this->returnValue(0));
656+
$this->_scopeConfig->expects($this->at(3))->method('getValue')
657+
->with($this->equalTo('system/cron/test_group/schedule_lifetime'))
658+
->will($this->returnValue(2*24*60));
659+
$this->_scopeConfig->expects($this->at(4))->method('getValue')
660+
->with($this->equalTo('system/cron/test_group/history_success_lifetime'))
661+
->will($this->returnValue(0));
662+
$this->_scopeConfig->expects($this->at(5))->method('getValue')
663+
->with($this->equalTo('system/cron/test_group/history_failure_lifetime'))
664+
->will($this->returnValue(0));
665+
666+
/* 2. Initialize dependencies of _cleanup() method which is called second */
625667
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
626668
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
627669
$this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock));
@@ -633,7 +675,8 @@ public function testMissedJobsCleanedInTime()
633675
)->disableOriginalConstructor()->getMock();
634676
$collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf());
635677
$collection->expects($this->any())->method('load')->will($this->returnSelf());
636-
$collection->addItem($schedule);
678+
$collection->addItem($schedule1);
679+
$collection->addItem($schedule2);
637680

638681
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
639682
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection));

0 commit comments

Comments
 (0)