1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Command\RunCommand; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Dtc\QueueBundle\ODM\JobManager; |
8
|
|
|
use Dtc\QueueBundle\EventDispatcher\EventDispatcher; |
9
|
|
|
use Dtc\QueueBundle\Model\WorkerManager; |
10
|
|
|
use Dtc\QueueBundle\Tests\FibonacciWorker; |
11
|
|
|
use Dtc\QueueBundle\Run\Loop; |
12
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
13
|
|
|
use Symfony\Component\Console\Tests\Fixtures\DummyOutput; |
14
|
|
|
use Symfony\Component\DependencyInjection\Container; |
15
|
|
|
|
16
|
|
|
class RunCommandTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
use CommandTrait; |
19
|
|
|
|
20
|
|
|
public function testORMRun() |
21
|
|
|
{ |
22
|
|
|
\Dtc\QueueBundle\Tests\ORM\JobManagerTest::setUpBeforeClass(); |
23
|
|
|
|
24
|
|
|
/** @var JobManager $jobManager */ |
25
|
|
|
$jobManager = \Dtc\QueueBundle\Tests\ORM\JobManagerTest::$jobManager; |
26
|
|
|
$runManager = \Dtc\QueueBundle\Tests\ORM\JobManagerTest::$runManager; |
27
|
|
|
$eventDispatcher = new EventDispatcher(); |
28
|
|
|
$workerManager = new WorkerManager($jobManager, $eventDispatcher); |
29
|
|
|
$worker = new FibonacciWorker(); |
30
|
|
|
$worker->setJobClass(\Dtc\QueueBundle\Entity\Job::class); |
31
|
|
|
$workerManager->addWorker($worker); |
32
|
|
|
$worker->setJobManager($jobManager); |
33
|
|
|
|
34
|
|
|
$loop = new Loop($workerManager, $jobManager, $runManager); |
35
|
|
|
$container = new Container(); |
36
|
|
|
$container->set('dtc_queue.run.loop', $loop); |
37
|
|
|
$container->set('dtc_queue.run_manager', $runManager); |
38
|
|
|
$worker->later()->fibonacci(1); |
39
|
|
|
|
40
|
|
|
$this->runRunCommand($loop, $container, [], 1); |
41
|
|
|
|
42
|
|
|
$startTime = time(); |
43
|
|
|
$this->runRunCommand($loop, $container, ['-d' => 2], 0); |
44
|
|
|
self::assertGreaterThanOrEqual(2, time() - $startTime); |
45
|
|
|
|
46
|
|
|
$worker->later()->fibonacci(1); |
47
|
|
|
$worker->later()->fibonacci(1); |
48
|
|
|
|
49
|
|
|
$this->runRunCommand($loop, $container, ['-m' => 4], 2); |
50
|
|
|
|
51
|
|
|
$worker->later()->fibonacci(1); |
52
|
|
|
$worker->later()->fibonacci(2); |
53
|
|
|
|
54
|
|
|
$this->runRunCommand($loop, $container, ['-m' => 1], 1); |
55
|
|
|
$this->runRunCommand($loop, $container, ['-m' => 1], 1); |
56
|
|
|
|
57
|
|
|
$runCommand = new RunCommand(); |
58
|
|
|
$runCommand->setContainer($container); |
59
|
|
|
$input = new ArrayInput(['-m' => 0, '-d' => 0]); |
60
|
|
|
$output = new DummyOutput(); |
61
|
|
|
$failed = false; |
62
|
|
|
try { |
63
|
|
|
$runCommand->run($input, $output); |
64
|
|
|
$failed = true; |
65
|
|
|
} catch (\Exception $exception) { |
66
|
|
|
self::assertTrue(true); |
67
|
|
|
} |
68
|
|
|
static::assertFalse($failed); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function runRunCommand(Loop $loop, Container $container, $params, $amountProcessed) |
72
|
|
|
{ |
73
|
|
|
$command = RunCommand::class; |
74
|
|
|
$this->runCommand($command, $container, $params); |
75
|
|
|
self::assertEquals($amountProcessed, $loop->getLastRun()->getProcessed()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|