busbey commented on a change in pull request #2685:
URL: https://github.com/apache/hbase/pull/2685#discussion_r529793609
##########
File path:
hbase-client/src/test/java/org/apache/hadoop/hbase/util/TestRoundRobinPoolMap.java
##########
@@ -45,58 +52,104 @@ protected PoolType getPoolType() {
}
@Test
- public void testSingleThreadedClient() throws InterruptedException,
ExecutionException {
- Random rand = ThreadLocalRandom.current();
- String randomKey = String.valueOf(rand.nextInt());
- String randomValue = String.valueOf(rand.nextInt());
- // As long as the pool is not full, we'll get null back.
- // This forces the user to create new values that can be used to populate
- // the pool.
- runThread(randomKey, randomValue, null);
- assertEquals(1, poolMap.size(randomKey));
+ public void testGetOrCreate() throws IOException {
+ String key = "key";
+ String value = "value";
+ String result = poolMap.getOrCreate(key, () -> value);
+
+ assertEquals(value, result);
+ assertEquals(1, poolMap.values().size());
+ }
+
+ @Test
+ public void testMultipleKeys() throws IOException {
+ for (int i = 0; i < KEY_COUNT; i++) {
+ String key = Integer.toString(i);
+ String value = Integer.toString(2 * i);
+ String result = poolMap.getOrCreate(key, () -> value);
+
+ assertEquals(value, result);
+ }
+
+ assertEquals(KEY_COUNT, poolMap.values().size());
+ }
+
+ @Test
+ public void testMultipleValues() throws IOException {
+ String key = "key";
+
+ for (int i = 0; i < POOL_SIZE; i++) {
+ String value = Integer.toString(i);
+ String result = poolMap.getOrCreate(key, () -> value);
+
+ assertEquals(value, result);
+ }
+
+ assertEquals(POOL_SIZE, poolMap.values().size());
}
@Test
- public void testMultiThreadedClients() throws InterruptedException,
ExecutionException {
- Random rand = ThreadLocalRandom.current();
+ public void testRoundRobin() throws IOException {
+ String key = "key";
+
for (int i = 0; i < POOL_SIZE; i++) {
- String randomKey = String.valueOf(rand.nextInt());
- String randomValue = String.valueOf(rand.nextInt());
- // As long as the pool is not full, we'll get null back
- runThread(randomKey, randomValue, null);
- // As long as we use distinct keys, each pool will have one value
- assertEquals(1, poolMap.size(randomKey));
+ String value = Integer.toString(i);
+ poolMap.getOrCreate(key, () -> value);
}
- poolMap.clear();
- String randomKey = String.valueOf(rand.nextInt());
- for (int i = 0; i < POOL_SIZE - 1; i++) {
- String randomValue = String.valueOf(rand.nextInt());
- // As long as the pool is not full, we'll get null back
- runThread(randomKey, randomValue, null);
- // since we use the same key, the pool size should grow
- assertEquals(i + 1, poolMap.size(randomKey));
+
+ assertEquals(POOL_SIZE, poolMap.values().size());
+
+ // pool is filled, get() should return elements round robin order
+ // starting from 1, because the first get was called by runThread()
+ for (int i = 0; i < 2 * POOL_SIZE; i++) {
+ String expected = Integer.toString(i % POOL_SIZE);
+ assertEquals(expected, poolMap.getOrCreate(key, () -> {
+ throw new IOException("must not call me");
+ }));
}
- // at the end of the day, there should be as many values as we put
- assertEquals(POOL_SIZE - 1, poolMap.size(randomKey));
+
+ assertEquals(POOL_SIZE, poolMap.values().size());
}
@Test
- public void testPoolCap() throws InterruptedException, ExecutionException {
- Random rand = ThreadLocalRandom.current();
- String randomKey = String.valueOf(rand.nextInt());
- List<String> randomValues = new ArrayList<>();
- for (int i = 0; i < POOL_SIZE * 2; i++) {
- String randomValue = String.valueOf(rand.nextInt());
- randomValues.add(randomValue);
- if (i < POOL_SIZE - 1) {
- // As long as the pool is not full, we'll get null back
- runThread(randomKey, randomValue, null);
- } else {
- // when the pool becomes full, we expect the value we get back to be
- // what we put earlier, in round-robin order
- runThread(randomKey, randomValue, randomValues.get((i - POOL_SIZE + 1)
% POOL_SIZE));
+ public void testMultiThreadedRoundRobin() throws ExecutionException,
InterruptedException {
Review comment:
this is a great test.
##########
File path:
hbase-client/src/test/java/org/apache/hadoop/hbase/util/TestRoundRobinPoolMap.java
##########
@@ -45,58 +52,104 @@ protected PoolType getPoolType() {
}
@Test
- public void testSingleThreadedClient() throws InterruptedException,
ExecutionException {
- Random rand = ThreadLocalRandom.current();
- String randomKey = String.valueOf(rand.nextInt());
- String randomValue = String.valueOf(rand.nextInt());
- // As long as the pool is not full, we'll get null back.
- // This forces the user to create new values that can be used to populate
- // the pool.
- runThread(randomKey, randomValue, null);
- assertEquals(1, poolMap.size(randomKey));
+ public void testGetOrCreate() throws IOException {
+ String key = "key";
+ String value = "value";
+ String result = poolMap.getOrCreate(key, () -> value);
+
+ assertEquals(value, result);
+ assertEquals(1, poolMap.values().size());
+ }
+
+ @Test
+ public void testMultipleKeys() throws IOException {
+ for (int i = 0; i < KEY_COUNT; i++) {
+ String key = Integer.toString(i);
+ String value = Integer.toString(2 * i);
+ String result = poolMap.getOrCreate(key, () -> value);
+
+ assertEquals(value, result);
+ }
+
+ assertEquals(KEY_COUNT, poolMap.values().size());
+ }
+
+ @Test
+ public void testMultipleValues() throws IOException {
+ String key = "key";
+
+ for (int i = 0; i < POOL_SIZE; i++) {
+ String value = Integer.toString(i);
+ String result = poolMap.getOrCreate(key, () -> value);
+
+ assertEquals(value, result);
+ }
+
+ assertEquals(POOL_SIZE, poolMap.values().size());
}
@Test
- public void testMultiThreadedClients() throws InterruptedException,
ExecutionException {
- Random rand = ThreadLocalRandom.current();
+ public void testRoundRobin() throws IOException {
+ String key = "key";
+
for (int i = 0; i < POOL_SIZE; i++) {
- String randomKey = String.valueOf(rand.nextInt());
- String randomValue = String.valueOf(rand.nextInt());
- // As long as the pool is not full, we'll get null back
- runThread(randomKey, randomValue, null);
- // As long as we use distinct keys, each pool will have one value
- assertEquals(1, poolMap.size(randomKey));
+ String value = Integer.toString(i);
+ poolMap.getOrCreate(key, () -> value);
}
- poolMap.clear();
- String randomKey = String.valueOf(rand.nextInt());
- for (int i = 0; i < POOL_SIZE - 1; i++) {
- String randomValue = String.valueOf(rand.nextInt());
- // As long as the pool is not full, we'll get null back
- runThread(randomKey, randomValue, null);
- // since we use the same key, the pool size should grow
- assertEquals(i + 1, poolMap.size(randomKey));
+
+ assertEquals(POOL_SIZE, poolMap.values().size());
+
+ // pool is filled, get() should return elements round robin order
+ // starting from 1, because the first get was called by runThread()
Review comment:
nit: this second line is wrong. the first value is now 0 and not 1, and
runThread doesn't exist.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]