Skip to content

Commit

Permalink
Ensure Redis is available in Cache locks test (#19791)
Browse files Browse the repository at this point in the history
  • Loading branch information
alepeino authored and taylorotwell committed Jun 28, 2017
1 parent 294eac8 commit d95a54d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
57 changes: 35 additions & 22 deletions tests/Integration/Cache/CacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,40 @@
use Illuminate\Support\Carbon;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Tests\Redis\InteractsWithRedis;

/**
* @group integration
*/
class CacheLockTest extends TestCase
{
public function test_locks_can_be_acquired_and_released()
use InteractsWithRedis;

public function test_memcached_locks_can_be_acquired_and_released()
{
// Memcached...
Cache::store('memcached')->lock('foo')->release();
$this->assertTrue(Cache::store('memcached')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('memcached')->lock('foo', 10)->get());
Cache::store('memcached')->lock('foo')->release();
$this->assertTrue(Cache::store('memcached')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('memcached')->lock('foo', 10)->get());
Cache::store('memcached')->lock('foo')->release();

// Redis...
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
}

public function test_locks_can_run_callbacks()
public function test_redis_locks_can_be_acquired_and_released()
{
Cache::store('memcached')->lock('foo')->release();
$this->assertEquals('taylor', Cache::store('memcached')->lock('foo', 10)->get(function () {
return 'taylor';
}));
$this->ifRedisAvailable(function () {
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
});
}

public function test_locks_can_block_for_seconds()
public function test_memcached_locks_can_block_for_seconds()
{
Carbon::setTestNow();

Expand All @@ -49,14 +47,29 @@ public function test_locks_can_block_for_seconds()

Cache::store('memcached')->lock('foo')->release();
$this->assertTrue(Cache::store('memcached')->lock('foo', 10)->block(1));
}

public function test_redis_locks_can_block_for_seconds()
{
$this->ifRedisAvailable(function () {
Carbon::setTestNow();

Cache::store('redis')->lock('foo')->release();
$this->assertEquals('taylor', Cache::store('redis')->lock('foo', 10)->block(1, function () {
return 'taylor';
}));

Cache::store('redis')->lock('foo')->release();
$this->assertEquals('taylor', Cache::store('redis')->lock('foo', 10)->block(1, function () {
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->block(1));
});
}

public function test_locks_can_run_callbacks()
{
Cache::store('memcached')->lock('foo')->release();
$this->assertEquals('taylor', Cache::store('memcached')->lock('foo', 10)->get(function () {
return 'taylor';
}));

Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->block(1));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Redis/InteractsWithRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ public function redisDriverProvider()

return $providers;
}

public function ifRedisAvailable($callback)
{
$this->setUpRedis();

$callback();

$this->tearDownRedis();
}
}

0 comments on commit d95a54d

Please sign in to comment.