|
|
|
@ -17,8 +17,121 @@
|
|
|
|
|
|
|
|
|
|
package cn.hippo4j.config.toolkit;
|
|
|
|
|
|
|
|
|
|
import cn.hippo4j.common.toolkit.Assert;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SimpleReadWriteLock Test
|
|
|
|
|
*/
|
|
|
|
|
public class SimpleReadWriteLockTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void singleTryReadLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryReadLock();
|
|
|
|
|
Assert.isTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void multiTryReadLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
simpleReadWriteLock.tryReadLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryReadLock();
|
|
|
|
|
Assert.isTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void singleTryWriteLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
Assert.isTrue(result);
|
|
|
|
|
}
|
|
|
|
|
@Test
|
|
|
|
|
public void multiTryWriteLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
Assert.isTrue(!result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void tryReadWriteLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
simpleReadWriteLock.tryReadLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
Assert.isTrue(!result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void tryWriteReadLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryReadLock();
|
|
|
|
|
Assert.isTrue(!result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void releaseReadLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
simpleReadWriteLock.tryReadLock();
|
|
|
|
|
simpleReadWriteLock.releaseReadLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
Assert.isTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void releaseWriteLockTest() {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
simpleReadWriteLock.tryWriteLock();
|
|
|
|
|
simpleReadWriteLock.releaseWriteLock();
|
|
|
|
|
boolean result = simpleReadWriteLock.tryReadLock();
|
|
|
|
|
Assert.isTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void multiThreadTryWriteLockTest() throws Exception {
|
|
|
|
|
SimpleReadWriteLock simpleReadWriteLock = new SimpleReadWriteLock();
|
|
|
|
|
CountDownLatch countDownLatch = new CountDownLatch(2);
|
|
|
|
|
new Thread(() -> {
|
|
|
|
|
try {
|
|
|
|
|
TimeUnit.SECONDS.sleep(1);
|
|
|
|
|
while (true) {
|
|
|
|
|
if (simpleReadWriteLock.tryWriteLock()) {
|
|
|
|
|
System.out.println(Thread.currentThread() + " -1 get write lock success");
|
|
|
|
|
TimeUnit.SECONDS.sleep(5);
|
|
|
|
|
System.out.println(Thread.currentThread() + " -1 execute done");
|
|
|
|
|
simpleReadWriteLock.releaseWriteLock();
|
|
|
|
|
countDownLatch.countDown();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
new Thread(() -> {
|
|
|
|
|
try {
|
|
|
|
|
TimeUnit.SECONDS.sleep(2);
|
|
|
|
|
while (true) {
|
|
|
|
|
if (simpleReadWriteLock.tryWriteLock()){
|
|
|
|
|
System.out.println(Thread.currentThread() + " -2 get write lock success");
|
|
|
|
|
TimeUnit.SECONDS.sleep(1);
|
|
|
|
|
System.out.println(Thread.currentThread() + " -2 execute done");
|
|
|
|
|
countDownLatch.countDown();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}finally {
|
|
|
|
|
simpleReadWriteLock.releaseWriteLock();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
countDownLatch.await();
|
|
|
|
|
Assert.isTrue(simpleReadWriteLock.tryWriteLock());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|