Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lock
Manage
Activity
Members
Labels
Plan
Issues
9
Issue boards
Milestones
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
TYPO3
Extensions
lock
Commits
7e6d3376
Commit
7e6d3376
authored
7 years ago
by
Daniel Hürtgen
Browse files
Options
Downloads
Patches
Plain Diff
[FEATURE] NinjaMutexAdapterStrategy implemented
parent
51833f1b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Classes/Locking/Strategy/NinjaMutexAdapterStrategy.php
+107
-0
107 additions, 0 deletions
Classes/Locking/Strategy/NinjaMutexAdapterStrategy.php
Tests/Unit/Locking/Strategy/NinjaMutexAdapterStrategyTest.php
+145
-0
145 additions, 0 deletions
...s/Unit/Locking/Strategy/NinjaMutexAdapterStrategyTest.php
with
252 additions
and
0 deletions
Classes/Locking/Strategy/NinjaMutexAdapterStrategy.php
0 → 100644
+
107
−
0
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
();
}
}
This diff is collapsed.
Click to expand it.
Tests/Unit/Locking/Strategy/NinjaMutexAdapterStrategyTest.php
0 → 100644
+
145
−
0
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
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment