aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3
diff options
context:
space:
mode:
authorJochen Wiedmann <jochen.wiedmann@gmail.com>2020-05-31 22:18:02 +0200
committerJochen Wiedmann <jochen.wiedmann@gmail.com>2020-05-31 22:18:02 +0200
commit3d4ed4a8ac63db1e51601ffc31fed44dccbb276c (patch)
treed2e44bfd4d3e304accc0b36abaac0232b4ce0f86 /src/test/java/org/apache/commons/lang3
parentf544897e49fc6c3e3970bf06ff74940bcd6d3505 (diff)
downloadapache-commons-lang-3d4ed4a8ac63db1e51601ffc31fed44dccbb276c.tar.gz
Fixing Checkstyle warnings.
Closes #532.
Diffstat (limited to 'src/test/java/org/apache/commons/lang3')
-rw-r--r--src/test/java/org/apache/commons/lang3/LocksTest.java115
1 files changed, 69 insertions, 46 deletions
diff --git a/src/test/java/org/apache/commons/lang3/LocksTest.java b/src/test/java/org/apache/commons/lang3/LocksTest.java
index e2dd6b0b8..5b0801395 100644
--- a/src/test/java/org/apache/commons/lang3/LocksTest.java
+++ b/src/test/java/org/apache/commons/lang3/LocksTest.java
@@ -16,58 +16,81 @@
*/
package org.apache.commons.lang3;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.function.LongConsumer;
import org.apache.commons.lang3.Functions.FailableConsumer;
import org.apache.commons.lang3.Locks.Lock;
import org.junit.jupiter.api.Test;
class LocksTest {
- @Test
- void testReadLock() throws Exception {
- final long DELAY=3000;
- final boolean[] booleanValues = new boolean[10];
- final Lock<boolean[]> lock = Locks.lock(booleanValues);
- final boolean[] runningValues = new boolean[10];
+ private static final int NUMBER_OF_THREADS = 10;
+
+ @Test
+ void testReadLock() throws Exception {
+ final long DELAY=3000;
+ /** If our threads are running concurrently, then we expect to be faster
+ * than running one after the other.
+ */
+ runTest(DELAY, false, (l) -> assertTrue(l < NUMBER_OF_THREADS*DELAY));
+ }
+
+ void testWriteLock() throws Exception {
+ final long DELAY = 100;
+ /** If our threads are running concurrently, then we expect to be no faster
+ * than running one after the other.
+ */
+ runTest(DELAY, true, (l) -> assertTrue(l >= NUMBER_OF_THREADS*DELAY));
+ }
+
+ private void runTest(long delay, boolean exclusiveLock, LongConsumer runTimeCheck) throws InterruptedException {
+ final boolean[] booleanValues = new boolean[10];
+ final Lock<boolean[]> lock = Locks.lock(booleanValues);
+ final boolean[] runningValues = new boolean[10];
- 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(DELAY);
- b[index] = true;
- modify(runningValues, index, false);
- };
- final Thread t = new Thread(() -> lock.runReadLocked(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]);
- }
- // If our threads would be running in exclusive mode, then we'd need
- // at least DELAY milliseconds for each.
- assertTrue((endTime-startTime) < booleanValues.length*DELAY);
- }
+ 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(delay);
+ b[index] = true;
+ modify(runningValues, index, false);
+ };
+ final Thread t = new Thread(() -> {
+ if (exclusiveLock) {
+ lock.runWriteLocked(consumer);
+ } else {
+ lock.runReadLocked(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]);
+ }
+ runTimeCheck.accept(endTime-startTime);
+ }
- protected void modify(boolean[] booleanArray, int offset, boolean value) {
- synchronized(booleanArray) {
- booleanArray[offset] = value;
- }
- }
- protected boolean someValueIsTrue(boolean[] booleanArray) {
- synchronized(booleanArray) {
- for (int i = 0; i < booleanArray.length; i++) {
- if (booleanArray[i]) {
- return true;
- }
- }
- return false;
- }
- }
+ protected void modify(boolean[] booleanArray, int offset, boolean value) {
+ synchronized(booleanArray) {
+ booleanArray[offset] = value;
+ }
+ }
+ protected boolean someValueIsTrue(boolean[] booleanArray) {
+ synchronized(booleanArray) {
+ for (int i = 0; i < booleanArray.length; i++) {
+ if (booleanArray[i]) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
}