Search in sources :

Example 51 with RedisAvailable

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

the class RedisLockRegistryTests method testEquals.

@Test
@RedisAvailable
public void testEquals() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    RedisLockRegistry registry1 = new RedisLockRegistry(connectionFactory, this.registryKey);
    RedisLockRegistry registry2 = new RedisLockRegistry(connectionFactory, this.registryKey);
    RedisLockRegistry registry3 = new RedisLockRegistry(connectionFactory, this.registryKey2);
    Lock lock1 = registry1.obtain("foo");
    Lock lock2 = registry1.obtain("foo");
    assertEquals(lock1, lock2);
    lock1.lock();
    lock2.lock();
    assertEquals(lock1, lock2);
    lock1.unlock();
    lock2.unlock();
    assertEquals(lock1, lock2);
    lock1 = registry1.obtain("foo");
    lock2 = registry2.obtain("foo");
    assertNotEquals(lock1, lock2);
    lock1.lock();
    assertFalse(lock2.tryLock());
    lock1.unlock();
    lock1 = registry1.obtain("foo");
    lock2 = registry3.obtain("foo");
    assertNotEquals(lock1, lock2);
    lock1.lock();
    lock2.lock();
    lock1.unlock();
    lock2.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)

Example 52 with RedisAvailable

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

the class RedisLockRegistryTests method testThreadLocalListLeaks.

@Test
@RedisAvailable
public void testThreadLocalListLeaks() {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey, 100);
    for (int i = 0; i < 10; i++) {
        registry.obtain("foo" + i);
    }
    assertEquals(10, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
    for (int i = 0; i < 10; i++) {
        Lock lock = registry.obtain("foo" + i);
        lock.lock();
    }
    assertEquals(10, TestUtils.getPropertyValue(registry, "locks", Map.class).size());
    for (int i = 0; i < 10; i++) {
        Lock lock = registry.obtain("foo" + i);
        lock.unlock();
    }
    assertEquals(10, 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 53 with RedisAvailable

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

the class SearchReceivingMessageSourceWithRedisTests method testPollForTweetsThreeResultsWithRedisMetadataStore.

@Test
@RedisAvailable
public void testPollForTweetsThreeResultsWithRedisMetadataStore() throws Exception {
    String metadataKey = TestUtils.getPropertyValue(twitterSearchAdapter, "source.metadataKey", String.class);
    // There is need to set a value, not 'remove' and re-init 'twitterMessageSource'
    this.metadataStore.put(metadataKey, "-1");
    this.twitterMessageSource.afterPropertiesSet();
    MetadataStore metadataStore = TestUtils.getPropertyValue(this.twitterSearchAdapter, "source.metadataStore", MetadataStore.class);
    assertTrue("Expected metadataStore to be an instance of RedisMetadataStore", metadataStore instanceof RedisMetadataStore);
    assertSame(this.metadataStore, metadataStore);
    assertEquals("twitterSearchAdapter.74", metadataKey);
    this.twitterSearchAdapter.start();
    Message<?> receive = this.tweets.receive(10000);
    assertNotNull(receive);
    receive = this.tweets.receive(10000);
    assertNotNull(receive);
    receive = this.tweets.receive(10000);
    assertNotNull(receive);
    /* We received 3 messages so far. When invoking receive() again the search
		 * will return again the 3 test Tweets but as we already processed them
		 * no message (null) is returned. */
    assertNull(this.tweets.receive(0));
    String persistedMetadataStoreValue = this.metadataStore.get(metadataKey);
    assertNotNull(persistedMetadataStoreValue);
    assertEquals("3", persistedMetadataStoreValue);
    this.twitterSearchAdapter.stop();
    this.metadataStore.put(metadataKey, "1");
    this.twitterMessageSource.afterPropertiesSet();
    this.twitterSearchAdapter.start();
    receive = this.tweets.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(Tweet.class));
    assertEquals(((Tweet) receive.getPayload()).getId(), 2L);
    receive = this.tweets.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(Tweet.class));
    assertEquals(((Tweet) receive.getPayload()).getId(), 3L);
    assertNull(this.tweets.receive(0));
    persistedMetadataStoreValue = this.metadataStore.get(metadataKey);
    assertNotNull(persistedMetadataStoreValue);
    assertEquals("3", persistedMetadataStoreValue);
}
Also used : MetadataStore(org.springframework.integration.metadata.MetadataStore) RedisMetadataStore(org.springframework.integration.redis.metadata.RedisMetadataStore) Tweet(org.springframework.social.twitter.api.Tweet) RedisMetadataStore(org.springframework.integration.redis.metadata.RedisMetadataStore) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 54 with RedisAvailable

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

the class SubscribableRedisChannelTests method dispatcherHasNoSubscribersTest.

@Test
@RedisAvailable
public void dispatcherHasNoSubscribersTest() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    SubscribableRedisChannel channel = new SubscribableRedisChannel(connectionFactory, "si.test.channel.no.subs");
    channel.setBeanName("dhnsChannel");
    channel.setBeanFactory(mock(BeanFactory.class));
    channel.afterPropertiesSet();
    RedisMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container", RedisMessageListenerContainer.class);
    @SuppressWarnings("unchecked") Map<?, Set<MessageListenerAdapter>> channelMapping = (Map<?, Set<MessageListenerAdapter>>) TestUtils.getPropertyValue(container, "channelMapping");
    MessageListenerAdapter listener = channelMapping.entrySet().iterator().next().getValue().iterator().next();
    Object delegate = TestUtils.getPropertyValue(listener, "delegate");
    try {
        ReflectionUtils.findMethod(delegate.getClass(), "handleMessage", Object.class).invoke(delegate, "Hello, world!");
        fail("Exception expected");
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        assertNotNull(cause);
        assertThat(cause.getMessage(), containsString("Dispatcher has no subscribers for redis-channel 'si.test.channel.no.subs' (dhnsChannel)."));
    }
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) Set(java.util.Set) BeanFactory(org.springframework.beans.factory.BeanFactory) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) Map(java.util.Map) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 55 with RedisAvailable

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

the class SubscribableRedisChannelTests method pubSubChannelTest.

@Test
@RedisAvailable
public void pubSubChannelTest() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    SubscribableRedisChannel channel = new SubscribableRedisChannel(connectionFactory, "si.test.channel");
    channel.setBeanFactory(mock(BeanFactory.class));
    channel.afterPropertiesSet();
    channel.start();
    this.awaitContainerSubscribed(TestUtils.getPropertyValue(channel, "container", RedisMessageListenerContainer.class));
    final CountDownLatch latch = new CountDownLatch(3);
    MessageHandler handler = message -> latch.countDown();
    channel.subscribe(handler);
    channel.send(new GenericMessage<String>("1"));
    channel.send(new GenericMessage<String>("2"));
    channel.send(new GenericMessage<String>("3"));
    assertTrue(latch.await(20, TimeUnit.SECONDS));
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) TestUtils(org.springframework.integration.test.util.TestUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) Assert.assertThat(org.junit.Assert.assertThat) TimeUnit(java.util.concurrent.TimeUnit) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) CountDownLatch(java.util.concurrent.CountDownLatch) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) MessageHandler(org.springframework.messaging.MessageHandler) RedisAvailableTests(org.springframework.integration.redis.rules.RedisAvailableTests) ReflectionUtils(org.springframework.util.ReflectionUtils) Map(java.util.Map) BeanFactory(org.springframework.beans.factory.BeanFactory) Assert.fail(org.junit.Assert.fail) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) 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