forked from awslabs/amazon-kinesis-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get unassigned leases in leasesToTake (awslabs#1320)
Consider all null leases as a possible lease to take alongside expired leases --------- Co-authored-by: Brendan Lynch <[email protected]>
- Loading branch information
1 parent
ec34ed1
commit 34fe58c
Showing
3 changed files
with
112 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
amazon-kinesis-client/src/test/java/software/amazon/kinesis/leases/LeaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package software.amazon.kinesis.leases; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import software.amazon.kinesis.retrieval.kpl.ExtendedSequenceNumber; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class LeaseTest { | ||
|
||
private static final long MOCK_CURRENT_TIME = 10000000000L; | ||
private static final long LEASE_DURATION_MILLIS = 1000L; | ||
|
||
private static final long LEASE_DURATION_NANOS = TimeUnit.MILLISECONDS.toNanos(LEASE_DURATION_MILLIS); | ||
|
||
//Write a unit test for software.amazon.kinesis.leases.Lease to test leaseOwner as null and epired | ||
@Test | ||
public void testLeaseOwnerNullAndExpired() { | ||
long expiredTime = MOCK_CURRENT_TIME - LEASE_DURATION_NANOS - 1; | ||
Lease lease = createLease(null, "leaseKey", expiredTime); | ||
Assert.assertTrue(lease.isAvailable(LEASE_DURATION_NANOS, MOCK_CURRENT_TIME)); | ||
Assert.assertNull(lease.leaseOwner()); | ||
} | ||
|
||
@Test | ||
public void testLeaseOwnerNotNullAndExpired() { | ||
long expiredTime = MOCK_CURRENT_TIME - LEASE_DURATION_NANOS - 1; | ||
Lease lease = createLease("leaseOwner", "leaseKey", expiredTime); | ||
Assert.assertTrue(lease.isAvailable(LEASE_DURATION_NANOS, MOCK_CURRENT_TIME)); | ||
Assert.assertEquals("leaseOwner", lease.leaseOwner()); | ||
} | ||
|
||
@Test | ||
public void testLeaseOwnerNotNullAndNotExpired() { | ||
long notExpiredTime = MOCK_CURRENT_TIME - LEASE_DURATION_NANOS + 1; | ||
Lease lease = createLease("leaseOwner", "leaseKey", notExpiredTime); | ||
Assert.assertFalse(lease.isAvailable(LEASE_DURATION_NANOS, MOCK_CURRENT_TIME)); | ||
Assert.assertEquals("leaseOwner", lease.leaseOwner()); | ||
} | ||
|
||
@Test | ||
public void testLeaseOwnerNullAndNotExpired() { | ||
long notExpiredTime = MOCK_CURRENT_TIME - LEASE_DURATION_NANOS + 1; | ||
Lease lease = createLease(null, "leaseKey", notExpiredTime); | ||
Assert.assertTrue(lease.isAvailable(LEASE_DURATION_NANOS, MOCK_CURRENT_TIME)); | ||
Assert.assertNull(lease.leaseOwner()); | ||
} | ||
|
||
private Lease createLease(String leaseOwner, String leaseKey, long lastCounterIncrementNanos) { | ||
final Lease lease = new Lease(); | ||
lease.checkpoint(new ExtendedSequenceNumber("checkpoint")); | ||
lease.ownerSwitchesSinceCheckpoint(0L); | ||
lease.leaseCounter(0L); | ||
lease.leaseOwner(leaseOwner); | ||
lease.parentShardIds(Collections.singleton("parentShardId")); | ||
lease.childShardIds(new HashSet<>()); | ||
lease.leaseKey(leaseKey); | ||
lease.lastCounterIncrementNanos(lastCounterIncrementNanos); | ||
return lease; | ||
} | ||
} |