/
ExpiringStoreTestTrait.php
133 lines (107 loc) · 3.61 KB
/
ExpiringStoreTestTrait.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
126
127
128
129
130
131
132
133
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Lock\Tests\Store;
use Symfony\Component\Lock\Exception\LockExpiredException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\StoreInterface;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
trait ExpiringStoreTestTrait
{
/**
* Amount of microseconds used as a delay to test expiration. Should be
* small enough not to slow the test suite too much, and high enough not to
* fail because of race conditions.
*
* @return int
*/
abstract protected function getClockDelay();
/**
* @see AbstractStoreTest::getStore()
*/
abstract protected function getStore();
/**
* Tests the store automatically delete the key when it expire.
*
* This test is time-sensitive: the $clockDelay could be adjusted.
*/
public function testExpiration()
{
$key = new Key(uniqid(__METHOD__, true));
$clockDelay = $this->getClockDelay();
/** @var StoreInterface $store */
$store = $this->getStore();
$store->save($key);
$store->putOffExpiration($key, 2 * $clockDelay / 1000000);
$this->assertTrue($store->exists($key));
usleep(3 * $clockDelay);
$this->assertFalse($store->exists($key));
}
/**
* Tests the store thrown exception when TTL expires.
*
* @expectedException \Symfony\Component\Lock\Exception\LockExpiredException
*/
public function testAbortAfterExpiration()
{
$key = new Key(uniqid(__METHOD__, true));
/** @var StoreInterface $store */
$store = $this->getStore();
$store->save($key);
$store->putOffExpiration($key, 1 / 1000000);
}
/**
* Tests the refresh can push the limits to the expiration.
*
* This test is time-sensitive: the $clockDelay could be adjusted.
*/
public function testRefreshLock()
{
// Amount of microseconds we should wait without slowing things down too much
$clockDelay = $this->getClockDelay();
$key = new Key(uniqid(__METHOD__, true));
/** @var StoreInterface $store */
$store = $this->getStore();
$store->save($key);
$store->putOffExpiration($key, 2 * $clockDelay / 1000000);
$this->assertTrue($store->exists($key));
usleep(3 * $clockDelay);
$this->assertFalse($store->exists($key));
}
public function testSetExpiration()
{
$key = new Key(uniqid(__METHOD__, true));
/** @var StoreInterface $store */
$store = $this->getStore();
$store->save($key);
$store->putOffExpiration($key, 1);
$this->assertGreaterThanOrEqual(0, $key->getRemainingLifetime());
$this->assertLessThanOrEqual(1, $key->getRemainingLifetime());
}
public function testExpiredLockCleaned()
{
$resource = uniqid(__METHOD__, true);
$key1 = new Key($resource);
$key2 = new Key($resource);
/** @var StoreInterface $store */
$store = $this->getStore();
$key1->reduceLifetime(0);
$this->assertTrue($key1->isExpired());
try {
$store->save($key1);
$this->fail('The store shouldn\'t have save an expired key');
} catch (LockExpiredException $e) {
}
$this->assertFalse($store->exists($key1));
$store->save($key2);
$this->assertTrue($store->exists($key2));
}
}