1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CacheTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_cache\Cache; |
7
|
|
|
use kalanis\kw_cache\CacheException; |
8
|
|
|
use kalanis\kw_cache\Simple; |
9
|
|
|
use kalanis\kw_semaphore\Semaphore; |
10
|
|
|
use kalanis\kw_semaphore\SemaphoreException; |
11
|
|
|
use kalanis\kw_storage\Storage as XStorage; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class SemaphoreTest extends ACacheTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @throws CacheException |
18
|
|
|
* @throws SemaphoreException |
19
|
|
|
*/ |
20
|
|
|
public function testRun(): void |
21
|
|
|
{ |
22
|
|
|
$semaphore = new Semaphore\Storage($this->getStorage(new \MockStorage()), 'dummy'); |
23
|
|
|
$lib = new Cache\Semaphore(new Simple\Variable(), $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
|
|
|
$semaphore = new Semaphore\Storage($this->getStorage(new \MockFailedStorage()), 'dummy'); |
46
|
|
|
$lib = new Cache\Semaphore(new MockCacheFail(), $semaphore); |
47
|
|
|
$lib->init(''); |
48
|
|
|
|
49
|
|
|
$this->assertFalse($lib->exists()); |
50
|
|
|
$this->assertEquals('', $lib->get()); |
51
|
|
|
$this->assertFalse($lib->set(static::TESTING_CONTENT)); |
52
|
|
|
$this->assertFalse($lib->exists()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws CacheException |
57
|
|
|
*/ |
58
|
|
|
public function testFailExists(): void |
59
|
|
|
{ |
60
|
|
|
$semaphore = new Semaphore\Storage($this->getStorage(new \MockKillingStorage()), 'dummy'); |
61
|
|
|
$storage = new Simple\Variable(); |
62
|
|
|
$lib = new Cache\Semaphore($storage, $semaphore); |
63
|
|
|
|
64
|
|
|
$storage->set(static::TESTING_CONTENT); |
65
|
|
|
$this->expectException(CacheException::class); |
66
|
|
|
$lib->exists(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws CacheException |
71
|
|
|
* @throws SemaphoreException |
72
|
|
|
*/ |
73
|
|
|
public function testFailSet(): void |
74
|
|
|
{ |
75
|
|
|
$semaphore = new SemaphoreCache($this->getStorage(new \MockStorage()), 'dummy'); |
76
|
|
|
$lib = new Cache\Semaphore(new Simple\Variable(), $semaphore); |
77
|
|
|
|
78
|
|
|
$this->assertTrue($lib->set(static::TESTING_CONTENT)); |
79
|
|
|
$this->assertTrue($lib->exists()); |
80
|
|
|
$this->assertEquals(static::TESTING_CONTENT, $lib->get()); |
81
|
|
|
|
82
|
|
|
// semaphore action |
83
|
|
|
$semaphore->want(); |
84
|
|
|
$semaphore->setStorage($this->getStorage(new \MockKillingStorage())); |
85
|
|
|
$this->expectException(CacheException::class); |
86
|
|
|
$lib->set(static::TESTING_CONTENT . static::TESTING_CONTENT); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class SemaphoreCache extends Semaphore\Storage |
92
|
|
|
{ |
93
|
|
|
public function setStorage(XStorage\Storage $storage): void |
94
|
|
|
{ |
95
|
|
|
$this->storage = $storage; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|