Added more leases in tests.

This commit is contained in:
Sachin Sundar P S 2021-10-27 16:39:12 -07:00
parent e66493da28
commit 72017417da
2 changed files with 36 additions and 22 deletions

BIN
.log.swp Normal file

Binary file not shown.

View file

@ -18,12 +18,15 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.stream.Collectors;
import junit.framework.Assert; import junit.framework.Assert;
@ -109,16 +112,14 @@ public class DynamoDBLeaseTakerTest {
@Test @Test
public void test_computeLeaseCounts_noExpiredLease() throws Exception { public void test_computeLeaseCounts_noExpiredLease() throws Exception {
final Lease lease = new Lease(); final List<Lease> leases = new ImmutableList.Builder<Lease>()
lease.checkpoint(new ExtendedSequenceNumber("checkpoint")); .add(createLease(null, "1"))
lease.ownerSwitchesSinceCheckpoint(0L); .add(createLease("foo", "2"))
lease.leaseCounter(0L); .add(createLease("bar", "3"))
lease.leaseOwner(null); .add(createLease("baz", "4"))
lease.parentShardIds(Collections.singleton("parentShardId")); .build();
lease.childShardIds(new HashSet<>()); dynamoDBLeaseTaker.allLeases.putAll(
lease.leaseKey("1"); leases.stream().collect(Collectors.toMap(Lease::leaseKey, Function.identity())));
final List<Lease> leases = Collections.singletonList(lease);
dynamoDBLeaseTaker.allLeases.put("1", lease);
when(leaseRefresher.listLeases()).thenReturn(leases); when(leaseRefresher.listLeases()).thenReturn(leases);
when(metricsFactory.createMetrics()).thenReturn(new NullMetricsScope()); when(metricsFactory.createMetrics()).thenReturn(new NullMetricsScope());
@ -128,22 +129,21 @@ public class DynamoDBLeaseTakerTest {
final Map<String, Integer> expectedOutput = new HashMap<>(); final Map<String, Integer> expectedOutput = new HashMap<>();
expectedOutput.put(null, 1); expectedOutput.put(null, 1);
expectedOutput.put("foo", 0); expectedOutput.put("foo", 1);
expectedOutput.put("bar", 1);
expectedOutput.put("baz", 1);
assertEquals(expectedOutput, actualOutput); assertEquals(expectedOutput, actualOutput);
} }
@Test @Test
public void test_computeLeaseCounts_withExpiredLease() throws Exception { public void test_computeLeaseCounts_withExpiredLease() throws Exception {
final Lease lease = new Lease(); final List<Lease> leases = new ImmutableList.Builder<Lease>()
lease.checkpoint(new ExtendedSequenceNumber("checkpoint")); .add(createLease("foo", "2"))
lease.ownerSwitchesSinceCheckpoint(0L); .add(createLease("bar", "3"))
lease.leaseCounter(0L); .add(createLease("baz", "4"))
lease.leaseOwner(null); .build();
lease.parentShardIds(Collections.singleton("parentShardId")); dynamoDBLeaseTaker.allLeases.putAll(
lease.childShardIds(new HashSet<>()); leases.stream().collect(Collectors.toMap(Lease::leaseKey, Function.identity())));
lease.leaseKey("1");
final List<Lease> leases = Collections.singletonList(lease);
dynamoDBLeaseTaker.allLeases.put("1", lease);
when(leaseRefresher.listLeases()).thenReturn(leases); when(leaseRefresher.listLeases()).thenReturn(leases);
when(metricsFactory.createMetrics()).thenReturn(new NullMetricsScope()); when(metricsFactory.createMetrics()).thenReturn(new NullMetricsScope());
@ -151,6 +151,20 @@ public class DynamoDBLeaseTakerTest {
final Map<String, Integer> actualOutput = dynamoDBLeaseTaker.computeLeaseCounts(leases); final Map<String, Integer> actualOutput = dynamoDBLeaseTaker.computeLeaseCounts(leases);
assertEquals(ImmutableMap.of("foo", 0), actualOutput); final Map<String, Integer> expectedOutput = new HashMap<>();
expectedOutput.put("foo", 0);
assertEquals(expectedOutput, actualOutput);
}
private Lease createLease(String leaseOwner, String leaseKey) {
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);
return lease;
} }
} }