-
Notifications
You must be signed in to change notification settings - Fork 6
/
LockSetSpec.php
112 lines (98 loc) · 3.71 KB
/
LockSetSpec.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
<?php
namespace spec\TH\Lock;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use TH\Lock\Lock;
use TH\Lock\LockSet;
use TH\Lock\RuntimeException;
use VirtualFileSystem\FileSystem;
class LockSetSpec extends ObjectBehavior
{
public function let(Lock $lock1)
{
$this->beConstructedWith([$lock1]);
}
public function letgo(Lock $lock1, Lock $lock2)
{
foreach (array_filter([$lock1, $lock2]) as $lock) {
$o = $lock->getWrappedObject()->getProphecy();
$r = new \ReflectionObject($o);
$p = $r->getProperty('methodProphecies');
$p->setAccessible(true);
$p->setValue($o, []);
}
}
public function it_is_initializable(Lock $lock1)
{
$this->shouldHaveType(LockSet::class);
$this->shouldImplement(Lock::class);
}
public function it_should_not_be_empty()
{
$this->beConstructedWith([]);
$this->shouldThrow(new RuntimeException("Lock set cannot be empty"))->duringInstantiation();
}
public function it_should_acquire_a_lock(Lock $lock1)
{
$lock1->acquire()->shouldBeCalled();
$this->acquire();
}
public function it_should_acquire_all_locks(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$this->acquire();
$lock1->acquire()->shouldHaveBeenCalled();
$lock2->acquire()->shouldHaveBeenCalled();
$lock1->release()->shouldBeCalled();
$lock2->release()->shouldBeCalled();
}
public function it_should_fail_to_acquire_if_one_lock_fail(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$lock1->acquire()->shouldBeCalled();
$lock1->release()->shouldBeCalled();
$lock2->acquire()->shouldBeCalled();
$lock2->acquire()->willThrow(new RuntimeException);
$this->shouldThrow(RuntimeException::class)->duringAcquire();
}
public function it_should_stop_trying_to_acquire_on_failure(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$lock1->acquire()->shouldBeCalled();
$lock1->acquire()->willThrow(new RuntimeException);
$lock2->acquire()->shouldNotBeCalled();
$this->shouldThrow(RuntimeException::class)->duringAcquire();
}
public function it_should_release_acquired_lock_on_acquire_failure(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$lock1->acquire()->shouldBeCalled();
$lock1->release()->shouldBeCalled();
$lock2->acquire()->willThrow(new RuntimeException);
$this->shouldThrow(RuntimeException::class)->duringAcquire();
}
public function it_should_not_release_not_acquired_lock_on_acquire_failure(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$lock1->acquire()->shouldBeCalled();
$lock1->acquire()->willThrow(new RuntimeException);
$lock1->release()->shouldNotBeCalled();
$lock2->release()->shouldNotBeCalled();
$this->shouldThrow(RuntimeException::class)->duringAcquire();
}
public function it_should_release_all_locks(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$lock1->release()->shouldBeCalled();
$lock2->release()->shouldBeCalled();
$this->release();
}
public function it_should_release_all_locks_even_if_one_failed(Lock $lock1, Lock $lock2)
{
$this->beConstructedWith([$lock1, $lock2]);
$lock1->release()->shouldBeCalled();
$lock1->release()->willThrow(new RuntimeException);
$lock2->release()->shouldBeCalled();
$this->shouldThrow(RuntimeException::class)->duringRelease();
}
}