use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testWriteRead.
@Test
public void testWriteRead() throws InterruptedException {
RReadWriteLock readWriteLock = redisson.getReadWriteLock("TEST");
readWriteLock.writeLock().lock();
int threads = 20;
CountDownLatch ref = new CountDownLatch(threads);
for (int i = 0; i < threads; i++) {
Thread t1 = new Thread(() -> {
readWriteLock.readLock().lock();
try {
Thread.sleep(800);
} catch (InterruptedException e) {
}
readWriteLock.readLock().unlock();
ref.countDown();
});
t1.start();
t1.join(100);
}
readWriteLock.writeLock().unlock();
assertThat(ref.await(1, TimeUnit.SECONDS)).isTrue();
}
use of org.redisson.api.RReadWriteLock 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());
}
use of org.redisson.api.RReadWriteLock 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();
}
use of org.redisson.api.RReadWriteLock 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();
});
Assertions.assertEquals(iterations, lockedCounter.get());
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testExpireWrite.
@Test
public void testExpireWrite() throws InterruptedException {
RReadWriteLock lock = redisson.getReadWriteLock("lock");
lock.writeLock().lock(2, TimeUnit.SECONDS);
final long startTime = System.currentTimeMillis();
Thread t = new Thread() {
public void run() {
RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
lock1.writeLock().lock();
long spendTime = System.currentTimeMillis() - startTime;
Assertions.assertTrue(spendTime < 2050);
lock1.writeLock().unlock();
}
};
t.start();
t.join();
lock.writeLock().unlock();
}
Aggregations