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\Jobs\Options; |
17
|
|
|
use Spiral\Jobs\Registry\ContainerRegistry; |
18
|
|
|
use Spiral\Jobs\ShortCircuit; |
19
|
|
|
use Spiral\Jobs\Tests\Local\ErrorJob; |
20
|
|
|
use Spiral\Jobs\Tests\Local\Job; |
21
|
|
|
|
22
|
|
|
class ShortCircuitTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
protected function tearDown(): void |
25
|
|
|
{ |
26
|
|
|
if (file_exists(Job::JOB_FILE)) { |
27
|
|
|
unlink(Job::JOB_FILE); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testLocal(): void |
32
|
|
|
{ |
33
|
|
|
$c = new ContainerRegistry(new Container()); |
34
|
|
|
$jobs = new ShortCircuit($c, $c); |
35
|
|
|
|
36
|
|
|
$id = $jobs->push(Job::class, ['data' => 100]); |
37
|
|
|
|
38
|
|
|
$this->assertNotEmpty($id); |
39
|
|
|
|
40
|
|
|
$this->assertFileExists(Job::JOB_FILE); |
41
|
|
|
|
42
|
|
|
$data = json_decode(file_get_contents(Job::JOB_FILE), true); |
43
|
|
|
$this->assertSame($id, $data['id']); |
44
|
|
|
$this->assertSame(100, $data['data']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testLocalDelayed(): void |
48
|
|
|
{ |
49
|
|
|
$c = new ContainerRegistry(new Container()); |
50
|
|
|
$jobs = new ShortCircuit($c, $c); |
51
|
|
|
|
52
|
|
|
$t = microtime(true); |
53
|
|
|
$id = $jobs->push(Job::class, ['data' => 100], Options::delayed(1)); |
54
|
|
|
|
55
|
|
|
$this->assertTrue(microtime(true) - $t >= 1); |
56
|
|
|
|
57
|
|
|
$this->assertNotEmpty($id); |
58
|
|
|
|
59
|
|
|
$this->assertFileExists(Job::JOB_FILE); |
60
|
|
|
|
61
|
|
|
$data = json_decode(file_get_contents(Job::JOB_FILE), true); |
62
|
|
|
$this->assertSame($id, $data['id']); |
63
|
|
|
$this->assertSame(100, $data['data']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @expectedException \Spiral\Jobs\Exception\JobException |
68
|
|
|
*/ |
69
|
|
|
public function testError(): void |
70
|
|
|
{ |
71
|
|
|
$c = new ContainerRegistry(new Container()); |
72
|
|
|
$jobs = new ShortCircuit($c, $c); |
73
|
|
|
$jobs->push(ErrorJob::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testLocalDelay(): void |
77
|
|
|
{ |
78
|
|
|
$c = new ContainerRegistry(new Container()); |
79
|
|
|
$jobs = new ShortCircuit($c, $c); |
80
|
|
|
|
81
|
|
|
$id = $jobs->push(Job::class, ['data' => 100], Options::delayed(1)); |
82
|
|
|
$this->assertNotEmpty($id); |
83
|
|
|
|
84
|
|
|
$this->assertFileExists(Job::JOB_FILE); |
85
|
|
|
|
86
|
|
|
$data = json_decode(file_get_contents(Job::JOB_FILE), true); |
87
|
|
|
$this->assertSame($id, $data['id']); |
88
|
|
|
$this->assertSame(100, $data['data']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|