Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -143,22 +144,41 @@ public void testThreadReadLock() {
}

@Test
public void testAcquireReadLockWhileWriting() {
public void testAcquireReadLockWhileWriting() throws InterruptedException {
StampedWriterPreferredLock lock = new StampedWriterPreferredLock();
lock.writeLock();
AtomicInteger counter = new AtomicInteger();
new Thread(
CountDownLatch readerFinished = new CountDownLatch(1);
Thread readerThread =
new Thread(
() -> {
lock.threadReadLock();
counter.incrementAndGet();
lock.threadReadUnlock();
})
.start();
// block reader util writer release write lock
Assert.assertEquals(0, counter.get());
lock.writeUnlock();
Awaitility.await().atMost(2, TimeUnit.SECONDS).until(() -> counter.get() == 1);
Assert.assertEquals(1, counter.get());
try {
counter.incrementAndGet();
} finally {
lock.threadReadUnlock();
readerFinished.countDown();
}
});
readerThread.setDaemon(true);
readerThread.start();

boolean writeLocked = true;
try {
// block reader until writer release write lock
Awaitility.await()
.atMost(10, TimeUnit.SECONDS)
.until(() -> readerThread.getState() == Thread.State.WAITING);
Assert.assertEquals(0, counter.get());
lock.writeUnlock();
writeLocked = false;
Assert.assertTrue(readerFinished.await(10, TimeUnit.SECONDS));
Assert.assertEquals(1, counter.get());
} finally {
if (writeLocked) {
lock.writeUnlock();
}
}
}

@Test
Expand Down
Loading