use of org.redisson.RedisRunner.RedisProcess 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);
}
use of org.redisson.RedisRunner.RedisProcess 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);
}
use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.
the class RedissonMultiLockTest method testWaitAndLeaseTimeouts.
@Test
public void testWaitAndLeaseTimeouts() 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");
ExecutorService executor = Executors.newFixedThreadPool(10);
AtomicInteger counter = new AtomicInteger();
for (int i = 0; i < 10; i++) {
executor.submit(() -> {
RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
try {
boolean res = lock.tryLock(-1, 10, TimeUnit.SECONDS);
if (res) {
counter.incrementAndGet();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
assertThat(executor.awaitTermination(1, TimeUnit.SECONDS)).isTrue();
assertThat(counter.get()).isEqualTo(1);
}
use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.
the class RedissonMultiLockTest 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();
client1.shutdown();
client2.shutdown();
client3.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
assertThat(redis2.stop()).isEqualTo(0);
assertThat(redis3.stop()).isEqualTo(0);
}
use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.
the class RedissonPriorityBlockingQueueTest method testPollAsyncReattach.
@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
RedisProcess runner = new RedisRunner().nosave().randomDir().randomPort().run();
Config config = new Config();
config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
RBlockingQueue<Integer> queue1 = getQueue(redisson);
RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
try {
f.toCompletableFuture().get(1, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException e) {
// skip
}
runner.stop();
runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
queue1.put(123);
// check connection rotation
for (int i = 0; i < 10; i++) {
queue1.put(i + 1000);
}
Integer result = f.get();
assertThat(queue1.size()).isEqualTo(10);
assertThat(result).isEqualTo(123);
redisson.shutdown();
runner.stop();
}
Aggregations