Search in sources :

Example 11 with RLock

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

the class RedissonLockHeavyTest method tryLockUnlockRLock.

@ParameterizedTest
@MethodSource("mapClasses")
public void tryLockUnlockRLock(int threads, int loops) throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    for (int i = 0; i < threads; i++) {
        Runnable worker = new Runnable() {

            @Override
            public void run() {
                for (int j = 0; j < loops; j++) {
                    RLock lock = redisson.getLock("RLOCK_" + j);
                    try {
                        if (lock.tryLock(ThreadLocalRandom.current().nextInt(10), TimeUnit.MILLISECONDS)) {
                            try {
                                RBucket<String> bucket = redisson.getBucket("RBUCKET_" + j);
                                bucket.set("TEST", 30, TimeUnit.SECONDS);
                                RSemaphore semaphore = redisson.getSemaphore("SEMAPHORE_" + j);
                                semaphore.release();
                                try {
                                    semaphore.acquire();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } finally {
                                lock.unlock();
                            }
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        executor.execute(worker);
    }
    executor.shutdown();
    executor.awaitTermination(threads * loops, TimeUnit.SECONDS);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) ExecutorService(java.util.concurrent.ExecutorService) RLock(org.redisson.api.RLock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with RLock

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

the class RedissonLockTest method testForceUnlock.

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

Example 13 with RLock

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

the class RedissonFairLockTest method testExpire.

@Test
public void testExpire() throws InterruptedException {
    RLock lock = redisson.getFairLock("lock");
    lock.lock(2, TimeUnit.SECONDS);
    final long startTime = System.currentTimeMillis();
    Thread t = new Thread() {

        public void run() {
            RLock lock1 = redisson.getFairLock("lock");
            lock1.lock();
            long spendTime = System.currentTimeMillis() - startTime;
            System.out.println(spendTime);
            Assertions.assertTrue(spendTime < 2020);
            lock1.unlock();
        }
    };
    t.start();
    t.join();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 14 with RLock

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

the class RedissonFairLockTest method testUnlockFail.

@Test
public void testUnlockFail() {
    Assertions.assertThrows(IllegalMonitorStateException.class, () -> {
        RLock lock = redisson.getFairLock("lock");
        Thread t = new Thread() {

            public void run() {
                RLock lock = redisson.getFairLock("lock");
                lock.lock();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                lock.unlock();
            }
        };
        t.start();
        t.join(400);
        try {
            lock.unlock();
        } catch (IllegalMonitorStateException e) {
            t.join();
            throw e;
        }
    });
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 15 with RLock

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

the class RedissonFairLockTest method testForceUnlock.

@Test
public void testForceUnlock() {
    RLock lock = redisson.getFairLock("lock");
    lock.lock();
    lock.forceUnlock();
    Assertions.assertFalse(lock.isLocked());
    lock = redisson.getFairLock("lock");
    Assertions.assertFalse(lock.isLocked());
}
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