use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.
the class RedisLockRegistryTests method testTwoThreadsSecondFailsToGetLock.
@Test
@RedisAvailable
public void testTwoThreadsSecondFailsToGetLock() throws Exception {
final RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
final Lock lock1 = registry.obtain("foo");
lock1.lockInterruptibly();
final AtomicBoolean locked = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
Lock lock2 = registry.obtain("foo");
locked.set(lock2.tryLock(200, TimeUnit.MILLISECONDS));
latch.countDown();
try {
lock2.unlock();
} catch (IllegalStateException ise) {
return ise;
}
return null;
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertFalse(locked.get());
lock1.unlock();
Object ise = result.get(10, TimeUnit.SECONDS);
assertThat(ise, instanceOf(IllegalStateException.class));
assertThat(((Exception) ise).getMessage(), containsString("You do not own lock at"));
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 testTwoThreadsWrongOneUnlocks.
@Test
@RedisAvailable
public void testTwoThreadsWrongOneUnlocks() throws Exception {
final RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
final Lock lock = registry.obtain("foo");
lock.lockInterruptibly();
final AtomicBoolean locked = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
try {
lock.unlock();
} catch (IllegalStateException ise) {
latch.countDown();
return ise;
}
return null;
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertFalse(locked.get());
lock.unlock();
Object ise = result.get(10, TimeUnit.SECONDS);
assertThat(ise, instanceOf(IllegalStateException.class));
assertThat(((Exception) ise).getMessage(), containsString("You do not own lock at"));
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 testReentrantLock.
@Test
@RedisAvailable
public void testReentrantLock() throws Exception {
RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
for (int i = 0; i < 10; i++) {
Lock lock1 = registry.obtain("foo");
lock1.lock();
try {
Lock lock2 = registry.obtain("foo");
assertSame(lock1, lock2);
lock2.lock();
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 RedisQueueGatewayIntegrationTests method testRequestReplyWithMessage.
@Test
@RedisAvailable
public void testRequestReplyWithMessage() throws Exception {
this.inboundGateway.setSerializer(new JdkSerializationRedisSerializer());
this.inboundGateway.setExtractPayload(false);
this.outboundGateway.setSerializer(new JdkSerializationRedisSerializer());
this.outboundGateway.setExtractPayload(false);
this.sendChannel.send(new GenericMessage<Integer>(2));
Message<?> receive = this.outputChannel.receive(10000);
assertNotNull(receive);
assertEquals(3, receive.getPayload());
this.inboundGateway.setSerializer(new StringRedisSerializer());
this.inboundGateway.setExtractPayload(true);
this.outboundGateway.setSerializer(new StringRedisSerializer());
this.outboundGateway.setExtractPayload(true);
}
Aggregations