Polishing

This commit is contained in:
Juergen Hoeller
2014-01-21 01:55:09 +01:00
parent e0f9a85955
commit 11ef4308b8
4 changed files with 79 additions and 115 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,6 @@
package org.springframework.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
@@ -36,14 +33,19 @@ import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.util.ConcurrentReferenceHashMap.Entry;
import org.springframework.util.ConcurrentReferenceHashMap.Reference;
import org.springframework.util.ConcurrentReferenceHashMap.Restructure;
import org.springframework.util.comparator.ComparableComparator;
import org.springframework.util.comparator.NullSafeComparator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Tests for {@link ConcurrentReferenceHashMap}.
*
* @author Phillip Webb
*/
public class ConcurrentReferenceHashMapTests {
@@ -56,6 +58,7 @@ public class ConcurrentReferenceHashMapTests {
private TestWeakConcurrentCache<Integer, String> map = new TestWeakConcurrentCache<Integer, String>();
@Test
public void shouldCreateWithDefaults() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>();
@@ -66,8 +69,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateWithInitialCapacity() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
32);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(32);
assertThat(map.getSegmentsSize(), is(16));
assertThat(map.getSegment(0).getSize(), is(2));
assertThat(map.getLoadFactor(), is(0.75f));
@@ -75,8 +77,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateWithInitialCapacityAndLoadFactor() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
32, 0.5f);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(32, 0.5f);
assertThat(map.getSegmentsSize(), is(16));
assertThat(map.getSegment(0).getSize(), is(2));
assertThat(map.getLoadFactor(), is(0.5f));
@@ -84,8 +85,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateWithInitialCapacityAndConcurrenyLevel() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
16, 2);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(16, 2);
assertThat(map.getSegmentsSize(), is(2));
assertThat(map.getSegment(0).getSize(), is(8));
assertThat(map.getLoadFactor(), is(0.75f));
@@ -93,8 +93,7 @@ public class ConcurrentReferenceHashMapTests {
@Test
public void shouldCreateFullyCustom() throws Exception {
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
5, 0.5f, 3);
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(5, 0.5f, 3);
// concurrencyLevel of 3 ends up as 4 (nearest power of 2)
assertThat(map.getSegmentsSize(), is(4));
// initialCapacity is 5/4 (rounded up, to nearest power of 2)
@@ -102,19 +101,11 @@ public class ConcurrentReferenceHashMapTests {
assertThat(map.getLoadFactor(), is(0.5f));
}
@Test
public void shouldNeedPositiveConcurrenyLevel() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("ConcurrencyLevel must be positive");
new TestWeakConcurrentCache<Integer, String>(1, 0);
}
@Test
public void shouldNeedNonNegativeInitialCapacity() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(0, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("InitialCapacity must not be negative");
this.thrown.expectMessage("Initial capacity must not be negative");
new TestWeakConcurrentCache<Integer, String>(-1, 1);
}
@@ -122,10 +113,18 @@ public class ConcurrentReferenceHashMapTests {
public void shouldNeedPositiveLoadFactor() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(0, 0.1f, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("LoadFactor must be positive");
this.thrown.expectMessage("Load factor must be positive");
new TestWeakConcurrentCache<Integer, String>(0, 0.0f, 1);
}
@Test
public void shouldNeedPositiveConcurrencyLevel() throws Exception {
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Concurrency level must be positive");
new TestWeakConcurrentCache<Integer, String>(1, 0);
}
@Test
public void shouldPutAndGet() throws Exception {
// NOTE we are using mock references so we don't need to worry about GC
@@ -521,13 +520,11 @@ public class ConcurrentReferenceHashMapTests {
/**
* Time a multi-threaded access to a cache.
*
* @param cache the cache to test
* @return the timing stopwatch
* @throws InterruptedException
*/
private <V> StopWatch timeMultiThreaded(String id, final Map<Integer, V> map,
ValueFactory<V> factory) throws InterruptedException {
StopWatch stopWatch = new StopWatch(id);
for (int i = 0; i < 500; i++) {
map.put(i, factory.newValue(i));
@@ -536,7 +533,6 @@ public class ConcurrentReferenceHashMapTests {
stopWatch.start("Running threads");
for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) {
threads[threadIndex] = new Thread("Cache access thread " + threadIndex) {
@Override
public void run() {
for (int j = 0; j < 1000; j++) {
@@ -544,29 +540,30 @@ public class ConcurrentReferenceHashMapTests {
map.get(i);
}
}
};
}
};
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
for (Thread thread : threads) {
thread.start();
}
for (int i = 0; i < threads.length; i++) {
if (threads[i].isAlive()) {
threads[i].join(2000);
for (Thread thread : threads) {
if (thread.isAlive()) {
thread.join(2000);
}
}
stopWatch.stop();
return stopWatch;
}
private static interface ValueFactory<V> {
V newValue(int k);
}
private static class TestWeakConcurrentCache<K, V> extends
ConcurrentReferenceHashMap<K, V> {
private static class TestWeakConcurrentCache<K, V> extends ConcurrentReferenceHashMap<K, V> {
private int supplimentalHash;
@@ -582,8 +579,7 @@ public class ConcurrentReferenceHashMapTests {
this.disableTestHooks = disableTestHooks;
}
public TestWeakConcurrentCache(int initialCapacity, float loadFactor,
int concurrencyLevel) {
public TestWeakConcurrentCache(int initialCapacity, float loadFactor, int concurrencyLevel) {
super(initialCapacity, loadFactor, concurrencyLevel);
}
@@ -607,9 +603,7 @@ public class ConcurrentReferenceHashMapTests {
@Override
protected ReferenceManager createReferenceManager() {
return new ReferenceManager() {
@Override
public Reference<K, V> createReference(Entry<K, V> entry, int hash,
Reference<K, V> next) {
@@ -618,7 +612,6 @@ public class ConcurrentReferenceHashMapTests {
}
return new MockReference<K, V>(entry, hash, next, TestWeakConcurrentCache.this.queue);
}
@Override
public Reference<K, V> pollForPurge() {
if (TestWeakConcurrentCache.this.disableTestHooks) {
@@ -634,6 +627,7 @@ public class ConcurrentReferenceHashMapTests {
}
}
private static class MockReference<K, V> implements Reference<K, V> {
private final int hash;
@@ -644,8 +638,7 @@ public class ConcurrentReferenceHashMapTests {
private final LinkedList<MockReference<K, V>> queue;
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next,
LinkedList<MockReference<K, V>> queue) {
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next, LinkedList<MockReference<K, V>> queue) {
this.hash = hash;
this.entry = entry;
this.next = next;
@@ -677,4 +670,5 @@ public class ConcurrentReferenceHashMapTests {
this.queue.add(this);
}
}
}