add SimpleReadWriteLockTest test case (#710)

* add MapUtilTest test case

* add RequestUtilTest test case

* add SimpleReadWriteLockTest test case
pull/713/head
monsterxxp 2 years ago committed by GitHub
parent deafe26db4
commit f1d3ed9d60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,8 +17,121 @@
package cn.hippo4j.config.toolkit; 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 * SimpleReadWriteLock Test
*/ */
public class SimpleReadWriteLockTest { 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());
}
} }

Loading…
Cancel
Save