Search in sources :

Example 41 with RLock

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

the class RedissonMultiLockTest method testMultiThreads.

@Test
public void testMultiThreads() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    Config config1 = new Config();
    config1.useSingleServer().setAddress(redis1.getRedisServerAddressAndPort());
    RedissonClient client = Redisson.create(config1);
    RLock lock1 = client.getLock("lock1");
    RLock lock2 = client.getLock("lock2");
    RLock lock3 = client.getLock("lock3");
    Thread t = new Thread() {

        public void run() {
            RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
            lock.lock();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            lock.unlock();
        }
    };
    t.start();
    t.join(1000);
    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    lock.lock();
    lock.unlock();
    client.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 42 with RLock

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

the class RedissonReadWriteLockTest method testIsLockedOtherThread.

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

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

        public void run() {
            RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
            RLock lock = rwlock.readLock();
            Assertions.assertFalse(lock.isLocked());
        }
    };
    t2.start();
    t2.join();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 43 with RLock

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

the class RedissonReadWriteLockTest method testName.

@Test
public void testName() throws InterruptedException, ExecutionException, TimeoutException {
    ExecutorService service = Executors.newFixedThreadPool(10);
    RReadWriteLock rwlock = redisson.getReadWriteLock("{test}:abc:key");
    RLock rlock = rwlock.readLock();
    List<Callable<Void>> callables = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        callables.add(() -> {
            for (int j = 0; j < 10; j++) {
                rlock.lock();
                try {
                } finally {
                    rlock.unlock();
                }
            }
            return null;
        });
    }
    List<Future<Void>> futures = service.invokeAll(callables);
    for (Future<Void> future : futures) {
        assertThatCode(future::get).doesNotThrowAnyException();
    }
    service.shutdown();
    assertThat(service.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Callable(java.util.concurrent.Callable) Test(org.junit.jupiter.api.Test)

Example 44 with RLock

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

the class RedissonReadWriteLockTest method testLockUnlock.

@Test
public void testLockUnlock() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    lock.lock();
    lock.unlock();
    lock.lock();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 45 with RLock

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

the class RedissonReadWriteLockTest method testConcurrencyLoop_MultiInstance.

@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
    final int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();
    final Random r = new SecureRandom();
    testMultiInstanceConcurrency(16, rc -> {
        for (int i = 0; i < iterations; i++) {
            boolean useWriteLock = r.nextBoolean();
            RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance1");
            RLock lock;
            if (useWriteLock) {
                lock = rwlock.writeLock();
            } else {
                lock = rwlock.readLock();
            }
            lock.lock();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lockedCounter.incrementAndGet();
            rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance1");
            if (useWriteLock) {
                lock = rwlock.writeLock();
            } else {
                lock = rwlock.readLock();
            }
            lock.unlock();
        }
    });
    Assertions.assertEquals(16 * iterations, lockedCounter.get());
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecureRandom(java.security.SecureRandom) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) 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