Search in sources :

Example 16 with RLock

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

the class RedissonFairLockTest method testTryLockNonDelayed.

@Test
public void testTryLockNonDelayed() throws InterruptedException {
    String LOCK_NAME = "SOME_LOCK";
    Thread t1 = new Thread(() -> {
        RLock fairLock = redisson.getFairLock(LOCK_NAME);
        try {
            if (fairLock.tryLock(0, TimeUnit.SECONDS)) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                Assertions.fail("Unable to acquire lock for some reason");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            fairLock.unlock();
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            Thread.sleep(200L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        RLock fairLock = redisson.getFairLock(LOCK_NAME);
        try {
            if (fairLock.tryLock(200, TimeUnit.MILLISECONDS)) {
                Assertions.fail("Should not be inside second block");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            fairLock.unlock();
        }
    });
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    RLock fairLock = redisson.getFairLock(LOCK_NAME);
    try {
        if (!fairLock.tryLock(0, TimeUnit.SECONDS)) {
            Assertions.fail("Could not get unlocked lock " + LOCK_NAME);
        }
    } finally {
        fairLock.unlock();
    }
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 17 with RLock

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

the class RedissonFairLockTest method testTryLockWait.

@Test
public void testTryLockWait() throws InterruptedException {
    testSingleInstanceConcurrency(1, r -> {
        RLock lock = r.getFairLock("lock");
        lock.lock();
    });
    RLock lock = redisson.getFairLock("lock");
    long startTime = System.currentTimeMillis();
    lock.tryLock(3, TimeUnit.SECONDS);
    assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3100L);
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 18 with RLock

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

the class RedissonFairLockTest method testIsHeldByCurrentThread.

@Test
public void testIsHeldByCurrentThread() {
    RLock lock = redisson.getFairLock("lock");
    Assertions.assertFalse(lock.isHeldByCurrentThread());
    lock.lock();
    Assertions.assertTrue(lock.isHeldByCurrentThread());
    lock.unlock();
    Assertions.assertFalse(lock.isHeldByCurrentThread());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 19 with RLock

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

the class RedissonFairLockTest method testReentrancy.

@Test
public void testReentrancy() throws InterruptedException {
    Lock lock = redisson.getFairLock("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.getFairLock("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 20 with RLock

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

the class RedissonLockTest method testIsLockedOtherThread.

@Test
public void testIsLockedOtherThread() throws InterruptedException {
    RLock lock = redisson.getLock("lock");
    lock.lock();
    Thread t = new Thread() {

        public void run() {
            RLock lock = redisson.getLock("lock");
            Assertions.assertTrue(lock.isLocked());
        }
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

        public void run() {
            RLock lock = redisson.getLock("lock");
            Assertions.assertFalse(lock.isLocked());
        }
    };
    t2.start();
    t2.join();
}
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