Example usage for org.springframework.integration.redis.util RedisLockRegistry obtain

List of usage examples for org.springframework.integration.redis.util RedisLockRegistry obtain

Introduction

In this page you can find the example usage for org.springframework.integration.redis.util RedisLockRegistry obtain.

Prototype

@Override
    public Lock obtain(Object lockKey) 

Source Link

Usage

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable/*  www.jav  a2  s  .co  m*/
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 {
            assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
        } finally {
            lock.unlock();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//from   www.jav a2 s . c o  m
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 {
            assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
        } finally {
            lock.unlock();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable/*  w  ww.  j ava 2s  . c o m*/
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();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//from  w w  w .  j  av a 2s.  c o  m
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();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable/*from  w w w  . ja v a  2  s  .c  o m*/
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();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//from   www.ja v a 2  s.  c  om
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("Lock is not locked"));
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//ww  w  . j a v  a 2  s .  c o m
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();
    assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
    Executors.newSingleThreadExecutor().execute(() -> {
        Lock lock2 = registry.obtain("foo");
        try {
            latch1.countDown();
            lock2.lockInterruptibly();
            assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
            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());
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//  ww  w.  ja v a2  s  .co  m
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();
    assertNotNull(TestUtils.getPropertyValue(registry1, "hardThreadLocks", ThreadLocal.class).get());
    Executors.newSingleThreadExecutor().execute(() -> {
        Lock lock2 = registry2.obtain("foo");
        try {
            latch1.countDown();
            lock2.lockInterruptibly();
            assertNotNull(TestUtils.getPropertyValue(registry2, "hardThreadLocks", ThreadLocal.class).get());
            latch2.await(10, TimeUnit.SECONDS);
            locked.set(true);
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
            logger.error("Interrupted while locking: " + lock2, e1);
        } finally {
            try {
                lock2.unlock();
                latch3.countDown();
            } catch (IllegalStateException e2) {
                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());
    assertNull(TestUtils.getPropertyValue(registry1, "hardThreadLocks", ThreadLocal.class).get());
    assertNull(TestUtils.getPropertyValue(registry2, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//  w w  w .j  a v a2  s  .  com
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("Lock is owned by"));
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable/*w  ww.j av a2  s.  co  m*/
public void testList() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
    Lock foo = registry.obtain("foo");
    foo.lockInterruptibly();
    Lock bar = registry.obtain("bar");
    bar.lockInterruptibly();
    Lock baz = registry.obtain("baz");
    baz.lockInterruptibly();
    Collection<Lock> locks = registry.listLocks();
    assertEquals(3, locks.size());
    foo.unlock();
    bar.unlock();
    baz.unlock();
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}