use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.
the class RedisLockRegistryTests method testTwoThreadsDifferentRegistries.
@Test
@RedisAvailable
public void testTwoThreadsDifferentRegistries() throws Exception {
final RedisLockRegistry registry1 = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
final RedisLockRegistry registry2 = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
final Lock lock1 = registry1.obtain("foo");
final AtomicBoolean locked = new AtomicBoolean();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
lock1.lockInterruptibly();
assertEquals(1, TestUtils.getPropertyValue(registry1, "locks", Map.class).size());
Executors.newSingleThreadExecutor().execute(() -> {
Lock lock2 = registry2.obtain("foo");
try {
latch1.countDown();
lock2.lockInterruptibly();
assertEquals(1, TestUtils.getPropertyValue(registry2, "locks", Map.class).size());
latch2.await(10, TimeUnit.SECONDS);
locked.set(true);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
this.logger.error("Interrupted while locking: " + lock2, e1);
} finally {
try {
lock2.unlock();
latch3.countDown();
} catch (IllegalStateException e2) {
this.logger.error("Failed to unlock: " + lock2, e2);
}
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
assertFalse(locked.get());
lock1.unlock();
latch2.countDown();
assertTrue(latch3.await(10, TimeUnit.SECONDS));
assertTrue(locked.get());
registry1.expireUnusedOlderThan(-1000);
registry2.expireUnusedOlderThan(-1000);
assertEquals(0, TestUtils.getPropertyValue(registry1, "locks", Map.class).size());
assertEquals(0, TestUtils.getPropertyValue(registry2, "locks", Map.class).size());
}
use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.
the class RedisLockRegistryTests method testTwoLocks.
@Test
@RedisAvailable
public void testTwoLocks() throws Exception {
RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
for (int i = 0; i < 10; i++) {
Lock lock1 = registry.obtain("foo");
lock1.lockInterruptibly();
try {
Lock lock2 = registry.obtain("bar");
assertNotSame(lock1, lock2);
lock2.lockInterruptibly();
try {
// just get the lock
} finally {
lock2.unlock();
}
} finally {
lock1.unlock();
}
}
registry.expireUnusedOlderThan(-1000);
assertEquals(0, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
}
use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.
the class RedisLockRegistryTests method testLock.
@Test
@RedisAvailable
public void testLock() throws Exception {
RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
for (int i = 0; i < 10; i++) {
Lock lock = registry.obtain("foo");
lock.lock();
try {
assertEquals(1, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
} finally {
lock.unlock();
}
}
registry.expireUnusedOlderThan(-1000);
assertEquals(0, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
}
use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.
the class RedisLockRegistryTests method testReentrantLockInterruptibly.
@Test
@RedisAvailable
public void testReentrantLockInterruptibly() throws Exception {
RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
for (int i = 0; i < 10; i++) {
Lock lock1 = registry.obtain("foo");
lock1.lockInterruptibly();
try {
Lock lock2 = registry.obtain("foo");
assertSame(lock1, lock2);
lock2.lockInterruptibly();
try {
// just get the lock
} finally {
lock2.unlock();
}
} finally {
lock1.unlock();
}
}
registry.expireUnusedOlderThan(-1000);
assertEquals(0, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
}
use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.
the class RedisLockRegistryTests method testLockInterruptibly.
@Test
@RedisAvailable
public void testLockInterruptibly() throws Exception {
RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
for (int i = 0; i < 10; i++) {
Lock lock = registry.obtain("foo");
lock.lockInterruptibly();
try {
assertEquals(1, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
} finally {
lock.unlock();
}
}
registry.expireUnusedOlderThan(-1000);
assertEquals(0, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
}
Aggregations