You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/07/23 13:25:15 UTC
[commons-lang] branch master updated: Better names in test. Use
final. Sort members.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 3276da8 Better names in test. Use final. Sort members.
new 37de491 Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-lang.git
3276da8 is described below
commit 3276da82f0b592741088ff0181c0c89df7d279cc
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 23 09:24:53 2020 -0400
Better names in test. Use final. Sort members.
---
.../concurrent/locks/LockingVisitorsTest.java | 140 ++++++++++-----------
1 file changed, 70 insertions(+), 70 deletions(-)
diff --git a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java
index aba1d20..8a92343 100644
--- a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java
+++ b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java
@@ -28,51 +28,80 @@ import org.apache.commons.lang3.function.FailableConsumer;
import org.junit.jupiter.api.Test;
public class LockingVisitorsTest {
- private static final int NUMBER_OF_THREADS = 10;
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;
- @Test
- public void testStampedLockNotExclusive() throws Exception {
-
- /*
- * If our threads are running concurrently, then we expect to be faster than running one after the other.
- */
- boolean[] booleanValues = new boolean[10];
- runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues,
- LockingVisitors.stampedLockVisitor(booleanValues));
+ protected boolean containsTrue(final boolean[] booleanArray) {
+ synchronized (booleanArray) {
+ for (final boolean element : booleanArray) {
+ if (element) {
+ return true;
+ }
+ }
+ return false;
+ }
}
- @Test
- public void testReentrantReadWriteLockNotExclusive() throws Exception {
+ private void runTest(final long delayMillis, final boolean exclusiveLock, final LongConsumer runTimeCheck,
+ final boolean[] booleanValues, final LockVisitor<boolean[], ?> visitor) throws InterruptedException {
+ final boolean[] runningValues = new boolean[10];
- /*
- * If our threads are running concurrently, then we expect to be faster than running one after the other.
- */
- boolean[] booleanValues = new boolean[10];
- runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues,
- LockingVisitors.reentrantReadWriteLockVisitor(booleanValues));
+ final long startTime = System.currentTimeMillis();
+ for (int i = 0; i < booleanValues.length; i++) {
+ final int index = i;
+ final FailableConsumer<boolean[], ?> consumer = b -> {
+ b[index] = false;
+ 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);
+ }
+
+ protected void set(final boolean[] booleanArray, final int offset, final boolean value) {
+ synchronized (booleanArray) {
+ booleanArray[offset] = value;
+ }
}
@Test
- public void testStampedLockExclusive() throws Exception {
+ public void testReentrantReadWriteLockExclusive() throws Exception {
/*
* 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,
- LockingVisitors.stampedLockVisitor(booleanValues));
+ LockingVisitors.reentrantReadWriteLockVisitor(booleanValues));
}
@Test
- public void testReentrantReadWriteLockExclusive() throws Exception {
+ public void testReentrantReadWriteLockNotExclusive() 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 faster than running one after the other.
*/
- boolean[] booleanValues = new boolean[10];
- runTest(DELAY_MILLIS, true, l -> assertTrue(l >= TOTAL_DELAY_MILLIS), booleanValues,
+ final boolean[] booleanValues = new boolean[10];
+ runTest(DELAY_MILLIS, false, l -> assertTrue(l < TOTAL_DELAY_MILLIS), booleanValues,
LockingVisitors.reentrantReadWriteLockVisitor(booleanValues));
}
@@ -90,54 +119,25 @@ public class LockingVisitorsTest {
assertNotSame(hidden, o2);
}
- private void runTest(final long delayMillis, final boolean exclusiveLock, final LongConsumer runTimeCheck,
- boolean[] booleanValues, LockVisitor<boolean[], ?> visitor) throws InterruptedException {
- final boolean[] runningValues = new boolean[10];
+ @Test
+ public void testStampedLockExclusive() throws Exception {
- final long startTime = System.currentTimeMillis();
- for (int i = 0; i < booleanValues.length; i++) {
- final int index = i;
- final FailableConsumer<boolean[], ?> consumer = b -> {
- b[index] = false;
- Thread.sleep(delayMillis);
- 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);
+ /*
+ * If our threads are running concurrently, then we expect to be no faster than running one after the other.
+ */
+ final boolean[] booleanValues = new boolean[10];
+ runTest(DELAY_MILLIS, true, l -> assertTrue(l >= TOTAL_DELAY_MILLIS), booleanValues,
+ LockingVisitors.stampedLockVisitor(booleanValues));
}
- protected void modify(final boolean[] booleanArray, final int offset, final boolean value) {
- synchronized (booleanArray) {
- booleanArray[offset] = value;
- }
- }
+ @Test
+ public void testStampedLockNotExclusive() throws Exception {
- protected boolean someValueIsTrue(final boolean[] booleanArray) {
- synchronized (booleanArray) {
- for (int i = 0; i < booleanArray.length; i++) {
- if (booleanArray[i]) {
- return true;
- }
- }
- return false;
- }
+ /*
+ * 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.stampedLockVisitor(booleanValues));
}
}