Search in sources :

Example 46 with RedisAvailable

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());
}
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 47 with RedisAvailable

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

Example 48 with RedisAvailable

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

Example 49 with RedisAvailable

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

Example 50 with RedisAvailable

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());
}
Also used : 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