Skip to content

Commit

Permalink
Merge pull request #2 from yriveiro/develop
Browse files Browse the repository at this point in the history
merge Develop
  • Loading branch information
yriveiro authored Oct 1, 2018
2 parents e85b88d + 3ed9528 commit 793e038
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 72 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
# we recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.{json}]
indent_style = space
indent_size = 4

[*.md]
trim_trailing_whitespace = false
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- nightly
- hhvm

Expand All @@ -16,7 +13,7 @@ matrix:

before_script:
- composer self-update
- composer install --ignore-platform-reqs
- composer install

script:
- mkdir -p build/logs
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=7.0.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"satooshi/php-coveralls": "^0.6.1"
"phpunit/phpunit": "^6.0.0",
"php-coveralls/php-coveralls": "^2.1.0",
"phpstan/phpstan": "^0.9.2"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 6 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>./vendor</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
76 changes: 38 additions & 38 deletions src/FSLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,69 @@
class FSLock implements FSLockInterface
{
/**
* Lock id
* Lock id.
*
* @var string
*/
protected $lockID;

/**
* The resource linked to current lock
* The resource linked to current lock.
*
* @var resource
* @var resource|null
*/
protected $lock;

/**
* Temporal folder to store lock files
* Temporal folder to store lock files.
*
* @var string
*/
protected $lockBucket;

/**
* @param string $lockID The id of the lock with want to work, if not exists
* a new resource is created.
* @param string $lockID the id of the lock with want to work, if not
* exists a new resource is created
* @param string|null $bucket the directory to store the locks
*
* @throws FSLockIOException if temporal folder lockBucket is not writable
*/
public function __construct($lockID)
public function __construct(string $lockID, $bucket = null)
{
$this->lockID = $lockID;
$this->lockBucket = sys_get_temp_dir();
$lockFile = sprintf("%s/%s.fslock", $this->lockBucket, $this->lockID);
$this->lockBucket = $bucket ?: sys_get_temp_dir();
$lockFile = sprintf('%s/%s.fslock', $this->lockBucket, $this->lockID);

$this->lock = @fopen($lockFile, 'c');

$this->lock = fopen($lockFile, 'c');
if ($this->lock === false) {
throw new FSLockIOException("$bucket is not writable");
}
}

/**
* Destructor method. Internally calls FSLock::destroy()
* Clean up.
*/
public function __destruct()
{
$this->destroy();
if (is_resource($this->lock)) {
flock($this->lock, LOCK_UN);
fclose($this->lock);

$this->lock = null;
}
}

/**
* Acquires the lock
* Acquires the lock.
*
* @param boolean $blocker If the lock is acquire by other process before,
* and we call acquire as blocker, this call blocks
* after previous acquire release the lock.
* @param bool $blocker if the lock is acquire by other process before,
* and we call acquire as blocker, this call blocks
* after previous acquire release the lock
*
* @return boolean
* @return bool
*/
public function acquire($blocker = false)
public function acquire(bool $blocker = false): bool
{
if (!is_resource($this->lock)) {
return false;
Expand All @@ -64,11 +76,11 @@ public function acquire($blocker = false)
}

/**
* Releases the lock
* Releases the lock.
*
* @return boolean
* @return bool
*/
public function release()
public function release(): bool
{
if (!is_resource($this->lock)) {
return true;
Expand All @@ -78,34 +90,22 @@ public function release()
}

/**
* Destroy the lock manually
*/
public function destroy()
{
if (is_resource($this->lock)) {
flock($this->lock, LOCK_UN);
fclose($this->lock);
$this->lock = null;
}
}

/**
* Returns the lock id
* Returns the lock id.
*
* @return string
*/
public function id()
public function id(): string
{
return $this->lockID;
}

/**
* Returns the lock path
* Returns the lock path.
*
* @return string
*/
public function getPath()
public function getPath(): string
{
return sprintf("%s/%s.fslock", $this->lockBucket, $this->lockID);
return "$this->lockBucket/$this->lockID.fslock";
}
}
8 changes: 8 additions & 0 deletions src/FSLockIOException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace FSLock;

use RuntimeException;

class FSLockIOException extends RuntimeException
{
}
13 changes: 7 additions & 6 deletions src/FSLockInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace FSLock;

interface FSLockInterface
{
public function id();
public function release();
public function destroy();
public function getPath();
public function acquire($blocker = false);
public function id(): string;

public function getPath(): string;

public function release(): bool;

public function acquire(bool $blocker = false): bool;
}
49 changes: 30 additions & 19 deletions tests/FSLockTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace FSLock\tests;

use Exception;
use FSLock\FSLock;
use ReflectionProperty;
use PHPUnit_Framework_TestCase as TestCase;
use FSLock\FSLock;
use FSLock\FSLockIOException;
use PHPUnit\Framework\TestCase as TestCase;

class FSLockTest extends TestCase
{
Expand All @@ -16,27 +15,28 @@ public function setUp()

public function tearDown()
{
@unlink(sprintf("%s/unittest.fslock", $this->lockBucket));
@unlink("$this->lockBucket/unittest.fslock");
}

public function testCreateFSLockInstance()
{
$mutex = new FSLock('unittest');

$this->assertEquals(
$this->lockBucket . '/' . 'unittest.fslock',
$mutex->getPath()
"$this->lockBucket/unittest.fslock",
(new FSLock('unittest'))->getPath()
);
}

public function testDestroyFSLockInstance()
public function testBucketWorksInSystemTempDir()
{
$mutex = new FSLock('unittest');

$fileLock = $mutex->getPath();
$mutex->destroy();
$this->assertInstanceOf('FSLock\\FSLock', new FSLock('unittest'));
}

$this->assertFalse($mutex->acquire());
/**
* @expectedException \FSLock\FSLockIOException
*/
public function testBucketIsNotWritable()
{
new FSLock('unittest', '/foo');
}

public function testAcquire()
Expand All @@ -55,25 +55,36 @@ public function testRelease()

$this->assertTrue($mutex1->acquire());
$this->assertFalse($mutex2->acquire());

$mutex1->release();

$this->assertTrue($mutex2->acquire());
}

public function testGetFSLockID()
{
$mutex = new FSLock('unittest');
$this->assertEquals('unittest', $mutex->id());
$this->assertEquals('unittest', (new FSLock('unittest'))->id());
}

public function testReleaseUnlockedFile()
{
$mutex = new FSLock('unittest');
$mutex->destroy();

$lock = new ReflectionProperty($mutex, 'lock');
$lock->setAccessible(true);
$lock->setValue($mutex, null);

$this->assertEquals(null, $lock->getValue($mutex));
$this->assertTrue($mutex->release());
}

public function testAcquireFSLockNoResource()
{
$mutex = new FSLock('unittest');

$lock = new ReflectionProperty($mutex, 'lock');
$lock->setAccessible(true);
$lock->setValue($mutex, null);

$this->assertFalse($mutex->acquire());
}
}

0 comments on commit 793e038

Please sign in to comment.