赞
踩
packagecom.zhi.test;importjava.util.Random;importjava.util.concurrent.CountDownLatch;importorg.apache.commons.dbcp2.BasicDataSource;importorg.apache.commons.lang3.StringUtils;importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importorg.apache.zookeeper.WatchedEvent;importorg.apache.zookeeper.Watcher;importorg.apache.zookeeper.ZooKeeper;importorg.junit.jupiter.api.MethodOrderer;importorg.junit.jupiter.api.Order;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.TestInstance;importorg.junit.jupiter.api.TestInstance.Lifecycle;importorg.junit.jupiter.api.TestMethodOrder;importcom.zhi.util.DatabaseLock;importcom.zhi.util.DistributeLock;importcom.zhi.util.RedisLock;importcom.zhi.util.ZookeeperLock;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;/*** 分布式锁测试
*
*@author张远志
*@since2020年5月3日13:10:43
**/@TestInstance(Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)public classLockTest {private final Logger logger = LogManager.getLogger(this.getClass());private static final String key = "test";
@Order(1)
@Testpublic voidtest1() {
logger.info("开始测试数据库(MySQL)分布式锁-----------------------------------------------");
BasicDataSource dataSource= newBasicDataSource();
dataSource.setUsername("root");
dataSource.setPassword("abc123");
dataSource.setUrl("jdbc:mysql://192.168.59.131:3306/zhi_test");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
doJob(newDatabaseLock(dataSource));
}
@Order(2)
@Testpublic voidtest2() {
logger.info("开始测试Redis分布式锁-----------------------------------------------");
JedisPoolConfig config= newJedisPoolConfig();
config.setMaxIdle(5);
config.setTestOnBorrow(false);
JedisPool jedisPool= new JedisPool(config, "192.168.59.131", 6379);
doJob(newRedisLock(jedisPool));
}
@Order(3)
@Testpublic voidtest3() {
logger.info("开始测试Zookeeper分布式锁-----------------------------------------------");
ZooKeeper zooKeeper= null;try{
zooKeeper= new ZooKeeper("192.168.59.131:2181", 60000, newWatcher() {public voidprocess(WatchedEvent event) {
logger.info("事件类型:{},路径:{}", event.getType(), event.getPath());
}
});
doJob(newZookeeperLock(zooKeeper));
}catch(Exception e) {
}finally{try{
zooKeeper.close();
}catch(Exception e) {
}
}
}private voiddoJob(DistributeLock locker) {int jobCount = 100;
CountDownLatch latch= newCountDownLatch(jobCount);for (int i = 0; i < jobCount; i++) {newWorker(i, locker, latch).start();
}try{
latch.await();
}catch(InterruptedException e) {
}
}class Worker extendsThread {privateDistributeLock locker;privateCountDownLatch latch;public Worker(inttNo, DistributeLock locker, CountDownLatch latch) {super("线程" + StringUtils.leftPad(String.valueOf(tNo), 3, "0"));this.locker =locker;this.latch =latch;
}
@Overridepublic voidrun() {
LockTest.doSleep(new Random().nextInt(20) * 1000);long startTime =System.currentTimeMillis();if(locker.lock(key, getName())) {
logger.info("{}申请到锁,申请动作耗时{}毫秒", getName(), System.currentTimeMillis() -startTime);
LockTest.doSleep(1000);boolean flag =locker.unlock(key, getName());
logger.info("{}释放锁{}", getName(), flag ? "成功" : "失败");
}
latch.countDown();
}
}public static void doSleep(longmillis) {try{
Thread.sleep(millis);
}catch(Exception e) {
}
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。