Search in sources :

Example 91 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testMessageGroupWithAddedMessage.

@Test
@RedisAvailable
public void testMessageGroupWithAddedMessage() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    Message<?> message = new GenericMessage<>("Hello");
    MessageGroup messageGroup = store.addMessageToGroup(this.groupId, message);
    assertEquals(1, messageGroup.size());
    // make sure the store is properly rebuild from Redis
    store = new RedisMessageStore(jcf);
    messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(1, messageGroup.size());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 92 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testRemoveMessageFromTheGroup.

@Test
@RedisAvailable
public void testRemoveMessageFromTheGroup() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    Message<?> message = new GenericMessage<>("2");
    store.addMessagesToGroup(messageGroup.getGroupId(), new GenericMessage<>("1"), message);
    messageGroup = store.addMessageToGroup(messageGroup.getGroupId(), new GenericMessage<>("3"));
    assertEquals(3, messageGroup.size());
    store.removeMessagesFromGroup(this.groupId, message);
    messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(2, messageGroup.size());
    // make sure the store is properly rebuild from Redis
    store = new RedisMessageStore(jcf);
    messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(2, messageGroup.size());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 93 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testIteratorOfMessageGroups.

@Test
@RedisAvailable
public void testIteratorOfMessageGroups() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store1 = new RedisMessageStore(jcf);
    RedisMessageStore store2 = new RedisMessageStore(jcf);
    store1.removeMessageGroup(this.groupId);
    UUID group2 = UUID.randomUUID();
    store1.removeMessageGroup(group2);
    UUID group3 = UUID.randomUUID();
    store1.removeMessageGroup(group3);
    store1.addMessagesToGroup(this.groupId, new GenericMessage<>("1"));
    store2.addMessagesToGroup(group2, new GenericMessage<>("2"));
    store1.addMessagesToGroup(group3, new GenericMessage<>("3"), new GenericMessage<>("3A"));
    Iterator<MessageGroup> messageGroups = store1.iterator();
    int counter = 0;
    while (messageGroups.hasNext()) {
        MessageGroup group = messageGroups.next();
        String groupId = (String) group.getGroupId();
        if (groupId.equals("1")) {
            assertEquals(1, group.getMessages().size());
        } else if (groupId.equals("2")) {
            assertEquals(1, group.getMessages().size());
        } else if (groupId.equals("3")) {
            assertEquals(2, group.getMessages().size());
        }
        counter++;
    }
    assertEquals(3, counter);
    store2.removeMessageGroup(group3);
    messageGroups = store1.iterator();
    counter = 0;
    while (messageGroups.hasNext()) {
        messageGroups.next();
        counter++;
    }
    assertEquals(2, counter);
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Matchers.containsString(org.hamcrest.Matchers.containsString) UUID(java.util.UUID) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 94 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisLockRegistryTests method testTwoThreads.

@Test
@RedisAvailable
public void testTwoThreads() throws Exception {
    final RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
    final Lock lock1 = registry.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(registry, "locks", Map.class).size());
    Executors.newSingleThreadExecutor().execute(() -> {
        Lock lock2 = registry.obtain("foo");
        try {
            latch1.countDown();
            lock2.lockInterruptibly();
            assertEquals(1, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
            latch2.await(10, TimeUnit.SECONDS);
            locked.set(true);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock2.unlock();
            latch3.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertFalse(locked.get());
    lock1.unlock();
    latch2.countDown();
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    assertTrue(locked.get());
    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 95 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisLockRegistryTests method testExpireNotChanged.

@Test
@RedisAvailable
public void testExpireNotChanged() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    final RedisLockRegistry registry = new RedisLockRegistry(connectionFactory, this.registryKey, 10000);
    Lock lock = registry.obtain("foo");
    lock.lock();
    Long expire = getExpire(registry, "foo");
    Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
        Lock lock2 = registry.obtain("foo");
        assertFalse(lock2.tryLock());
        return null;
    });
    result.get();
    assertEquals(expire, getExpire(registry, "foo"));
    lock.unlock();
}
Also used : RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) Lock(java.util.concurrent.locks.Lock) 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