Completed
Push — master ( 745336...aadd5d )
by Matthew
06:17
created

PruneCommandTest::runPruneOld()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 2
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Command;
4
5
use Doctrine\ODM\MongoDB\DocumentRepository;
6
use Dtc\QueueBundle\Command\PruneCommand;
7
use Dtc\QueueBundle\Document\Job;
8
use PHPUnit\Framework\TestCase;
9
use Dtc\QueueBundle\ODM\JobManager;
10
use Dtc\QueueBundle\EventDispatcher\EventDispatcher;
11
use Dtc\QueueBundle\Model\WorkerManager;
12
use Dtc\QueueBundle\Tests\FibonacciWorker;
13
use Symfony\Component\DependencyInjection\Container;
14
15
class PruneCommandTest extends TestCase
16
{
17
    use CommandTrait;
18
19
    public function testPruneCommandExpired()
20
    {
21
        \Dtc\QueueBundle\Tests\ODM\JobManagerTest::setUpBeforeClass();
22
23
        /** @var JobManager $jobManager */
24
        $jobManager = \Dtc\QueueBundle\Tests\ODM\JobManagerTest::$jobManager;
25
        $eventDispatcher = new EventDispatcher();
26
        $workerManager = new WorkerManager($jobManager, $eventDispatcher);
27
        $worker = new FibonacciWorker();
28
        $worker->setJobClass(\Dtc\QueueBundle\Document\Job::class);
29
        $workerManager->addWorker($worker);
30
        $worker->setJobManager($jobManager);
31
32
        $container = new Container();
33
        $container->set('dtc_queue.job_manager', $jobManager);
34
35
        /** @var Job $job */
36
        $time = time() - 1;
37
        $date = new \DateTime("@$time");
38
        $job = $worker->later()->setExpiresAt($date)->fibonacci(1);
39
        self::assertNotNull($job->getId(), 'Job id should be generated');
40
41
        /** @var JobManager $jobManager $ */
42
        /** @var DocumentRepository $repository */
43
        $repository = $jobManager->getRepository();
44
        $count = $repository->createQueryBuilder()->getQuery()->count(true);
45
        self::assertEquals(1, $count);
46
        $archiveRepository = $jobManager->getObjectManager()->getRepository($jobManager->getArchiveObjectName());
47
        $countArchive = $archiveRepository->createQueryBuilder()->getQuery()->count(true);
48
        self::assertEquals(0, $countArchive);
49
50
        $this->runCommand('\Dtc\QueueBundle\Command\PruneCommand', $container, ['type' => 'expired']);
51
52
        $count = $repository->createQueryBuilder()->getQuery()->count(true);
53
        self::assertEquals(0, $count);
54
55
        $countArchive = $archiveRepository->createQueryBuilder()->getQuery()->count(true);
56
        self::assertEquals(1, $countArchive);
57
    }
58
59
    public function testPruneCommandError()
60
    {
61
        $this->runPruneCommand(['type' => 'error'], 'pruneErroneousJobs');
62
    }
63
64
    public function testPruneCommandStalled()
65
    {
66
        $this->runPruneCommand(['type' => 'stalled'], 'pruneStalledJobs');
67
    }
68
69
    protected function getPruneCommandOlderDateDiff($older, $type = 'old', $call = 'pruneArchivedJobs')
70
    {
71
        $startTime = time();
72
        /** @var \DateTime $dateVal */
73
        $dateVal = $this->runPruneCommandOlder($older, 0, $type, $call);
74
        $endTime = time();
75
        $dateVal2 = new \DateTime("@$endTime");
76
        $dateDiff = $dateVal2->diff($dateVal);
77
78
        return [$dateDiff, $endTime - $startTime];
79
    }
80
81
    protected function getPruneCommandOlderDateDays($older, $type = 'old', $call = 'pruneArchivedJobs')
82
    {
83
        list($dateDiff) = $this->getPruneCommandOlderDateDiff($older, $type, $call);
84
85
        return $dateDiff->format('%a');
86
    }
87
88
    protected function getPruneCommandOlderDateSeconds($older, $type = 'old', $call = 'pruneArchivedJobs')
89
    {
90
        list($dateDiff, $varianceSeconds) = $this->getPruneCommandOlderDateDiff($older, $type, $call);
91
        $date1 = new \DateTime();
92
        $date2 = clone $date1;
93
        $date2->sub($dateDiff);
94
95
        return [$date2->getTimestamp() - $date1->getTimestamp(), $varianceSeconds];
96
    }
97
98
    public function testPruneOldRuns()
99
    {
100
        // Test invalid
101
        $this->runPruneOld();
102
        $this->runPruneOld('old_runs', 'pruneArchivedRuns');
103
    }
104
105
    public function runPruneOld($type = 'old', $call = 'pruneArchivedJobs')
106
    {
107
        // Test invalid
108
        $this->runPruneCommandOlder(null, 1, $type, $call);
109
        $this->runPruneCommandOlder('1x', 1, $type, $call);
110
        $this->runPruneCommandOlder('1dd', 1, $type, $call);
111
112
        // Test by day / month / year
113
        $result = $this->getPruneCommandOlderDateDays('1d', $type, $call);
114
        self::assertEquals(1, intval($result));
115
        $result = $this->getPruneCommandOlderDateDays('1m', $type, $call);
116
        self::assertGreaterThanOrEqual(28, intval($result));
117
        self::assertLessThanOrEqual(31, intval($result));
118
        $result = $this->getPruneCommandOlderDateDays('1y', $type, $call);
119
        self::assertGreaterThanOrEqual(365, intval($result));
120
        self::assertLessThanOrEqual(366, intval($result));
121
122
        // Test by time
123
        $this->runPruneCommandOlderSeconds('2h', 7200, $type, $call);
124
        $this->runPruneCommandOlderSeconds('1h', 3600, $type, $call);
125
        $this->runPruneCommandOlderSeconds('5i', 300, $type, $call);
126
        $this->runPruneCommandOlderSeconds('1i', 60, $type, $call);
127
        $this->runPruneCommandOlderSeconds('5s', 5, $type, $call);
128
        $this->runPruneCommandOlderSeconds('1s', 1, $type, $call);
129
130
        // Test timestamps
131
        $this->runPruneCommandOlderSeconds(time() - 1, 1, $type, $call);
132
        $this->runPruneCommandOlderSeconds(time() - 60, 60, $type, $call);
133
        $this->runPruneCommandOlderSeconds(time() - 86400, 86400, $type, $call);
134
    }
135
136
    protected function runPruneCommandOlderSeconds($older, $expected, $type = 'old', $call = 'pruneArchivedJobs')
137
    {
138
        list($result, $variance) = $this->getPruneCommandOlderDateSeconds($older, $type, $call);
139
        self::assertGreaterThanOrEqual($expected, intval($result));
140
        self::assertLessThanOrEqual($expected + $variance, intval($result));
141
    }
142
143
    protected function runPruneCommand($params, $call, $expectedResult = 0)
144
    {
145
        return $this->runStubCommand(PruneCommand::class, $params, $call, $expectedResult);
146
    }
147
148
    protected function runPruneCommandOlder($older, $expectedResult = 0, $type = 'old', $call = 'pruneArchivedJobs')
149
    {
150
        $params = ['type' => $type];
151
        if (null !== $older) {
152
            $params['--older'] = $older;
153
        }
154
        $manager = $this->runPruneCommand($params, $call, $expectedResult);
155
        if (0 === $expectedResult) {
156
            self::assertTrue(isset($manager->calls[$call][0][0]));
157
            self::assertTrue(!isset($manager->calls[$call][0][1]));
158
159
            return $manager->calls[$call][0][0];
160
        }
161
    }
162
}
163