提交 ece80f36 编写于 作者: W wuqian30624

Add testcase

Signed-off-by: Nwuqian30624 <wuqian0808@me.com>
上级 167d507c
......@@ -212,7 +212,7 @@ public class RedissonFairLock extends RedissonLock implements RLock {
"if (timeout > tonumber(ARGV[4])) and (redis.call('exists', KEYS[1]) == 0) then " +
"redis.call('lpop', KEYS[2]);" +
"redis.call('zrem', KEYS[3], firstThreadId);" +
"else" +
"else " +
"break;" +
"end;" +
"end;" +
......
......@@ -933,5 +933,62 @@ public class RedissonFairLockTest extends BaseConcurrentTest {
await().atMost(30, TimeUnit.SECONDS).until(() -> lockedCounter.get() == totalThreads);
}
@Test
public void testLockBlock() throws InterruptedException{
Config cfg = createConfig();
cfg.useSingleServer().setSubscriptionsPerConnection(100);
RedissonClient redisson = Redisson.create(cfg);
int totalExecutorCount = 5;
int totalThreadCount = 100;
int interval = 1000;
Lock lock = redisson.getFairLock("testLockBlock");
for (int count = 0; count < totalExecutorCount; count++) {
ExecutorService executor = Executors.newFixedThreadPool(totalThreadCount);
for (int i = 0; i < totalThreadCount; i++) {
final int finalI = i;
executor.submit(() -> {
log.info("running " + finalI + " in thread " + Thread.currentThread().getId());
try {
lock.lock();
log.info("Thread " + finalI + " got lock");
} catch (Exception ex) {
log.error("Failed to get lock");
} finally {
lock.unlock();
}
});
}
executor.shutdownNow();
}
redisson.shutdown();
// In case connection closed
redisson = Redisson.create(cfg);
long timeOut = redisson.getConfig().getLockWatchdogTimeout() + interval;
ExecutorService lockExecutor = Executors.newFixedThreadPool(1);
Lock lockSecond = redisson.getFairLock("testLockBlock");
Future<Boolean> future = lockExecutor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
// check if this lock can be acquired in short time
Thread.sleep(timeOut);
lockSecond.lock();
return Boolean.TRUE;
}
});
Boolean got = Boolean.FALSE;
try{
got = future.get(timeOut + interval, TimeUnit.MILLISECONDS);
if (got) {
log.info("Got lock immediately after startup");
}else{
log.info("Failed to get lock due to blocked");
}
}catch (Exception e){
log.info("Failed to get lock due to blocked");
}
Assert.assertTrue(got);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册