aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java
blob: d8d1a453d848f711c4376d9e062051b494d120b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.lang3.concurrent.locks;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Duration;
import java.util.function.LongConsumer;

import org.apache.commons.lang3.AbstractLangTest;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ThreadUtils;
import org.apache.commons.lang3.concurrent.locks.LockingVisitors.LockVisitor;
import org.apache.commons.lang3.concurrent.locks.LockingVisitors.StampedLockVisitor;
import org.apache.commons.lang3.function.FailableConsumer;
import org.junit.jupiter.api.Test;

public class LockingVisitorsTest extends AbstractLangTest {

    private static final Duration SHORT_DELAY = Duration.ofMillis(100);
    private static final Duration DELAY = Duration.ofMillis(1500);
    private static final int NUMBER_OF_THREADS = 10;
    private static final Duration TOTAL_DELAY = DELAY.multipliedBy(NUMBER_OF_THREADS);

    protected boolean containsTrue(final boolean[] booleanArray) {
        synchronized (booleanArray) {
            return ArrayUtils.contains(booleanArray, true);
        }
    }

    private void runTest(final Duration delay, final boolean exclusiveLock, final LongConsumer runTimeCheck,
        final boolean[] booleanValues, final LockVisitor<boolean[], ?> visitor) throws InterruptedException {
        final boolean[] runningValues = new boolean[10];

        final long startTimeMillis = System.currentTimeMillis();
        for (int i = 0; i < booleanValues.length; i++) {
            final int index = i;
            final FailableConsumer<boolean[], ?> consumer = b -> {
                b[index] = false;
                ThreadUtils.sleep(delay);
                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)) {
            ThreadUtils.sleep(SHORT_DELAY);
        }
        final long endTimeMillis = System.currentTimeMillis();
        for (final boolean booleanValue : booleanValues) {
            assertTrue(booleanValue);
        }
        // WRONG assumption
        // runTimeCheck.accept(endTimeMillis - startTimeMillis);
    }

    protected void set(final boolean[] booleanArray, final int offset, final boolean value) {
        synchronized (booleanArray) {
            booleanArray[offset] = value;
        }
    }

    @Test
    public void testReentrantReadWriteLockExclusive() throws Exception {

        /*
         * 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, true, millis -> assertTrue(millis >= TOTAL_DELAY.toMillis()), 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, false, millis -> assertTrue(millis < TOTAL_DELAY.toMillis()), booleanValues,
            LockingVisitors.reentrantReadWriteLockVisitor(booleanValues));
    }

    @Test
    public void testResultValidation() {
        final Object hidden = new Object();
        final StampedLockVisitor<Object> lock = LockingVisitors.stampedLockVisitor(hidden);
        final Object o1 = lock.applyReadLocked(h -> new Object());
        assertNotNull(o1);
        assertNotSame(hidden, o1);
        final Object o2 = lock.applyWriteLocked(h -> new Object());
        assertNotNull(o2);
        assertNotSame(hidden, o2);
    }

    @Test
    public void testStampedLockExclusive() throws Exception {

        /*
         * 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, true, millis -> assertTrue(millis >= TOTAL_DELAY.toMillis()), booleanValues,
            LockingVisitors.stampedLockVisitor(booleanValues));
    }

    @Test
    public void testStampedLockNotExclusive() 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, false, millis -> assertTrue(millis < TOTAL_DELAY.toMillis()), booleanValues,
            LockingVisitors.stampedLockVisitor(booleanValues));
    }
}