Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RetryScheduledTaskTest.php « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 36e055cd8f6b8253b7e60f0741539c52f650592f (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Integration;
use Piwik\Scheduler\RetryableException;
use Piwik\Scheduler\Timetable;
use Piwik\Scheduler\Scheduler;
use Piwik\Scheduler\Task;
use Piwik\Tests\Framework\Mock\PiwikOption;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Psr\Log\NullLogger;
use ReflectionProperty;

/**
 * @group Scheduler
 * @group SchedulerRetryTaskTest
 */
class RetryScheduledTaskTest extends IntegrationTestCase
{

    public function testRetryCount()
    {

        $timetable = new Timetable();

        $task1 = 'task1';
        $task2 = 'task2';

        // Should be zero if no retry entry present
        $this->assertEquals(0, $timetable->getRetryCount($task1));

        // Should increment by one
        $timetable->incrementRetryCount($task1);
        $this->assertEquals(1, $timetable->getRetryCount($task1));

        // Should not break if more than one tasks is counting retries
        $timetable->incrementRetryCount($task2);
        $timetable->incrementRetryCount($task2);
        $this->assertEquals(2, $timetable->getRetryCount($task2));
        $timetable->incrementRetryCount($task1);
        $this->assertEquals(2, $timetable->getRetryCount($task1));

        // Should clear retry count without affecting other tasks
        $timetable->clearRetryCount($task1);
        $this->assertEquals(0, $timetable->getRetryCount($task1));
        $this->assertEquals(2, $timetable->getRetryCount($task2));
        $timetable->clearRetryCount($task2);
        $this->assertEquals(0, $timetable->getRetryCount($task1));

    }

    public function testTaskIsRetriedIfRetryableExcetionIsThrown()
    {

        // Mock timetable
        $now = time() - 60;
        $taskName = 'Piwik\Tests\Integration\RetryScheduledTaskTest.exceptionalTask';
        $timetableData = serialize([$taskName => $now]);
        self::getReflectedPiwikOptionInstance()->setValue(new PiwikOption($timetableData));

        // Create task
        $dailySchedule = $this->createPartialMock('Piwik\Scheduler\Schedule\Daily', array('getTime'));
        $dailySchedule->expects($this->any())
            ->method('getTime')
            ->will($this->returnValue($now));

        // Setup scheduler
        $tasks = [new Task($this, 'exceptionalTask', null, $dailySchedule)];
        $taskLoader = $this->createMock('Piwik\Scheduler\TaskLoader');
        $taskLoader->expects($this->atLeastOnce())
            ->method('loadTasks')
            ->willReturn($tasks);

        $scheduler = new Scheduler($taskLoader, new NullLogger());

        // First run
        $scheduler->run();
        $nextRun = $scheduler->getScheduledTimeForMethod('Piwik\Tests\Integration\RetryScheduledTaskTest', 'exceptionalTask', null);

        // Should be rescheduled one hour from now
        $this->assertEquals($now+3660, $nextRun);

        self::getReflectedPiwikOptionInstance()->setValue(null);

    }

    public function testTaskIsNotRetriedIfNormalExcetionIsThrown()
    {
        // Mock timetable
        $now = time() - 60;
        $taskName = 'Piwik\Tests\Integration\RetryScheduledTaskTest.normalExceptionTask';
        $timetableData = serialize([$taskName => $now]);
        self::getReflectedPiwikOptionInstance()->setValue(new PiwikOption($timetableData));

        // Create task
        $specificSchedule = $this->createPartialMock('Piwik\Scheduler\Schedule\SpecificTime', array('getTime'));
        $specificSchedule->setScheduledTime($now+50000);
        $specificSchedule->expects($this->any())
            ->method('getTime')
            ->will($this->returnValue($now));

        // Setup scheduler
        $tasks = [new Task($this, 'normalExceptionTask', null, $specificSchedule)];
        $taskLoader = $this->createMock('Piwik\Scheduler\TaskLoader');
        $taskLoader->expects($this->atLeastOnce())
            ->method('loadTasks')
            ->willReturn($tasks);

        $scheduler = new Scheduler($taskLoader, new NullLogger());

        // First run
        $scheduler->run();
        $nextRun = $scheduler->getScheduledTimeForMethod('Piwik\Tests\Integration\RetryScheduledTaskTest', 'normalExceptionTask', null);

        // Should not have scheduled for retry
        $this->assertEquals($now+50000, $nextRun);

        self::getReflectedPiwikOptionInstance()->setValue(null);
    }

    private static function getReflectedPiwikOptionInstance()
    {
        $piwikOptionInstance = new ReflectionProperty('Piwik\Option', 'instance');
        $piwikOptionInstance->setAccessible(true);
        return $piwikOptionInstance;
    }

    public function exceptionalTask()
    {
        throw new RetryableException('This task fails and should be retried');
    }

    public function normalExceptionTask()
    {
        throw new \Exception('This task fails and should not be retried');
    }

}