Search in sources :

Example 31 with RLock

use of org.redisson.api.RLock in project redisson by redisson.

the class RedissonRedLockTest method testLockLeasetime.

private void testLockLeasetime(final long leaseTime, final TimeUnit unit) throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");
    RLock lock4 = client2.getLock("lock4");
    RLock lock5 = client2.getLock("lock5");
    RLock lock6 = client2.getLock("lock6");
    RLock lock7 = client2.getLock("lock7");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3, lock4, lock5, lock6, lock7);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    AtomicInteger counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        executor.submit(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    lock.lock(leaseTime, unit);
                    int nextValue = counter.get() + 1;
                    Thread.sleep(1000);
                    counter.set(nextValue);
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    assertThat(counter.get()).isEqualTo(50);
    client1.shutdown();
    client2.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) RLock(org.redisson.api.RLock)

Example 32 with RLock

use of org.redisson.api.RLock in project redisson by redisson.

the class RedissonSpinLockTest method testTryLockAsyncSucceed.

@Test
public void testTryLockAsyncSucceed() throws InterruptedException, ExecutionException {
    RLock lock = redisson.getSpinLock("lock");
    Boolean result = lock.tryLockAsync().get();
    assertThat(result).isTrue();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 33 with RLock

use of org.redisson.api.RLock in project redisson by redisson.

the class RedissonSpinLockTest method testReentrancy.

@Test
public void testReentrancy() throws InterruptedException {
    Lock lock = redisson.getSpinLock("lock1");
    Assertions.assertTrue(lock.tryLock());
    Assertions.assertTrue(lock.tryLock());
    lock.unlock();
    // next row  for test renew expiration tisk.
    // Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(RedissonLock.LOCK_EXPIRATION_INTERVAL_SECONDS*2));
    Thread thread1 = new Thread() {

        @Override
        public void run() {
            RLock lock1 = redisson.getSpinLock("lock1");
            Assertions.assertFalse(lock1.tryLock());
        }
    };
    thread1.start();
    thread1.join();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) Lock(java.util.concurrent.locks.Lock) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 34 with RLock

use of org.redisson.api.RLock in project redisson by redisson.

the class RedissonSpinLockTest method testForceUnlock.

@Test
public void testForceUnlock() {
    RLock lock = redisson.getSpinLock("lock");
    lock.lock();
    lock.forceUnlock();
    Assertions.assertFalse(lock.isLocked());
    lock = redisson.getSpinLock("lock");
    Assertions.assertFalse(lock.isLocked());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 35 with RLock

use of org.redisson.api.RLock in project redisson by redisson.

the class RedissonSpinLockTest method testGetHoldCount.

@Test
public void testGetHoldCount() {
    RLock lock = redisson.getSpinLock("lock");
    Assertions.assertEquals(0, lock.getHoldCount());
    lock.lock();
    Assertions.assertEquals(1, lock.getHoldCount());
    lock.unlock();
    Assertions.assertEquals(0, lock.getHoldCount());
    lock.lock();
    lock.lock();
    Assertions.assertEquals(2, lock.getHoldCount());
    lock.unlock();
    Assertions.assertEquals(1, lock.getHoldCount());
    lock.unlock();
    Assertions.assertEquals(0, lock.getHoldCount());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Aggregations

RLock (org.redisson.api.RLock)111 Test (org.junit.jupiter.api.Test)77 RedissonClient (org.redisson.api.RedissonClient)21 RReadWriteLock (org.redisson.api.RReadWriteLock)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 RedisProcess (org.redisson.RedisRunner.RedisProcess)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 CacheWriterException (javax.cache.integration.CacheWriterException)10 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 Config (org.redisson.config.Config)10 ExecutorService (java.util.concurrent.ExecutorService)8 Test (org.junit.Test)6 RedissonObject (org.redisson.RedissonObject)4 RSemaphore (org.redisson.api.RSemaphore)4 SecureRandom (java.security.SecureRandom)3 Date (java.util.Date)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Lock (java.util.concurrent.locks.Lock)3