-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathWithoutOverlappingTraitTest.php
125 lines (103 loc) · 3.81 KB
/
WithoutOverlappingTraitTest.php
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
<?php
namespace Illuminated\Console\Tests;
use Illuminated\Console\MutexRuntimeException;
use Illuminated\Console\Tests\App\Console\Commands\GenericCommand;
use Illuminated\Console\Tests\App\Console\Commands\MysqlStrategyCommand;
use Illuminated\Console\Tests\App\Console\Commands\NullTimeoutCommand;
use Illuminated\Console\Tests\App\Console\Commands\TimeoutCommand;
class WithoutOverlappingTraitTest extends TestCase
{
/** @test */
public function it_adds_mutex_strategy_which_is_file_by_default()
{
$this->assertEquals('file', (new GenericCommand)->getMutexStrategy());
}
/** @test */
public function mutex_strategy_can_be_overloaded_by_protected_field()
{
$this->assertEquals('mysql', (new MysqlStrategyCommand)->getMutexStrategy());
}
/** @test */
public function mutex_strategy_can_be_set_by_the_public_method()
{
$command = new GenericCommand;
$command->setMutexStrategy('redis');
$this->assertEquals('redis', $command->getMutexStrategy());
}
/** @test */
public function it_adds_mutex_timeout_which_is_zero_by_default()
{
$this->assertEquals(0, (new GenericCommand)->getMutexTimeout());
}
/** @test */
public function mutex_timeout_can_be_overloaded_by_protected_field()
{
$this->assertEquals(3000, (new TimeoutCommand)->getMutexTimeout());
}
/** @test */
public function mutex_timeout_can_be_set_to_null_by_protected_field()
{
$this->assertNull((new NullTimeoutCommand)->getMutexTimeout());
}
/** @test */
public function mutex_timeout_can_be_set_by_the_public_method()
{
$command = new GenericCommand;
$command->setMutexTimeout(5000);
$this->assertEquals(5000, $command->getMutexTimeout());
}
/** @test */
public function mutex_timeout_can_be_set_to_null_by_the_public_method()
{
$command = new GenericCommand;
$command->setMutexTimeout(null);
$this->assertNull($command->getMutexTimeout());
}
/** @test */
public function it_generates_mutex_name_based_on_the_command_name_and_arguments()
{
/** @var \Mockery\Mock|\Illuminated\Console\WithoutOverlapping $command */
$command = mock(GenericCommand::class)->makePartial();
$command->expects('getName')->andReturn('icm:generic');
$command->expects('argument')->andReturn(['foo' => 'bar', 'baz' => 'faz']);
$hash = md5(json_encode(['foo' => 'bar', 'baz' => 'faz']));
$this->assertEquals("icmutex-icm:generic-{$hash}", $command->getMutexName());
}
/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function it_allows_to_run_command_if_there_is_no_other_running_instances()
{
$mutex = mock('overload:Illuminated\Console\Mutex');
$mutex->expects('acquireLock')->with(0)->andReturn(true);
$mutex->allows('releaseLock');
$this->artisan('icm:generic');
}
/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function it_blocks_if_trying_to_run_another_instance_of_the_command()
{
$this->willSeeException(MutexRuntimeException::class, 'Command is running now!');
$mutex = mock('overload:Illuminated\Console\Mutex');
$mutex->expects('acquireLock')->with(0)->andReturn(false);
$this->artisan('icm:generic');
}
/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function it_is_releasing_the_lock_after_command_execution()
{
/** @var \Mockery\Mock|\Illuminated\Console\Mutex $mutex */
$mutex = mock('overload:Illuminated\Console\Mutex');
$mutex->expects('releaseLock');
$command = new GenericCommand;
$command->releaseMutexLock($mutex);
}
}