| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace Illuminate\Tests\Integration\Cache;
- use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
- use Illuminate\Support\Facades\Cache;
- use Orchestra\Testbench\TestCase;
- use Redis;
- class PhpRedisCacheLockTest extends TestCase
- {
- use InteractsWithRedis;
- protected function setUp(): void
- {
- parent::setUp();
- $this->setUpRedis();
- }
- protected function tearDown(): void
- {
- parent::tearDown();
- $this->tearDownRedis();
- }
- public function testRedisLockCanBeAcquiredAndReleasedWithoutSerializationAndCompression()
- {
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- public function testRedisLockCanBeAcquiredAndReleasedWithPhpSerialization()
- {
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- public function testRedisLockCanBeAcquiredAndReleasedWithJsonSerialization()
- {
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- public function testRedisLockCanBeAcquiredAndReleasedWithIgbinarySerialization()
- {
- if (! defined('Redis::SERIALIZER_IGBINARY')) {
- $this->markTestSkipped('Redis extension is not configured to support the igbinary serializer.');
- }
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- public function testRedisLockCanBeAcquiredAndReleasedWithMsgpackSerialization()
- {
- if (! defined('Redis::SERIALIZER_MSGPACK')) {
- $this->markTestSkipped('Redis extension is not configured to support the msgpack serializer.');
- }
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_MSGPACK);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- /**
- * @requires extension lzf
- */
- public function testRedisLockCanBeAcquiredAndReleasedWithLzfCompression()
- {
- if (! defined('Redis::COMPRESSION_LZF')) {
- $this->markTestSkipped('Redis extension is not configured to support the lzf compression.');
- }
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
- $client->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_LZF);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- /**
- * @requires extension zstd
- */
- public function testRedisLockCanBeAcquiredAndReleasedWithZstdCompression()
- {
- if (! defined('Redis::COMPRESSION_ZSTD')) {
- $this->markTestSkipped('Redis extension is not configured to support the zstd compression.');
- }
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
- $client->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_ZSTD);
- $client->setOption(Redis::OPT_COMPRESSION_LEVEL, Redis::COMPRESSION_ZSTD_DEFAULT);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $client->setOption(Redis::OPT_COMPRESSION_LEVEL, Redis::COMPRESSION_ZSTD_MIN);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $client->setOption(Redis::OPT_COMPRESSION_LEVEL, Redis::COMPRESSION_ZSTD_MAX);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- /**
- * @requires extension lz4
- */
- public function testRedisLockCanBeAcquiredAndReleasedWithLz4Compression()
- {
- if (! defined('Redis::COMPRESSION_LZ4')) {
- $this->markTestSkipped('Redis extension is not configured to support the lz4 compression.');
- }
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
- $client->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_LZ4);
- $client->setOption(Redis::OPT_COMPRESSION_LEVEL, 1);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $client->setOption(Redis::OPT_COMPRESSION_LEVEL, 3);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $client->setOption(Redis::OPT_COMPRESSION_LEVEL, 12);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- /**
- * @requires extension Lzf
- */
- public function testRedisLockCanBeAcquiredAndReleasedWithSerializationAndCompression()
- {
- if (! defined('Redis::COMPRESSION_LZF')) {
- $this->markTestSkipped('Redis extension is not configured to support the lzf compression.');
- }
- $this->app['config']->set('database.redis.client', 'phpredis');
- $this->app['config']->set('cache.stores.redis.connection', 'default');
- $this->app['config']->set('cache.stores.redis.lock_connection', 'default');
- /** @var \Illuminate\Cache\RedisStore $store */
- $store = Cache::store('redis');
- /** @var \Redis $client */
- $client = $store->lockConnection()->client();
- $client->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
- $client->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_LZF);
- $store->lock('foo')->forceRelease();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- $lock = $store->lock('foo', 10);
- $this->assertTrue($lock->get());
- $this->assertFalse($store->lock('foo', 10)->get());
- $lock->release();
- $this->assertNull($store->lockConnection()->get($store->getPrefix().'foo'));
- }
- }
|