Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
lock
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
4
Issues
4
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Packages
Packages
Container Registry
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
TYPO3
Extensions
lock
Commits
7e6d3376
Commit
7e6d3376
authored
Nov 08, 2017
by
Daniel Hürtgen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FEATURE] NinjaMutexAdapterStrategy implemented
parent
51833f1b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
252 additions
and
0 deletions
+252
-0
Classes/Locking/Strategy/NinjaMutexAdapterStrategy.php
Classes/Locking/Strategy/NinjaMutexAdapterStrategy.php
+107
-0
Tests/Unit/Locking/Strategy/NinjaMutexAdapterStrategyTest.php
...s/Unit/Locking/Strategy/NinjaMutexAdapterStrategyTest.php
+145
-0
No files found.
Classes/Locking/Strategy/NinjaMutexAdapterStrategy.php
0 → 100644
View file @
7e6d3376
<?php
namespace
Higidi\Lock\Locking\Strategy
;
use
Higidi\Lock\Exception\InvalidArgumentException
;
use
NinjaMutex\Mutex
;
use
TYPO3\CMS\Core\Locking\Exception\LockAcquireException
;
use
TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
;
use
TYPO3\CMS\Core\Locking\Exception\LockCreateException
;
use
TYPO3\CMS\Core\Locking\LockingStrategyInterface
;
/**
* TYPO3 lock strategy adapter for the arvenil/ninja-mutex library.
*/
class
NinjaMutexAdapterStrategy
implements
LockingStrategyInterface
{
/**
* @var Mutex
*/
protected
$mutex
;
/**
* @param Mutex $mutex ID to identify this lock in the system
*
* @throws LockCreateException if the lock could not be created
*/
public
function
__construct
(
$mutex
)
{
if
(
!
$mutex
instanceof
Mutex
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'%s() expects parameter 1 to be %s, %s given'
,
__METHOD__
,
Mutex
::
class
,
is_object
(
$mutex
)
?
get_class
(
$mutex
)
:
gettype
(
$mutex
)
),
1510158724
);
}
$this
->
mutex
=
$mutex
;
}
/**
* @return int LOCK_CAPABILITY_* elements combined with bit-wise OR
*/
public
static
function
getCapabilities
()
{
return
static
::
LOCK_CAPABILITY_EXCLUSIVE
|
static
::
LOCK_CAPABILITY_NOBLOCK
;
}
/**
* @return int Returns a priority for the method. 0 to 100, 100 is highest
*/
public
static
function
getPriority
()
{
return
0
;
}
/**
* Try to acquire a lock
*
* @param int $mode LOCK_CAPABILITY_EXCLUSIVE or LOCK_CAPABILITY_SHARED
*
* @return bool Returns TRUE if the lock was acquired successfully
* @throws LockAcquireException if the lock could not be acquired
* @throws LockAcquireWouldBlockException if the acquire would have blocked and NOBLOCK was set
*/
public
function
acquire
(
$mode
=
self
::
LOCK_CAPABILITY_EXCLUSIVE
)
{
$timeout
=
null
;
if
(
$mode
&
static
::
LOCK_CAPABILITY_NOBLOCK
)
{
$timeout
=
0
;
}
return
$this
->
mutex
->
acquireLock
(
$timeout
);
}
/**
* Release the lock
*
* @return bool Returns TRUE on success or FALSE on failure
*/
public
function
release
()
{
return
$this
->
mutex
->
releaseLock
();
}
/**
* Destroys the resource associated with the lock
*
* @return void
*/
public
function
destroy
()
{
$this
->
release
();
}
/**
* Get status of this lock
*
* @return bool Returns TRUE if lock is acquired by this locker, FALSE otherwise
*/
public
function
isAcquired
()
{
return
$this
->
mutex
->
isAcquired
();
}
}
Tests/Unit/Locking/Strategy/NinjaMutexAdapterStrategyTest.php
0 → 100644
View file @
7e6d3376
<?php
namespace
Higidi\Lock\Tests\Unit\Locking\Strategy
;
use
Higidi\Lock\Locking\Strategy\NinjaMutexAdapterStrategy
;
use
Nimut\TestingFramework\TestCase\UnitTestCase
;
use
NinjaMutex\Mutex
;
use
TYPO3\CMS\Core\Locking\LockingStrategyInterface
;
/**
* Test case for "\Higidi\Lock\Locking\Strategy\NinjaMutexAdapterStrategy".
*
* @covers \Higidi\Lock\Locking\Strategy\NinjaMutexAdapterStrategy
*/
class
NinjaMutexAdapterStrategyTest
extends
UnitTestCase
{
/**
* @test
*/
public
function
itImplementsTheLockStrategyInterface
()
{
$mutex
=
$this
->
prophesize
(
Mutex
::
class
);
$sut
=
new
NinjaMutexAdapterStrategy
(
$mutex
->
reveal
());
$this
->
assertInstanceOf
(
LockingStrategyInterface
::
class
,
$sut
);
}
/**
* @test
* @expectedException \Higidi\Lock\Exception\InvalidArgumentException
* @expectedExceptionCode 1510158724
*/
public
function
itThrowsAnInvalidArgumentExceptionIfMutexIsNotPassed
()
{
new
NinjaMutexAdapterStrategy
(
'blafoo'
);
}
/**
* @test
*/
public
function
itReturnsAnExclusiveCapability
()
{
$capabilities
=
NinjaMutexAdapterStrategy
::
getCapabilities
();
$capability
=
LockingStrategyInterface
::
LOCK_CAPABILITY_EXCLUSIVE
;
$this
->
assertSame
(
$capability
,
$capabilities
&
$capability
);
}
/**
* @test
*/
public
function
itReturnsANoBlockCapability
()
{
$capabilities
=
NinjaMutexAdapterStrategy
::
getCapabilities
();
$capability
=
LockingStrategyInterface
::
LOCK_CAPABILITY_NOBLOCK
;
$this
->
assertSame
(
$capability
,
$capabilities
&
$capability
);
}
/**
* @test
*/
public
function
itReturnsAPriority
()
{
$priority
=
NinjaMutexAdapterStrategy
::
getPriority
();
$this
->
assertSame
(
0
,
$priority
);
}
/**
* @test
*/
public
function
itAcquireALock
()
{
$mutex
=
$this
->
prophesize
(
Mutex
::
class
);
$mutex
->
acquireLock
(
null
)
->
shouldBeCalled
()
->
willReturn
(
true
);
$sut
=
new
NinjaMutexAdapterStrategy
(
$mutex
->
reveal
());
$this
->
assertTrue
(
$sut
->
acquire
());
}
/**
* @test
*/
public
function
itAcquireANonBlockingLock
()
{
$mode
=
LockingStrategyInterface
::
LOCK_CAPABILITY_EXCLUSIVE
|
LockingStrategyInterface
::
LOCK_CAPABILITY_NOBLOCK
;
$mutex
=
$this
->
prophesize
(
Mutex
::
class
);
$mutex
->
acquireLock
(
0
)
->
shouldBeCalled
()
->
willReturn
(
true
);
$sut
=
new
NinjaMutexAdapterStrategy
(
$mutex
->
reveal
());
$this
->
assertTrue
(
$sut
->
acquire
(
$mode
));
}
/**
* @test
*/
public
function
itReleasesALock
()
{
$mutex
=
$this
->
prophesize
(
Mutex
::
class
);
$mutex
->
releaseLock
()
->
shouldBeCalled
()
->
willReturn
(
true
);
$sut
=
new
NinjaMutexAdapterStrategy
(
$mutex
->
reveal
());
$this
->
assertTrue
(
$sut
->
release
());
}
/**
* @test
*/
public
function
itDestroysALock
()
{
$mutex
=
$this
->
prophesize
(
Mutex
::
class
);
$mutex
->
releaseLock
()
->
shouldBeCalled
()
->
willReturn
(
true
);
$sut
=
new
NinjaMutexAdapterStrategy
(
$mutex
->
reveal
());
$sut
->
destroy
();
}
/**
* @test
*/
public
function
itReturnsTheCurrentLockStatus
()
{
$mutex
=
$this
->
prophesize
(
Mutex
::
class
);
$mutex
->
isAcquired
()
->
shouldBeCalled
()
->
willReturn
(
true
);
$sut
=
new
NinjaMutexAdapterStrategy
(
$mutex
->
reveal
());
$this
->
assertTrue
(
$sut
->
isAcquired
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment