|
5 | 5 | */
|
6 | 6 | namespace Magento\Cron\Test\Unit\Model;
|
7 | 7 |
|
| 8 | +use Magento\Cron\Model\Schedule; |
| 9 | + |
8 | 10 | /**
|
9 | 11 | * Class \Magento\Cron\Test\Unit\Model\ObserverTest
|
10 | 12 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
@@ -607,4 +609,84 @@ public function testDispatchCleanup()
|
607 | 609 |
|
608 | 610 | $this->_observer->dispatch('');
|
609 | 611 | }
|
| 612 | + |
| 613 | + public function testMissedJobsCleanedInTime() |
| 614 | + { |
| 615 | + /* 1. Initialize dependencies of _generate() method which is called first */ |
| 616 | + $jobConfig = [ |
| 617 | + 'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']], |
| 618 | + ]; |
| 619 | + |
| 620 | + // This item was scheduled 2 days ago |
| 621 | + $schedule1 = $this->getMockBuilder( |
| 622 | + 'Magento\Cron\Model\Schedule' |
| 623 | + )->disableOriginalConstructor()->setMethods( |
| 624 | + ['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup'] |
| 625 | + )->getMock(); |
| 626 | + $schedule1->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null)); |
| 627 | + $schedule1->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-2 day -1 hour')); |
| 628 | + $schedule1->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED)); |
| 629 | + //we expect this job be deleted from the list |
| 630 | + $schedule1->expects($this->once())->method('delete')->will($this->returnValue(true)); |
| 631 | + |
| 632 | + // This item was scheduled 1 day ago |
| 633 | + $schedule2 = $this->getMockBuilder( |
| 634 | + 'Magento\Cron\Model\Schedule' |
| 635 | + )->disableOriginalConstructor()->setMethods( |
| 636 | + ['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup'] |
| 637 | + )->getMock(); |
| 638 | + $schedule2->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null)); |
| 639 | + $schedule2->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day')); |
| 640 | + $schedule2->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED)); |
| 641 | + //we don't expect this job be deleted from the list |
| 642 | + $schedule2->expects($this->never())->method('delete'); |
| 643 | + |
| 644 | + $this->_collection->addItem($schedule1); |
| 645 | + $this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig)); |
| 646 | + |
| 647 | + //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()" |
| 648 | + $this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000)); |
| 649 | + //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()" |
| 650 | + $this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000)); |
| 651 | + |
| 652 | + $this->_scopeConfig->expects($this->at(0))->method('getValue') |
| 653 | + ->with($this->equalTo('system/cron/test_group/use_separate_process')) |
| 654 | + ->will($this->returnValue(0)); |
| 655 | + $this->_scopeConfig->expects($this->at(1))->method('getValue') |
| 656 | + ->with($this->equalTo('system/cron/test_group/schedule_generate_every')) |
| 657 | + ->will($this->returnValue(0)); |
| 658 | + $this->_scopeConfig->expects($this->at(2))->method('getValue') |
| 659 | + ->with($this->equalTo('system/cron/test_group/history_cleanup_every')) |
| 660 | + ->will($this->returnValue(0)); |
| 661 | + $this->_scopeConfig->expects($this->at(3))->method('getValue') |
| 662 | + ->with($this->equalTo('system/cron/test_group/schedule_lifetime')) |
| 663 | + ->will($this->returnValue(2*24*60)); |
| 664 | + $this->_scopeConfig->expects($this->at(4))->method('getValue') |
| 665 | + ->with($this->equalTo('system/cron/test_group/history_success_lifetime')) |
| 666 | + ->will($this->returnValue(0)); |
| 667 | + $this->_scopeConfig->expects($this->at(5))->method('getValue') |
| 668 | + ->with($this->equalTo('system/cron/test_group/history_failure_lifetime')) |
| 669 | + ->will($this->returnValue(0)); |
| 670 | + |
| 671 | + /* 2. Initialize dependencies of _cleanup() method which is called second */ |
| 672 | + $scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock(); |
| 673 | + $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection)); |
| 674 | + $this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock)); |
| 675 | + |
| 676 | + $collection = $this->getMockBuilder( |
| 677 | + 'Magento\Cron\Model\Resource\Schedule\Collection' |
| 678 | + )->setMethods( |
| 679 | + ['addFieldToFilter', 'load', '__wakeup'] |
| 680 | + )->disableOriginalConstructor()->getMock(); |
| 681 | + $collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf()); |
| 682 | + $collection->expects($this->any())->method('load')->will($this->returnSelf()); |
| 683 | + $collection->addItem($schedule1); |
| 684 | + $collection->addItem($schedule2); |
| 685 | + |
| 686 | + $scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock(); |
| 687 | + $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection)); |
| 688 | + $this->_scheduleFactory->expects($this->at(1))->method('create')->will($this->returnValue($scheduleMock)); |
| 689 | + |
| 690 | + $this->_observer->dispatch(''); |
| 691 | + } |
610 | 692 | }
|
0 commit comments