1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StorageTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_cache\CacheException; |
7
|
|
|
use kalanis\kw_cache\Storage; |
8
|
|
|
use kalanis\kw_semaphore\Semaphore; |
9
|
|
|
use kalanis\kw_semaphore\SemaphoreException; |
10
|
|
|
use kalanis\kw_storage\Storage as XStorage; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class SemaphoreTest extends AStorageTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @throws CacheException |
17
|
|
|
* @throws SemaphoreException |
18
|
|
|
*/ |
19
|
|
|
public function testRun(): void |
20
|
|
|
{ |
21
|
|
|
$storage = $this->getStorage(new \MockStorage()); |
22
|
|
|
$semaphore = new Semaphore\Storage($storage, 'dummy'); |
23
|
|
|
$lib = new Storage\Semaphore($storage, $semaphore); |
24
|
|
|
$lib->init(''); |
25
|
|
|
|
26
|
|
|
$this->assertFalse($lib->exists()); |
27
|
|
|
$this->assertEquals('', $lib->get()); |
28
|
|
|
// simple write |
29
|
|
|
$this->assertTrue($lib->set(static::TESTING_CONTENT)); |
30
|
|
|
$this->assertTrue($lib->exists()); |
31
|
|
|
$this->assertEquals(static::TESTING_CONTENT, $lib->get()); |
32
|
|
|
// retry reload |
33
|
|
|
$semaphore->want(); |
34
|
|
|
$this->assertTrue($lib->set(static::TESTING_CONTENT . static::TESTING_CONTENT)); |
35
|
|
|
// clear all |
36
|
|
|
$lib->clear(); |
37
|
|
|
$this->assertFalse($lib->exists()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws CacheException |
42
|
|
|
*/ |
43
|
|
|
public function testNotSet(): void |
44
|
|
|
{ |
45
|
|
|
$storage = $this->getStorage(new \MockFailedStorage()); |
46
|
|
|
$semaphore = new Semaphore\Storage($storage, 'dummy'); |
47
|
|
|
$lib = new Storage\Semaphore($storage, $semaphore); |
48
|
|
|
$lib->init(''); |
49
|
|
|
|
50
|
|
|
$this->assertFalse($lib->exists()); |
51
|
|
|
$this->assertEquals('', $lib->get()); |
52
|
|
|
$this->assertFalse($lib->set(static::TESTING_CONTENT)); |
53
|
|
|
$this->assertFalse($lib->exists()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws CacheException |
58
|
|
|
*/ |
59
|
|
|
public function testFailExists(): void |
60
|
|
|
{ |
61
|
|
|
$storage = $this->getStorage(new \MockKillingStorage()); |
62
|
|
|
$semaphore = new Semaphore\Storage($storage, 'dummy'); |
63
|
|
|
$lib = new Storage\Semaphore($storage, $semaphore); |
64
|
|
|
|
65
|
|
|
$this->expectException(CacheException::class); |
66
|
|
|
$lib->exists(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws CacheException |
71
|
|
|
*/ |
72
|
|
|
public function testCannotSet(): void |
73
|
|
|
{ |
74
|
|
|
$storage = $this->getStorage(new \MockKillingStorage2()); |
75
|
|
|
$lib = new Storage\Semaphore($storage, new \MockSemaphore()); |
76
|
|
|
$lib->init(''); |
77
|
|
|
$this->expectException(CacheException::class); |
78
|
|
|
$lib->set('lkjhgfdsa'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @throws CacheException |
83
|
|
|
*/ |
84
|
|
|
public function testNotGet(): void |
85
|
|
|
{ |
86
|
|
|
$storage = $this->getStorage(new \MockKillingStorage2()); |
87
|
|
|
$lib = new Storage\Semaphore($storage, new \MockSemaphore()); |
88
|
|
|
$lib->init(''); |
89
|
|
|
$this->expectException(CacheException::class); |
90
|
|
|
$lib->get(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws CacheException |
95
|
|
|
*/ |
96
|
|
|
public function testNotClear(): void |
97
|
|
|
{ |
98
|
|
|
$storage = $this->getStorage(new \MockKillingStorage2()); |
99
|
|
|
$lib = new Storage\Semaphore($storage, new \MockSemaphore()); |
100
|
|
|
$lib->init(''); |
101
|
|
|
$this->expectException(CacheException::class); |
102
|
|
|
$lib->clear(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @throws CacheException |
107
|
|
|
* @throws SemaphoreException |
108
|
|
|
*/ |
109
|
|
|
public function testFailSet(): void |
110
|
|
|
{ |
111
|
|
|
$semaphore = new SemaphoreStorage($this->getStorage(new \MockStorage()), 'dummy'); |
112
|
|
|
$lib = new Storage\Semaphore($this->getStorage(new \MockStorage()), $semaphore); |
113
|
|
|
|
114
|
|
|
$this->assertTrue($lib->set(static::TESTING_CONTENT)); |
115
|
|
|
$this->assertTrue($lib->exists()); |
116
|
|
|
$this->assertEquals(static::TESTING_CONTENT, $lib->get()); |
117
|
|
|
|
118
|
|
|
// semaphore action |
119
|
|
|
$semaphore->want(); |
120
|
|
|
$semaphore->setStorage($this->getStorage(new \MockKillingStorage())); |
121
|
|
|
$this->expectException(CacheException::class); |
122
|
|
|
$lib->set(static::TESTING_CONTENT . static::TESTING_CONTENT); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
class SemaphoreStorage extends Semaphore\Storage |
128
|
|
|
{ |
129
|
|
|
public function setStorage(XStorage\Storage $storage): void |
130
|
|
|
{ |
131
|
|
|
$this->storage = $storage; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|