yangyoupeng

添加对excludelock的多线程测试。

excludelock的expire超时的设置调整
......@@ -89,11 +89,10 @@ public class ExcludeLock {
while (timeout >= 0) {
final Lock newLock = asLock(System.currentTimeMillis() + lockExpiryInMillis);
Boolean result = valuOper.setIfAbsent(newLock.toString());
valuOper.expire(lockExpiryInMillis,TimeUnit.MILLISECONDS);
logger.debug("new lock [{}] setIfAbsent result is [{}]",newLock,result);
// final String currentValueStr1 = valuOper.get();
// System.out.println(currentValueStr1);
if (result) {
Boolean expireSetResult=valuOper.expire(lockExpiryInMillis,TimeUnit.MILLISECONDS);
this.lock = newLock;
return true;
}
......@@ -108,7 +107,7 @@ public class ExcludeLock {
return true;
}
}
}
}
timeout -= DEFAULT_ACQUIRY_RESOLUTION_MILLIS;
TimeUnit.MILLISECONDS.sleep(DEFAULT_ACQUIRY_RESOLUTION_MILLIS);
logger.debug("retry to lock the resouce with lock key:{} after sleep for {} milliseconds",lockKeyPath,DEFAULT_ACQUIRY_RESOLUTION_MILLIS);
......
......@@ -55,14 +55,18 @@ public class Token<T> {
public boolean addToken(String key,T token,int expiryInMillis){
BoundValueOperations<String, T> valuOper = redisTemplate.boundValueOps(buildTokenKeyWithNameSpace(key));
boolean result=valuOper.setIfAbsent(token);
valuOper.expire(expiryInMillis, TimeUnit.MILLISECONDS);
if(result){
valuOper.expire(expiryInMillis, TimeUnit.MILLISECONDS);
}
return result;
}
public boolean addToken(String key,T token){
BoundValueOperations<String, T> valuOper = redisTemplate.boundValueOps(buildTokenKeyWithNameSpace(key));
boolean result=valuOper.setIfAbsent(token);
valuOper.expire(getTokenExpiryInMillis(), TimeUnit.MILLISECONDS);
boolean result =valuOper.setIfAbsent(token);
if(result){
valuOper.expire(getTokenExpiryInMillis(), TimeUnit.MILLISECONDS);
}
return result;
}
private String buildTokenKeyWithNameSpace(String key){
......@@ -70,8 +74,8 @@ public class Token<T> {
}
public void updateToken(String key,T token){
BoundValueOperations<String, T> valuOper = redisTemplate.boundValueOps(buildTokenKeyWithNameSpace(key));
valuOper.expire(getTokenExpiryInMillis(), TimeUnit.MILLISECONDS);
valuOper.set(token);
valuOper.expire(getTokenExpiryInMillis(), TimeUnit.MILLISECONDS);
}
public T getToken(String key){
......
......@@ -44,10 +44,10 @@ public class TestExcludeLock {
ExcludeLock lock = new ExcludeLock(factory, testLock);
Boolean lockAcquirereuslt = lock.acquireLock();
assertTrue(lockAcquirereuslt);
Thread.sleep(3000);
ExcludeLock lock2 = new ExcludeLock(factory, testLock);
Boolean lockAcquirereuslt2 = lock2.acquireLock();
assertFalse(lockAcquirereuslt2);
assertTrue(lockAcquirereuslt2);
lock.getRedisTemplate().delete(lock.getLockKeyPath());
}
......
......@@ -26,8 +26,8 @@ public class TestThread2 {
}
@Test
public void testRun(){
ExcludeLock lock = new ExcludeLock(factory, testLock);
int num=10;
ExcludeLock lock = new ExcludeLock(factory, testLock,3000,30000);
int num=3;
CountDownLatch latch=new CountDownLatch(num);
for(int i=0;i<num;i++){
......@@ -76,7 +76,8 @@ public class TestThread2 {
factory.setHostName("192.168.0.188");
factory.setPort(6377);
factory.setUsePool(true);
factory.afterPropertiesSet();
// factory.setDatabase(2);
// factory.afterPropertiesSet();
return factory;
}
......
......@@ -48,7 +48,7 @@ public class TestMultiSubmitToken extends RepositoryCommonBase {
assertTrue(addResult);
Thread.sleep(50);
Thread.sleep(200);
addResult=submitToken.addSubmitToken(testTokenName, 2L);
......