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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
<?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\Semaphore\Tests\Store;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Semaphore\Exception\SemaphoreAcquiringException;
use Symfony\Component\Semaphore\Exception\SemaphoreExpiredException;
use Symfony\Component\Semaphore\Key;
use Symfony\Component\Semaphore\PersistingStoreInterface;
/**
* @author Jérémy Derussé <jeremy@derusse.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
abstract class AbstractStoreTestCase extends TestCase
{
abstract protected function getStore(): PersistingStoreInterface;
public function testSaveExistAndDelete()
{
$store = $this->getStore();
$key = new Key(__METHOD__, 1);
$this->assertFalse($store->exists($key));
$store->save($key, 10);
$this->assertTrue($store->exists($key));
$store->delete($key);
$this->assertFalse($store->exists($key));
}
public function testSaveWithDifferentResources()
{
$store = $this->getStore();
$key1 = new Key(__METHOD__.'1', 1);
$key2 = new Key(__METHOD__.'2', 1);
$store->save($key1, 10);
$this->assertTrue($store->exists($key1));
$this->assertFalse($store->exists($key2));
$store->save($key2, 10);
$this->assertTrue($store->exists($key1));
$this->assertTrue($store->exists($key2));
$store->delete($key1);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));
$store->delete($key2);
$this->assertFalse($store->exists($key1));
$this->assertFalse($store->exists($key2));
}
public function testSaveWithDifferentKeysOnSameResource()
{
$store = $this->getStore();
$resource = __METHOD__;
$key1 = new Key($resource, 1);
$key2 = new Key($resource, 1);
$store->save($key1, 10);
$this->assertTrue($store->exists($key1));
$this->assertFalse($store->exists($key2));
try {
$store->save($key2, 10);
$this->fail('The store shouldn\'t save the second key');
} catch (SemaphoreAcquiringException $e) {
}
// The failure of previous attempt should not impact the state of current semaphores
$this->assertTrue($store->exists($key1));
$this->assertFalse($store->exists($key2));
$store->delete($key1);
$this->assertFalse($store->exists($key1));
$this->assertFalse($store->exists($key2));
$store->save($key2, 10);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));
$store->delete($key2);
$this->assertFalse($store->exists($key1));
$this->assertFalse($store->exists($key2));
}
public function testSaveWithLimitAt2()
{
$store = $this->getStore();
$resource = __METHOD__;
$key1 = new Key($resource, 2);
$key2 = new Key($resource, 2);
$key3 = new Key($resource, 2);
$store->save($key1, 10);
$this->assertTrue($store->exists($key1));
$this->assertFalse($store->exists($key2));
$this->assertFalse($store->exists($key3));
$store->save($key2, 10);
$this->assertTrue($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertFalse($store->exists($key3));
try {
$store->save($key3, 10);
$this->fail('The store shouldn\'t save the third key');
} catch (SemaphoreAcquiringException $e) {
}
// The failure of previous attempt should not impact the state of current semaphores
$this->assertTrue($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertFalse($store->exists($key3));
$store->delete($key1);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertFalse($store->exists($key3));
$store->save($key3, 10);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertTrue($store->exists($key3));
$store->delete($key2);
$store->delete($key3);
}
public function testSaveWithWeightAndLimitAt3()
{
$store = $this->getStore();
$resource = __METHOD__;
$key1 = new Key($resource, 4, 2);
$key2 = new Key($resource, 4, 2);
$key3 = new Key($resource, 4, 2);
$store->save($key1, 10);
$this->assertTrue($store->exists($key1));
$this->assertFalse($store->exists($key2));
$this->assertFalse($store->exists($key3));
$store->save($key2, 10);
$this->assertTrue($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertFalse($store->exists($key3));
try {
$store->save($key3, 10);
$this->fail('The store shouldn\'t save the third key');
} catch (SemaphoreAcquiringException $e) {
}
// The failure of previous attempt should not impact the state of current semaphores
$this->assertTrue($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertFalse($store->exists($key3));
$store->delete($key1);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertFalse($store->exists($key3));
$store->save($key3, 10);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));
$this->assertTrue($store->exists($key3));
$store->delete($key2);
$store->delete($key3);
}
public function testPutOffExpiration()
{
$store = $this->getStore();
$key = new Key(__METHOD__, 4, 2);
$store->save($key, 20);
$store->putOffExpiration($key, 20);
// just asserts it doesn't throw an exception
$this->addToAssertionCount(1);
}
public function testPutOffExpirationWhenSaveHasNotBeenCalled()
{
// This test simulate the key has expired since it does not exist
$store = $this->getStore();
$key1 = new Key(__METHOD__, 4, 2);
$this->expectException(SemaphoreExpiredException::class);
$this->expectExceptionMessage('The semaphore "Symfony\Component\Semaphore\Tests\Store\AbstractStoreTestCase::testPutOffExpirationWhenSaveHasNotBeenCalled" has expired: the script returns a positive number.');
$store->putOffExpiration($key1, 20);
}
public function testSaveTwice()
{
$store = $this->getStore();
$key = new Key(__METHOD__, 1);
$store->save($key, 10);
$store->save($key, 10);
// just asserts it doesn't throw an exception
$this->addToAssertionCount(1);
$store->delete($key);
}
}
|