Skip to content

Commit

Permalink
default is non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrawley committed Sep 14, 2016
1 parent 28fabe4 commit 04aa36f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lib/FileLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ final public function getHandle()
*/
final public function acquire()
{
$type = LOCK_EX;
$type = LOCK_EX | LOCK_NB;
$desc = 'exclusive';

if (self::LOCK_SHARED & $this->options) {
$type = LOCK_EX;
$desc = 'shared';
}

if (self::LOCK_NON_BLOCKING & $this->options) {
$type |= LOCK_NB;
$desc .= ', non-blocking';
if (self::LOCK_BLOCKING & $this->options) {
$type ^= LOCK_NB;
$desc .= ', blocking';
}

if (!$this->flockOperation($type)) {
Expand Down Expand Up @@ -193,8 +193,7 @@ final public function release()
private function setOptions($options)
{
if ($options === null) {
$options = self::LOCK_SHARED;
$options |= self::LOCK_NON_BLOCKING;
$options = self::LOCK_SHARED | self::LOCK_NON_BLOCKING;
}

if (self::LOCK_SHARED & $options && self::LOCK_EXCLUSIVE & $options) {
Expand Down
16 changes: 14 additions & 2 deletions tests/FileLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function testLogger()
->expects($this->once())
->method('debug')
->with('Successfully acquired {desc} lock on file {file}.', [
'desc' => 'shared, non-blocking',
'desc' => 'shared',
'file' => __FILE__,
])
->willReturn(null);
Expand Down Expand Up @@ -185,14 +185,26 @@ public function testAcquireLogger()
->method('debug')
->with('Could not acquire {desc} lock on file {file}.', [
'file' => __FILE__.DIRECTORY_SEPARATOR.'does-not-exist.ext',
'desc' => 'shared, non-blocking'
'desc' => 'shared'
])
->willReturn(null);

$lock = new FileLock(__FILE__.DIRECTORY_SEPARATOR.'does-not-exist.ext', null, $logger);

$lock->acquire();
}

/**
* @expectedException \SR\File\Lock\Exception\FileLockAcquireException
*/
public function testExclusiveLock()
{
$lock1 = new FileLock(__FILE__, FileLock::LOCK_EXCLUSIVE | FileLock::LOCK_NON_BLOCKING);
$lock1->acquire();

$lock2 = new FileLock(__FILE__, FileLock::LOCK_EXCLUSIVE | FileLock::LOCK_NON_BLOCKING);
$lock2->acquire();
}
}

/* EOF */

0 comments on commit 04aa36f

Please sign in to comment.