Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-anderson committed Dec 10, 2024
1 parent b2df00b commit 36d99a3
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -31,21 +34,22 @@ class RandomSubsetterTest {
void desiredSubsetLargerThanHostList() {
List<PrioritizedHost> hosts = hosts(10);
List<PrioritizedHost> results = new RandomSubsetter(Integer.MAX_VALUE).subset(hosts);
assertThat(results, equalTo(hosts));
assertThat(results, sameInstance(hosts));
}

@Test
void desiredSubsetSmallerThanHostList() {
List<PrioritizedHost> hosts = hosts(10);
List<PrioritizedHost> results = new RandomSubsetter(5).subset(hosts);
assertThat(results.size(), equalTo(5));
Set<PrioritizedHost> results = subsetSet(5, hosts);
// We turn it to a hash-set to ensure each element is unique.
assertThat(new HashSet<>(results).size(), equalTo(5));
}

@Test
void enoughHealthEndpoints() {
List<PrioritizedHost> hosts = hosts(10);
when(hosts.get(0).isHealthy()).thenReturn(false);
List<PrioritizedHost> results = new RandomSubsetter(5).subset(hosts);
Set<PrioritizedHost> results = subsetSet(5, hosts);
long healthyCount = results.stream().filter(PrioritizedHost::isHealthy).count();
assertThat(healthyCount, equalTo(5L));
}
Expand All @@ -56,13 +60,20 @@ void notEnoughHealthEndpoints() {
for (PrioritizedHost host : hosts) {
when(host.isHealthy()).thenReturn(false);
}
when(hosts.get(0).isHealthy()).thenReturn(false);
List<PrioritizedHost> results = new RandomSubsetter(5).subset(hosts);
List<PrioritizedHost> results = subset(5, hosts);
long healthyCount = results.stream().filter(PrioritizedHost::isHealthy).count();
assertThat(healthyCount, equalTo(0L));
assertThat(hosts, equalTo(results));
}

private static Set<PrioritizedHost> subsetSet(int randomSubsetSize, List<PrioritizedHost> hosts) {
return new HashSet<>(subset(randomSubsetSize, hosts));
}

private static List<PrioritizedHost> subset(int randomSubsetSize, List<PrioritizedHost> hosts) {
return new RandomSubsetter(randomSubsetSize).subset(hosts);
}

private static List<PrioritizedHost> hosts(int count) {
List<PrioritizedHost> hosts = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Expand Down

0 comments on commit 36d99a3

Please sign in to comment.