INT-4083: RedisLockRegistryTests - Diagnostics

JIRA: https://jira.spring.io/browse/INT-4083
This commit is contained in:
Gary Russell
2016-07-29 11:11:57 -04:00
parent 6eba49c625
commit ebbd28c415
3 changed files with 34 additions and 13 deletions

View File

@@ -87,7 +87,7 @@ import org.springframework.util.Assert;
*/
public final class RedisLockRegistry implements LockRegistry {
private static final Log logger = LogFactory.getLog(LockRegistry.class);
private static final Log logger = LogFactory.getLog(RedisLockRegistry.class);
private static final byte[] hostName;
@@ -444,7 +444,7 @@ public final class RedisLockRegistry implements LockRegistry {
else {
this.thread = currentThread;
if (logger.isDebugEnabled()) {
logger.debug("New lock; " + this.toString());
logger.debug("New lock; " + this);
}
}
@@ -481,9 +481,9 @@ public final class RedisLockRegistry implements LockRegistry {
public void unlock() {
if (!Thread.currentThread().equals(this.thread)) {
if (this.thread == null) {
throw new IllegalStateException("Lock is not locked; " + this.toString());
throw new IllegalStateException("Lock is not locked; " + this);
}
throw new IllegalStateException("Lock is owned by " + this.thread.getName() + "; " + this.toString());
throw new IllegalStateException("Lock is owned by " + this.thread.getName() + "; " + this);
}
try {
@@ -492,7 +492,7 @@ public final class RedisLockRegistry implements LockRegistry {
this.assertLockInRedisIsUnchanged();
RedisLockRegistry.this.redisTemplate.delete(constructLockKey());
if (logger.isDebugEnabled()) {
logger.debug("Released lock; " + this.toString());
logger.debug("Released lock; " + this);
}
}
finally {
@@ -512,8 +512,8 @@ public final class RedisLockRegistry implements LockRegistry {
RedisLock lockInStore = RedisLockRegistry.this.redisTemplate.boundValueOps(
constructLockKey()).get();
if (lockInStore == null || !this.equals(lockInStore)) {
throw new IllegalStateException("Lock was released due to expiration; " + this.toString()
+ (lockInStore == null ? "" : "; lock in store: " + lockInStore.toString()));
throw new IllegalStateException("Lock was released due to expiration; " + this
+ (lockInStore == null ? "" : "; lock in store: " + lockInStore));
}
}

View File

@@ -38,8 +38,12 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -47,6 +51,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.test.rule.Log4jLevelAdjuster;
import org.springframework.integration.test.util.TestUtils;
/**
@@ -57,6 +62,11 @@ import org.springframework.integration.test.util.TestUtils;
*/
public class RedisLockRegistryTests extends RedisAvailableTests {
private final Log logger = LogFactory.getLog(getClass());
@Rule
public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, "org.springframework.integration.redis");
@Before
@After
public void setupShutDown() {
@@ -282,10 +292,16 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("Interrupted while locking: " + lock2, e);
}
finally {
lock2.unlock();
latch3.countDown();
try {
lock2.unlock();
latch3.countDown();
}
catch (IllegalStateException e) {
logger.error("Failed to unlock: " + lock2, e);
}
}
}
});