Search in sources :

Example 96 with RedisAvailable

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Lock(java.util.concurrent.locks.Lock) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 97 with RedisAvailable

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Lock(java.util.concurrent.locks.Lock) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 98 with RedisAvailable

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());
}
Also used : Lock(java.util.concurrent.locks.Lock) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 99 with RedisAvailable

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);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)99 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)99 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)62 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)28 GenericMessage (org.springframework.messaging.support.GenericMessage)24 BeanFactory (org.springframework.beans.factory.BeanFactory)23 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)16 MessageGroup (org.springframework.integration.store.MessageGroup)15 ArrayList (java.util.ArrayList)14 Lock (java.util.concurrent.locks.Lock)13 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)13 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)13 Message (org.springframework.messaging.Message)13 QueueChannel (org.springframework.integration.channel.QueueChannel)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)9 List (java.util.List)8 DefaultRedisList (org.springframework.data.redis.support.collections.DefaultRedisList)8 RedisList (org.springframework.data.redis.support.collections.RedisList)8