Better names in test. Use final. Sort members.

This commit is contained in:
Gary Gregory 2020-07-23 09:24:53 -04:00
parent 4e9460413a
commit 3276da82f0

View File

@ -28,41 +28,59 @@
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class LockingVisitorsTest { public class LockingVisitorsTest {
private static final int NUMBER_OF_THREADS = 10;
private static final long DELAY_MILLIS = 3000; private static final long DELAY_MILLIS = 3000;
private static final int NUMBER_OF_THREADS = 10;
private static final long TOTAL_DELAY_MILLIS = NUMBER_OF_THREADS * DELAY_MILLIS; private static final long TOTAL_DELAY_MILLIS = NUMBER_OF_THREADS * DELAY_MILLIS;
@Test protected boolean containsTrue(final boolean[] booleanArray) {
public void testStampedLockNotExclusive() throws Exception { synchronized (booleanArray) {
for (final boolean element : booleanArray) {
/* if (element) {
* If our threads are running concurrently, then we expect to be faster than running one after the other. return true;
*/ }
boolean[] booleanValues = new boolean[10]; }
runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues, return false;
LockingVisitors.stampedLockVisitor(booleanValues)); }
} }
@Test private void runTest(final long delayMillis, final boolean exclusiveLock, final LongConsumer runTimeCheck,
public void testReentrantReadWriteLockNotExclusive() throws Exception { final boolean[] booleanValues, final LockVisitor<boolean[], ?> visitor) throws InterruptedException {
final boolean[] runningValues = new boolean[10];
/* final long startTime = System.currentTimeMillis();
* If our threads are running concurrently, then we expect to be faster than running one after the other. for (int i = 0; i < booleanValues.length; i++) {
*/ final int index = i;
boolean[] booleanValues = new boolean[10]; final FailableConsumer<boolean[], ?> consumer = b -> {
runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues, b[index] = false;
LockingVisitors.reentrantReadWriteLockVisitor(booleanValues)); Thread.sleep(delayMillis);
b[index] = true;
set(runningValues, index, false);
};
final Thread t = new Thread(() -> {
if (exclusiveLock) {
visitor.acceptWriteLocked(consumer);
} else {
visitor.acceptReadLocked(consumer);
}
});
set(runningValues, i, true);
t.start();
}
while (containsTrue(runningValues)) {
Thread.sleep(100);
}
final long endTime = System.currentTimeMillis();
for (final boolean booleanValue : booleanValues) {
assertTrue(booleanValue);
}
// WRONG assumption
// runTimeCheck.accept(endTime - startTime);
} }
@Test protected void set(final boolean[] booleanArray, final int offset, final boolean value) {
public void testStampedLockExclusive() throws Exception { synchronized (booleanArray) {
booleanArray[offset] = value;
/* }
* If our threads are running concurrently, then we expect to be no faster than running one after the other.
*/
boolean[] booleanValues = new boolean[10];
runTest(DELAY_MILLIS, true, l -> assertTrue(l >= TOTAL_DELAY_MILLIS), booleanValues,
LockingVisitors.stampedLockVisitor(booleanValues));
} }
@Test @Test
@ -71,11 +89,22 @@ public void testReentrantReadWriteLockExclusive() throws Exception {
/* /*
* If our threads are running concurrently, then we expect to be no faster than running one after the other. * If our threads are running concurrently, then we expect to be no faster than running one after the other.
*/ */
boolean[] booleanValues = new boolean[10]; final boolean[] booleanValues = new boolean[10];
runTest(DELAY_MILLIS, true, l -> assertTrue(l >= TOTAL_DELAY_MILLIS), booleanValues, runTest(DELAY_MILLIS, true, l -> assertTrue(l >= TOTAL_DELAY_MILLIS), booleanValues,
LockingVisitors.reentrantReadWriteLockVisitor(booleanValues)); LockingVisitors.reentrantReadWriteLockVisitor(booleanValues));
} }
@Test
public void testReentrantReadWriteLockNotExclusive() throws Exception {
/*
* If our threads are running concurrently, then we expect to be faster than running one after the other.
*/
final boolean[] booleanValues = new boolean[10];
runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues,
LockingVisitors.reentrantReadWriteLockVisitor(booleanValues));
}
@Test @Test
public void testResultValidation() { public void testResultValidation() {
final Object hidden = new Object(); final Object hidden = new Object();
@ -90,54 +119,25 @@ public void testResultValidation() {
assertNotSame(hidden, o2); assertNotSame(hidden, o2);
} }
private void runTest(final long delayMillis, final boolean exclusiveLock, final LongConsumer runTimeCheck, @Test
boolean[] booleanValues, LockVisitor<boolean[], ?> visitor) throws InterruptedException { public void testStampedLockExclusive() throws Exception {
final boolean[] runningValues = new boolean[10];
final long startTime = System.currentTimeMillis(); /*
for (int i = 0; i < booleanValues.length; i++) { * If our threads are running concurrently, then we expect to be no faster than running one after the other.
final int index = i; */
final FailableConsumer<boolean[], ?> consumer = b -> { final boolean[] booleanValues = new boolean[10];
b[index] = false; runTest(DELAY_MILLIS, true, l -> assertTrue(l >= TOTAL_DELAY_MILLIS), booleanValues,
Thread.sleep(delayMillis); LockingVisitors.stampedLockVisitor(booleanValues));
b[index] = true;
modify(runningValues, index, false);
};
final Thread t = new Thread(() -> {
if (exclusiveLock) {
visitor.acceptWriteLocked(consumer);
} else {
visitor.acceptReadLocked(consumer);
}
});
modify(runningValues, i, true);
t.start();
}
while (someValueIsTrue(runningValues)) {
Thread.sleep(100);
}
final long endTime = System.currentTimeMillis();
for (int i = 0; i < booleanValues.length; i++) {
assertTrue(booleanValues[i]);
}
// WRONG assumption
// runTimeCheck.accept(endTime - startTime);
} }
protected void modify(final boolean[] booleanArray, final int offset, final boolean value) { @Test
synchronized (booleanArray) { public void testStampedLockNotExclusive() throws Exception {
booleanArray[offset] = value;
}
}
protected boolean someValueIsTrue(final boolean[] booleanArray) { /*
synchronized (booleanArray) { * If our threads are running concurrently, then we expect to be faster than running one after the other.
for (int i = 0; i < booleanArray.length; i++) { */
if (booleanArray[i]) { final boolean[] booleanValues = new boolean[10];
return true; runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues,
} LockingVisitors.stampedLockVisitor(booleanValues));
}
return false;
}
} }
} }