@Test public void testEmptyContext() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); assertEquals(endPoints, filter.filter(endPoints, PartitionContextBuilder.empty())); }
@Test public void testConsistency() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); // Generate a context to reuse and ensure consistency. What's in the context doesn't matter. PartitionContext context = PartitionContextBuilder.of(UUID.randomUUID().toString()); assertEquals(filter.filter(endPoints, context), filter.filter(endPoints, context)); }
@Test public void testIrrelevantPartitionContext() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter("cluster", "ensemble", "group"); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); // Ignores the PartitionContext because it doesn't have an entry for cluster, ensemble or group. assertEquals( endPoints, filter.filter(endPoints, PartitionContextBuilder.of("partition", "aaa"))); }
@Test public void testHashToBar() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); // It just so happens that "c" hashes to the same place as "bar". As long as the // ConsistentHashEndPointFilter // implementation doesn't change in a backward-compatible way, this should be deterministic and // testable. assertEquals(singleton(BAR), filter.filter(endPoints, PartitionContextBuilder.of("c"))); }
@Test public void testRelevantPartitionContext() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter("cluster", "ensemble", "group"); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); // Doesn't ignore the PartitionContext because it has an entry for at least one of cluster, // ensemble or group. assertEquals( singleton(FOO), filter.filter(endPoints, PartitionContextBuilder.of("ensemble", "aaa"))); }
@Test public void testServiceIdConflict() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); // Setup the ring with FOO and BAR. assertEquals( singleton(FOO), filter.filter(ImmutableList.of(FOO, BAR), PartitionContextBuilder.of("a"))); // Add a new server that will get ignored because it has the same ID as FOO and FOO comes last // in the list. assertEquals( singleton(FOO), filter.filter(ImmutableList.of(FOO2, FOO, BAR), PartitionContextBuilder.of("a"))); // Now remove the original FOO and verify that FOO2 is discovered. This can go wrong if we're // not careful in // the ring update implementation because removing FOO doesn't change the set of end point IDs. assertEquals( singleton(FOO2), filter.filter(ImmutableList.of(FOO2, BAR), PartitionContextBuilder.of("a"))); }
@Override public PartitionContext forCall(Method method, Object... args) { return PartitionContextBuilder.empty(); }