Search in sources :

Example 91 with RLock

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

the class RedissonLockHeavyTest method lockUnlockRLock.

@ParameterizedTest
@MethodSource("mapClasses")
public void lockUnlockRLock(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);
                    lock.lock();
                    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();
                    }
                }
            }
        };
        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 92 with RLock

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

the class RedissonRedLockTest method testTryLockLeasetime.

@Test
public void testTryLockLeasetime() 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");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
    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 {
                    if (lock.tryLock(4, 2, TimeUnit.SECONDS)) {
                        int nextValue = counter.get() + 1;
                        Thread.sleep(1000);
                        counter.set(nextValue);
                        lock.unlock();
                    } else {
                        j--;
                    }
                } 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) Test(org.junit.jupiter.api.Test)

Example 93 with RLock

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

the class RedissonRedLockTest method test.

// @Test
public void test() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedisProcess redis3 = redisTestMultilockInstance();
    NioEventLoopGroup group = new NioEventLoopGroup();
    RedissonClient client1 = createClient(group, redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(group, redis2.getRedisServerAddressAndPort());
    RedissonClient client3 = createClient(group, redis3.getRedisServerAddressAndPort());
    final RLock lock1 = client1.getLock("lock1");
    final RLock lock2 = client2.getLock("lock2");
    final RLock lock3 = client3.getLock("lock3");
    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    lock.lock();
    final AtomicBoolean executed = new AtomicBoolean();
    Thread t = new Thread() {

        @Override
        public void run() {
            RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
            assertThat(lock.tryLock()).isFalse();
            assertThat(lock.tryLock()).isFalse();
            executed.set(true);
        }
    };
    t.start();
    t.join();
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    lock.unlock();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
    assertThat(redis3.stop()).isEqualTo(0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) RLock(org.redisson.api.RLock) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 94 with RLock

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

the class RedissonReadWriteLockTest method testWR.

@Test
public void testWR() throws InterruptedException {
    RReadWriteLock rw = redisson.getReadWriteLock("my_read_write_lock");
    RLock writeLock = rw.writeLock();
    writeLock.lock();
    rw.readLock().lock();
    assertThat(writeLock.isLocked()).isTrue();
    rw.readLock().unlock();
    assertThat(writeLock.isLocked()).isTrue();
    writeLock.unlock();
    assertThat(writeLock.isLocked()).isFalse();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 95 with RLock

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

the class RedissonReadWriteLockTest method testReadLockLeaseTimeoutDiffThreadsRRW.

@Test
public void testReadLockLeaseTimeoutDiffThreadsRRW() throws InterruptedException {
    new Thread(() -> {
        RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
        try {
            Assertions.assertTrue(readLock.tryLock(1, 10, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
    Thread.sleep(5000);
    new Thread(() -> {
        RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
        try {
            Assertions.assertTrue(readLock2.tryLock(1, 10, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        readLock2.unlock();
    }).start();
    final AtomicBoolean executed = new AtomicBoolean();
    new Thread(() -> {
        RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
        try {
            boolean locked = writeLock.tryLock(10, 10, TimeUnit.SECONDS);
            executed.set(locked);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
    await().atMost(6, TimeUnit.SECONDS).untilTrue(executed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Aggregations

RLock (org.redisson.api.RLock)113 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