yangyoupeng

增加测试,优化,减少网络连接

......@@ -92,7 +92,6 @@ public class ExcludeLock {
logger.debug("new lock [{}] setIfAbsent result is [{}]",newLock,result);
if (result) {
Boolean expireSetResult=valuOper.expire(lockExpiryInMillis,TimeUnit.MILLISECONDS);
this.lock = newLock;
return true;
}
......
package com.zhaoonline.redis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
public class TestExpireOfRedis {
@Test
public void testSimpleConnect() throws InterruptedException{
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName("192.168.0.188");
factory.setPort(6377);
factory.setUsePool(true);
factory.afterPropertiesSet();
RedisConnection connection=factory.getConnection();
assertNotNull(connection);
byte[] testKey="yyp:test".getBytes();
connection.del(testKey);
boolean result=connection.setNX(testKey, "helloworld".getBytes());
connection.expire(testKey, 30);
// assertEquals(true, result);
boolean live =true;
while(live){
Long ttl=connection.ttl(testKey);
System.out.println("ttl left:"+ttl);
if(ttl<0){
live=false;
}
Thread.sleep(500);
}
//connection.del(testKey);
factory.destroy();
}
}
......@@ -41,13 +41,13 @@ public class TestExcludeLock {
@Test
public void testfailedAquireLockAfterHasBeenLock() throws InterruptedException{
ExcludeLock lock = new ExcludeLock(factory, testLock);
ExcludeLock lock = new ExcludeLock(factory, testLock,3000,30000);
Boolean lockAcquirereuslt = lock.acquireLock();
assertTrue(lockAcquirereuslt);
Thread.sleep(3000);
ExcludeLock lock2 = new ExcludeLock(factory, testLock);
Boolean lockAcquirereuslt2 = lock2.acquireLock();
assertTrue(lockAcquirereuslt2);
// Thread.sleep(3000);
// ExcludeLock lock2 = new ExcludeLock(factory, testLock);
// Boolean lockAcquirereuslt2 = lock2.acquireLock();
// assertTrue(lockAcquirereuslt2);
lock.getRedisTemplate().delete(lock.getLockKeyPath());
}
......@@ -72,6 +72,7 @@ public class TestExcludeLock {
factory.setHostName("192.168.0.188");
factory.setPort(6377);
factory.setUsePool(true);
factory.setDatabase(0);
factory.afterPropertiesSet();
return factory;
}
......