Skip to content

Commit

Permalink
Add tests for eventually getting the lock when SkipBlockingWait is set
Browse files Browse the repository at this point in the history
  • Loading branch information
moshegood committed Feb 15, 2024
1 parent dbc76cc commit f620e0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public class AmazonDynamoDBLockClient implements Runnable, Closeable {
private final boolean holdLockOnServiceUnavailable;
private final String ownerName;
private final ConcurrentHashMap<String, LockItem> locks;
private final ConcurrentHashMap<String, LockItem> notMyLocks;
private final ConcurrentHashMap<String, LockItem> notMyLocks = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Thread> sessionMonitors;
private final Optional<Thread> backgroundThread;
private final Function<String, ThreadFactory> namedThreadCreator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,45 @@ public void acquireLock_whenLockAlreadyExistsAndIsNotReleased_andSkipBlockingWai
.thenReturn(GetItemResponse.builder().item(item).build())
.thenReturn(GetItemResponse.builder().build());
AcquireLockOptions acquireLockOptions = AcquireLockOptions.builder("customer1")
.withShouldSkipBlockingWait(true)
.withDeleteLockOnRelease(false).build();
.withShouldSkipBlockingWait(true)
.withDeleteLockOnRelease(false).build();
client.acquireLock(acquireLockOptions);
}
/*
* Test case for the scenario, where the lock is being held by the first owner and the lock duration has not past
* the lease duration. In this case, We should expect a LockAlreadyOwnedException when shouldSkipBlockingWait is set.
* But if we try again later, we should get the lock.
*/
@Test
public void acquireLock_whenLockAlreadyExistsAndIsNotReleased_andSkipBlockingWait_eventuallyGetsTheLock()
throws InterruptedException {
UUID uuid = setOwnerNameToUuid();
AmazonDynamoDBLockClient client = getLockClient();
Map<String, AttributeValue> item = new HashMap<>(5);
item.put("customer", AttributeValue.builder().s("customer1").build());
item.put("ownerName", AttributeValue.builder().s("foobar").build());
item.put("recordVersionNumber", AttributeValue.builder().s(uuid.toString()).build());
item.put("leaseDuration", AttributeValue.builder().s("100").build());
when(dynamodb.getItem(Mockito.<GetItemRequest>any()))
.thenReturn(GetItemResponse.builder().item(item).build())
.thenReturn(GetItemResponse.builder().build());
AcquireLockOptions acquireLockOptions = AcquireLockOptions.builder("customer1")
.withShouldSkipBlockingWait(true)
.withDeleteLockOnRelease(false).build();

try {
client.acquireLock(acquireLockOptions);
} catch (LockCurrentlyUnavailableException e) {
// This is expected
} catch (RuntimeException e) {
Assert.fail("Expected LockCurrentlyUnavailableException, but got " + e.getClass().getName());
}

// Now wait for the TTL to expire and try to acquire the lock again
Thread.sleep(101);
LockItem lockItem = client.acquireLock(acquireLockOptions);
Assert.assertNotNull("Failed to get lock item, when the lock is not present in the db", lockItem);
}

@Test(expected = IllegalArgumentException.class)
public void sendHeartbeat_whenDeleteDataTrueAndDataNotNull_throwsIllegalArgumentException() {
Expand Down

0 comments on commit f620e0a

Please sign in to comment.