1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Jobs\Tests; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Spiral\Core\Container; |
16
|
|
|
use Spiral\Goridge\RPC; |
17
|
|
|
use Spiral\Goridge\SocketRelay; |
18
|
|
|
use Spiral\Jobs\Options; |
19
|
|
|
use Spiral\Jobs\Queue; |
20
|
|
|
use Spiral\Jobs\Registry\ContainerRegistry; |
21
|
|
|
|
22
|
|
|
abstract class BaseTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public const JOB = null; |
25
|
|
|
public const ERROR_JOB = null; |
26
|
|
|
|
27
|
|
|
private $job; |
28
|
|
|
private $errorJob; |
29
|
|
|
|
30
|
|
|
public function setUp(): void |
31
|
|
|
{ |
32
|
|
|
$this->job = static::JOB; |
33
|
|
|
$this->errorJob = static::ERROR_JOB; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function tearDown(): void |
37
|
|
|
{ |
38
|
|
|
if (file_exists((static::JOB)::JOB_FILE)) { |
39
|
|
|
unlink((static::JOB)::JOB_FILE); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testJob(): void |
44
|
|
|
{ |
45
|
|
|
$jobs = $this->makeJobs(); |
46
|
|
|
|
47
|
|
|
$id = $jobs->push($this->job, ['data' => 100]); |
48
|
|
|
|
49
|
|
|
$this->assertNotEmpty($id); |
50
|
|
|
|
51
|
|
|
$this->waitForJob(); |
52
|
|
|
$this->assertFileExists($this->job::JOB_FILE); |
53
|
|
|
|
54
|
|
|
$data = json_decode(file_get_contents($this->job::JOB_FILE), true); |
55
|
|
|
$this->assertSame($id, $data['id']); |
56
|
|
|
$this->assertSame(100, $data['data']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testErrorJob(): void |
60
|
|
|
{ |
61
|
|
|
$jobs = $this->makeJobs(); |
62
|
|
|
|
63
|
|
|
$id = $jobs->push($this->errorJob, ['data' => 100]); |
64
|
|
|
$this->assertNotEmpty($id); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testDelayJob(): void |
68
|
|
|
{ |
69
|
|
|
$jobs = $this->makeJobs(); |
70
|
|
|
|
71
|
|
|
$id = $jobs->push($this->job, ['data' => 100], Options::delayed(1)); |
72
|
|
|
|
73
|
|
|
$this->assertNotEmpty($id); |
74
|
|
|
|
75
|
|
|
$this->assertTrue($this->waitForJob() > 1); |
76
|
|
|
$this->assertFileExists($this->job::JOB_FILE); |
77
|
|
|
|
78
|
|
|
$data = json_decode(file_get_contents($this->job::JOB_FILE), true); |
79
|
|
|
$this->assertSame($id, $data['id']); |
80
|
|
|
$this->assertSame(100, $data['data']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @expectedException \Spiral\Jobs\Exception\JobException |
85
|
|
|
*/ |
86
|
|
|
public function testConnectionException(): void |
87
|
|
|
{ |
88
|
|
|
$jobs = new Queue( |
89
|
|
|
new RPC(new SocketRelay('localhost', 6002)), |
90
|
|
|
new ContainerRegistry(new Container()) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$jobs->push($this->job, ['data' => 100]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function makeJobs(): Queue |
97
|
|
|
{ |
98
|
|
|
return new Queue( |
99
|
|
|
new RPC(new SocketRelay('localhost', 6001)), |
100
|
|
|
new ContainerRegistry(new Container()) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function waitForJob(): float |
105
|
|
|
{ |
106
|
|
|
$start = microtime(true); |
107
|
|
|
$try = 0; |
108
|
|
|
while (!file_exists($this->job::JOB_FILE) && $try < 10) { |
109
|
|
|
usleep(250000); |
110
|
|
|
$try++; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return microtime(true) - $start; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|