Skip to content

Commit

Permalink
remove indefinite block
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 14, 2017
1 parent 4e6b2e4 commit 045e6f2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 61 deletions.
23 changes: 1 addition & 22 deletions src/Illuminate/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,14 @@ public function get($callback = null)
return $result;
}

/**
* Attempt to acquire the lock while blocking indefinitely.
*
* @param callable|null $calback
* @return bool
*/
public function block($callback = null)
{
while (! $this->acquire()) {
usleep(250 * 1000);
}

if (is_callable($callback)) {
return tap($callback(), function () {
$this->release();
});
}

return true;
}

/**
* Attempt to acquire the lock for the given number of seconds.
*
* @param int $seconds
* @param callable|null $callback
* @return bool
*/
public function blockFor($seconds, $callback = null)
public function block($seconds, $callback = null)
{
$starting = Carbon::now();

Expand Down
10 changes: 1 addition & 9 deletions src/Illuminate/Contracts/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ interface Lock
*/
public function get($callback = null);

/**
* Attempt to acquire the lock while blocking indefinitely.
*
* @param callable|null $calback
* @return bool
*/
public function block($callback = null);

/**
* Attempt to acquire the lock for the given number of seconds.
*
* @param int $seconds
* @param callable|null $callback
* @return bool
*/
public function blockFor($seconds, $callback = null);
public function block($seconds, $callback = null);

/**
* Release the lock.
Expand Down
35 changes: 5 additions & 30 deletions tests/Integration/Cache/CacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,51 +40,26 @@ public function test_locks_can_run_callbacks()
}


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

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


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

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


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

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

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


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

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


Expand All @@ -97,7 +72,7 @@ public function test_locks_throw_timeout_if_block_expires()

Cache::store('memcached')->lock('foo')->release();
Cache::store('memcached')->lock('foo', 5)->get();
$this->assertEquals('taylor', Cache::store('memcached')->lock('foo', 10)->blockFor(1, function () {
$this->assertEquals('taylor', Cache::store('memcached')->lock('foo', 10)->block(1, function () {
return 'taylor';
}));
}
Expand Down

0 comments on commit 045e6f2

Please sign in to comment.