From 57255ccb10546f4ed8bfda1cf30ba0e99d762acd Mon Sep 17 00:00:00 2001 From: David Hoover Date: Thu, 16 Apr 2015 14:26:35 -0700 Subject: [PATCH] Clean up some generics in ConsistentHash and add tests for non-defaults. --- .../main/java/hudson/util/ConsistentHash.java | 6 ++-- .../java/hudson/util/ConsistentHashTest.java | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/util/ConsistentHash.java b/core/src/main/java/hudson/util/ConsistentHash.java index 0919975ac5..cd67228ce0 100644 --- a/core/src/main/java/hudson/util/ConsistentHash.java +++ b/core/src/main/java/hudson/util/ConsistentHash.java @@ -192,18 +192,18 @@ public class ConsistentHash { String hash(T t); } - static final Hash DEFAULT_HASH = new Hash() { + static final Hash DEFAULT_HASH = new Hash() { public String hash(Object o) { return o.toString(); } }; public ConsistentHash() { - this(DEFAULT_HASH); + this((Hash) DEFAULT_HASH); } public ConsistentHash(int defaultReplication) { - this(DEFAULT_HASH,defaultReplication); + this((Hash) DEFAULT_HASH,defaultReplication); } public ConsistentHash(Hash hash) { diff --git a/core/src/test/java/hudson/util/ConsistentHashTest.java b/core/src/test/java/hudson/util/ConsistentHashTest.java index 0d10fff42a..ef3bff861c 100644 --- a/core/src/test/java/hudson/util/ConsistentHashTest.java +++ b/core/src/test/java/hudson/util/ConsistentHashTest.java @@ -140,6 +140,40 @@ public class ConsistentHashTest { assertEquals(5, hash.countAllPoints()); } + @Test + public void defaultReplicationIsOneHundred() { + ConsistentHash hash = new ConsistentHash(); + assertEquals(0, hash.countAllPoints()); + hash.add("foo"); + assertEquals(100, hash.countAllPoints()); + } + + @Test + public void setCustomDefaultReplication() { + ConsistentHash hash = new ConsistentHash((ConsistentHash.Hash) ConsistentHash.DEFAULT_HASH, 7); + assertEquals(0, hash.countAllPoints()); + hash.add("foo"); + assertEquals(7, hash.countAllPoints()); + } + + @Test + public void usesCustomHash() { + final RuntimeException exception = new RuntimeException(); + ConsistentHash.Hash hashFunction = new ConsistentHash.Hash() { + public String hash(String str) { + throw exception; + } + }; + + try { + ConsistentHash hash = new ConsistentHash(hashFunction); + hash.add("foo"); + fail("Didn't use custom hash function"); + } catch (RuntimeException e) { + assertSame(exception, e); + } + } + /** * This test doesn't fail but it's written to measure the performance of the consistent hash function with large data set. */