Cleaned up cache tests.

This commit is contained in:
Harrison Deng 2020-04-28 17:12:36 -05:00
parent ca1e1c0975
commit 61a6bbaa4f

View File

@ -6,7 +6,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@ -19,15 +18,24 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
@TestInstance(Lifecycle.PER_CLASS)
public class CacheTest {
Cache<Integer, String> integerCache;
private volatile Cache<Integer, String> integerCache;
private final int LARGE_CACHE_SIZE = 16384;
private final int[] answers = new int[LARGE_CACHE_SIZE];
private final Random rand = new Random();
Cache<Integer, Integer> lCache = new Cache<>(LARGE_CACHE_SIZE);
@BeforeAll
public void setUp() {
integerCache = new Cache<>(3);
for (int i = 0; i < answers.length; i++) {
answers[i] = rand.nextInt();
}
}
@AfterEach
public void cleanUp() {
lCache.clearCache();
integerCache.clearCache();
}
@ -113,18 +121,10 @@ public class CacheTest {
@Test
public void testMultithreadingWriteConsistency() {
int size = 16384;
int[] answers = new int[size];
Random rand = new Random();
for (int i = 0; i < answers.length; i++) {
answers[i] = rand.nextInt();
}
Cache<Integer, Integer> lCache = new Cache<>(size);
Runnable write = new Runnable() {
@Override
public void run() {
for (int i = 0; i < size; i++) {
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
lCache.set(i, answers[i]);
}
}
@ -145,27 +145,21 @@ public class CacheTest {
assertFalse(false, e.getCause().getMessage());
}
for (int i = 0; i < size; i++) {
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
assertEquals(answers[i], lCache.get(i), "Accessor at: " + i);
}
}
@Test
public void testMultithreadingReadConsistency() {
int size = 16384;
Cache<Integer, Integer> lCache = new Cache<>(size);
int[] answers = new int[size];
Random rand = new Random();
for (int i = 0; i < answers.length; i++) {
answers[i] = rand.nextInt();
lCache.set(i, answers[i]);
}
Runnable read = new Runnable() {
@Override
public void run() {
for (int i = 0; i < size; i++) {
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
assertEquals(answers[i], lCache.get(i), "Accessor at: " + i);
}
}
@ -189,20 +183,10 @@ public class CacheTest {
@Test
public void testMulthreadedReadWrite() {
int size = 16384;
Cache<Integer, Integer> lCache = new Cache<>(size);
int[] answers = new int[size];
Random rand = new Random();
for (int i = 0; i < answers.length; i++) {
answers[i] = rand.nextInt();
lCache.set(i, answers[i]);
}
Runnable readWrite = new Runnable() {
@Override
public void run() {
for (int i = 0; i < size / 2; i++) {
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
if (lCache.get(i) == null) {
lCache.set(i, answers[i]);
} else {
@ -216,7 +200,7 @@ public class CacheTest {
@Override
public void run() {
for (int i = 0; i < size; i++) {
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
assertEquals(answers[i], lCache.get(i), "Accessor at: " + i);
}
}
@ -240,27 +224,15 @@ public class CacheTest {
@Test
public void testMultiThreadConsistency() {
int size = 51200;
Cache<Integer, Integer> lCache = new Cache<>(size / 2);
int[] answers = new int[size];
Random rand = new Random();
ConcurrentHashMap<Integer, Integer> expectedStoredValues = new ConcurrentHashMap<>();
for (int i = 0; i < answers.length; i++) {
answers[i] = rand.nextInt();
lCache.set(i, answers[i]);
}
Runnable readWriteCheck = new Runnable(){
@Override
public void run() {
for (int i = 0; i < size; i++) {
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
if (lCache.get(i) != null) {
assertEquals(answers[i], lCache.get(i), "Accessor at: " + i);
} else {
lCache.set(i, answers[i]);
expectedStoredValues.put(i, answers[i]);
}
}
}