Package com.coditory.sherlock.test
Class DistributedLockMock
- java.lang.Object
-
- com.coditory.sherlock.test.DistributedLockMock
-
- All Implemented Interfaces:
DistributedLock
public final class DistributedLockMock extends java.lang.Object implements DistributedLock
Use it to mockDistributedLock
in tests.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
acquire()
Try to acquire the lock.boolean
acquire(java.time.Duration duration)
Try to acquire the lock for a given duration.boolean
acquireForever()
Try to acquire the lock without expiring date.int
acquireInvocations()
static DistributedLockMock
alwaysAcquiredLock(java.lang.String lockId)
Create a lock that can be never acquired.static DistributedLockMock
alwaysReleasedLock(java.lang.String lockId)
Create a lock that can be always acquired.int
aquireSuccesses()
java.lang.String
getId()
Return the lock id.boolean
release()
Release the lockint
releaseInvocations()
int
releaseSuccesses()
static DistributedLockMock
sequencedLock(java.lang.String lockId, java.util.List<java.lang.Boolean> results)
Create a lock returning a sequence of results for acquiring.static DistributedLockMock
sequencedLock(java.lang.String lockId, java.util.List<java.lang.Boolean> acquireResults, java.util.List<java.lang.Boolean> releaseResults)
Create a lock returning a sequence of results for acquiring and releasing.static DistributedLockMock
singleStateLock(java.lang.String lockId, boolean released)
Create always released lock if released parameter is true, otherwise always acquired lock is returnedboolean
wasAcquired()
boolean
wasAcquiredAndReleased()
boolean
wasAcquireInvoked()
boolean
wasAcquireRejected()
boolean
wasReleased()
boolean
wasReleaseInvoked()
boolean
wasReleaseRejected()
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.coditory.sherlock.DistributedLock
acquireAndExecute, acquireAndExecute, acquireForeverAndExecute, releaseAndExecute
-
-
-
-
Method Detail
-
alwaysReleasedLock
public static DistributedLockMock alwaysReleasedLock(java.lang.String lockId)
Create a lock that can be always acquired.- Parameters:
lockId
- the lock id- Returns:
- created mock of distributed lock
-
alwaysAcquiredLock
public static DistributedLockMock alwaysAcquiredLock(java.lang.String lockId)
Create a lock that can be never acquired.- Parameters:
lockId
- the lock id- Returns:
- created mock of distributed lock
-
singleStateLock
public static DistributedLockMock singleStateLock(java.lang.String lockId, boolean released)
Create always released lock if released parameter is true, otherwise always acquired lock is returned- Parameters:
lockId
- the lock idreleased
- the lock state- Returns:
- created mock of distributed lock
-
sequencedLock
public static DistributedLockMock sequencedLock(java.lang.String lockId, java.util.List<java.lang.Boolean> results)
Create a lock returning a sequence of results for acquiring.- Parameters:
lockId
- the lock idresults
- the sequence of acquire and release results- Returns:
- created mock of distributed lock
-
sequencedLock
public static DistributedLockMock sequencedLock(java.lang.String lockId, java.util.List<java.lang.Boolean> acquireResults, java.util.List<java.lang.Boolean> releaseResults)
Create a lock returning a sequence of results for acquiring and releasing.- Parameters:
lockId
- the lock idacquireResults
- the sequence of acquire resultsreleaseResults
- the sequence of release results- Returns:
- created mock of distributed lock
-
getId
public java.lang.String getId()
Description copied from interface:DistributedLock
Return the lock id.- Specified by:
getId
in interfaceDistributedLock
- Returns:
- the lock id
-
acquire
public boolean acquire()
Description copied from interface:DistributedLock
Try to acquire the lock. Lock is acquired for a configured duration.- Specified by:
acquire
in interfaceDistributedLock
- Returns:
- true, if lock is acquired
-
acquire
public boolean acquire(java.time.Duration duration)
Description copied from interface:DistributedLock
Try to acquire the lock for a given duration.- Specified by:
acquire
in interfaceDistributedLock
- Parameters:
duration
- how much time must pass for the acquired lock to expire- Returns:
- true, if lock is acquired
-
acquireForever
public boolean acquireForever()
Description copied from interface:DistributedLock
Try to acquire the lock without expiring date. It is potentially dangerous. Lookout for a situation where the lock owning instance goes down with out releasing the lock.- Specified by:
acquireForever
in interfaceDistributedLock
- Returns:
- true, if lock is acquired
-
release
public boolean release()
Description copied from interface:DistributedLock
Release the lock- Specified by:
release
in interfaceDistributedLock
- Returns:
- true, if lock was released in this call. If lock has expired or was released by a different instance then false is returned.
-
wasAcquired
public boolean wasAcquired()
- Returns:
- true if lock was successfully acquired
-
wasReleased
public boolean wasReleased()
- Returns:
- true if lock was successfully released
-
wasAcquiredAndReleased
public boolean wasAcquiredAndReleased()
- Returns:
- true if lock was successfully acquired and released
-
wasAcquireRejected
public boolean wasAcquireRejected()
- Returns:
- true if lock was at least once acquired without success
-
wasReleaseRejected
public boolean wasReleaseRejected()
- Returns:
- true if lock was at least once acquired without success
-
releaseSuccesses
public int releaseSuccesses()
- Returns:
- the count of successful releases
-
aquireSuccesses
public int aquireSuccesses()
- Returns:
- the count of successful acquisitions
-
wasAcquireInvoked
public boolean wasAcquireInvoked()
- Returns:
- true if acquire operation was invoked
-
wasReleaseInvoked
public boolean wasReleaseInvoked()
- Returns:
- true if release operation was invoked
-
releaseInvocations
public int releaseInvocations()
- Returns:
- the count of release invocations
-
acquireInvocations
public int acquireInvocations()
- Returns:
- the count of acquire invocations
-
-