|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BasicTests; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_semaphore\Interfaces\ISemaphore; |
|
7
|
|
|
use kalanis\kw_semaphore\Semaphore; |
|
8
|
|
|
use kalanis\kw_semaphore\SemaphoreException; |
|
9
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
|
10
|
|
|
use kalanis\kw_storage\Storage; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class StorageTest extends \CommonTestClass |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @throws SemaphoreException |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testStorage(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$lib = $this->getSemaphore(new \MockStorage()); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertFalse($lib->has()); |
|
23
|
|
|
$this->assertTrue($lib->want()); |
|
24
|
|
|
$this->assertTrue($lib->has()); |
|
25
|
|
|
$this->assertTrue($lib->remove()); |
|
26
|
|
|
$this->assertFalse($lib->has()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws SemaphoreException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testStorageFails(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$lib = $this->getSemaphore(new \MockFailedStorage()); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertFalse($lib->has()); |
|
37
|
|
|
$this->assertFalse($lib->want()); |
|
38
|
|
|
$this->assertFalse($lib->has()); |
|
39
|
|
|
$this->assertFalse($lib->remove()); |
|
40
|
|
|
$this->assertFalse($lib->has()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws SemaphoreException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testStorageKill1(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$lib = $this->getSemaphore(new \MockKillingStorage()); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertFalse($lib->has()); |
|
51
|
|
|
$this->expectException(SemaphoreException::class); |
|
52
|
|
|
$lib->want(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws SemaphoreException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testStorageKill2(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$lib = $this->getSemaphore(new \MockKillingStorage()); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertFalse($lib->has()); |
|
63
|
|
|
$this->expectException(SemaphoreException::class); |
|
64
|
|
|
$lib->remove(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function getSemaphore(IStorage $mockStorage): ISemaphore |
|
68
|
|
|
{ |
|
69
|
|
|
Storage\Key\DirKey::setDir(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR); |
|
70
|
|
|
$storage = new Storage\Factory(new Storage\Target\Factory(), new Storage\Format\Factory(), new Storage\Key\Factory()); |
|
71
|
|
|
return new Semaphore\Storage($storage->getStorage($mockStorage), 'dummy'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|