Search in sources :

Example 46 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.Test)

Example 47 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).until(() -> assertThat(executed.get()).isTrue());
    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 48 with RLock

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

the class RedissonRedLockTest method testLockLeasetime.

@Test
public void testLockLeasetime() 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(2, TimeUnit.SECONDS);
                    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) Test(org.junit.Test)

Example 49 with RLock

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

the class RedissonReadWriteLockTest method testForceUnlock.

@Test
public void testForceUnlock() {
    RReadWriteLock lock = redisson.getReadWriteLock("lock");
    RLock readLock = lock.readLock();
    readLock.lock();
    assertThat(readLock.isLocked()).isTrue();
    lock.writeLock().forceUnlock();
    assertThat(readLock.isLocked()).isTrue();
    lock.readLock().forceUnlock();
    assertThat(readLock.isLocked()).isFalse();
    RLock writeLock = lock.writeLock();
    writeLock.lock();
    assertThat(writeLock.isLocked()).isTrue();
    lock.readLock().forceUnlock();
    assertThat(writeLock.isLocked()).isTrue();
    lock.writeLock().forceUnlock();
    assertThat(writeLock.isLocked()).isFalse();
    lock = redisson.getReadWriteLock("lock");
    assertThat(lock.readLock().isLocked()).isFalse();
    assertThat(lock.writeLock().isLocked()).isFalse();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Example 50 with RLock

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

the class RedissonReadWriteLockTest method testConcurrency_MultiInstance.

@Test
public void testConcurrency_MultiInstance() throws InterruptedException {
    int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();
    final Random r = new SecureRandom();
    testMultiInstanceConcurrency(iterations, rc -> {
        RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance2");
        RLock lock;
        if (r.nextBoolean()) {
            lock = rwlock.writeLock();
        } else {
            lock = rwlock.readLock();
        }
        lock.lock();
        lockedCounter.incrementAndGet();
        lock.unlock();
    });
    Assert.assertEquals(iterations, lockedCounter.get());
}
Also used : SecureRandom(java.security.SecureRandom) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecureRandom(java.security.SecureRandom) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Aggregations

RLock (org.redisson.api.RLock)69 Test (org.junit.Test)51 RReadWriteLock (org.redisson.api.RReadWriteLock)13 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 CacheWriterException (javax.cache.integration.CacheWriterException)10 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 RedissonClient (org.redisson.api.RedissonClient)10 RedisProcess (org.redisson.RedisRunner.RedisProcess)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RedissonObject (org.redisson.RedissonObject)4 SecureRandom (java.security.SecureRandom)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 Lock (java.util.concurrent.locks.Lock)2 RSemaphore (org.redisson.api.RSemaphore)2