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 mockDistributedLockin tests.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacquire()Try to acquire the lock.booleanacquire(java.time.Duration duration)Try to acquire the lock for a given duration.booleanacquireForever()Try to acquire the lock without expiring date.intacquireInvocations()static DistributedLockMockalwaysAcquiredLock(java.lang.String lockId)Create a lock that can be never acquired.static DistributedLockMockalwaysReleasedLock(java.lang.String lockId)Create a lock that can be always acquired.intaquireSuccesses()java.lang.StringgetId()Return the lock id.booleanrelease()Release the lockintreleaseInvocations()intreleaseSuccesses()static DistributedLockMocksequencedLock(java.lang.String lockId, java.util.List<java.lang.Boolean> results)Create a lock returning a sequence of results for acquiring.static DistributedLockMocksequencedLock(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 DistributedLockMocksingleStateLock(java.lang.String lockId, boolean released)Create always released lock if released parameter is true, otherwise always acquired lock is returnedbooleanwasAcquired()booleanwasAcquiredAndReleased()booleanwasAcquireInvoked()booleanwasAcquireRejected()booleanwasReleased()booleanwasReleaseInvoked()booleanwasReleaseRejected()-
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:DistributedLockReturn the lock id.- Specified by:
getIdin interfaceDistributedLock- Returns:
- the lock id
-
acquire
public boolean acquire()
Description copied from interface:DistributedLockTry to acquire the lock. Lock is acquired for a configured duration.- Specified by:
acquirein interfaceDistributedLock- Returns:
- true, if lock is acquired
-
acquire
public boolean acquire(java.time.Duration duration)
Description copied from interface:DistributedLockTry to acquire the lock for a given duration.- Specified by:
acquirein 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:DistributedLockTry 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:
acquireForeverin interfaceDistributedLock- Returns:
- true, if lock is acquired
-
release
public boolean release()
Description copied from interface:DistributedLockRelease the lock- Specified by:
releasein 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
-
-