use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonCountDownLatchConcurrentTest method testSingleCountDownAwait_SingleInstance.
@Test
public void testSingleCountDownAwait_SingleInstance() throws InterruptedException {
final int iterations = Runtime.getRuntime().availableProcessors() * 3;
RedissonClient redisson = BaseTest.createInstance();
final RCountDownLatch latch = redisson.getCountDownLatch("latch");
latch.trySetCount(iterations);
final AtomicInteger counter = new AtomicInteger();
ExecutorService executor = Executors.newScheduledThreadPool(iterations);
for (int i = 0; i < iterations; i++) {
executor.execute(() -> {
try {
latch.await();
Assert.assertEquals(0, latch.getCount());
Assert.assertEquals(iterations, counter.get());
} catch (InterruptedException e) {
Assert.fail();
}
});
}
ExecutorService countDownExecutor = Executors.newFixedThreadPool(iterations);
for (int i = 0; i < iterations; i++) {
countDownExecutor.execute(() -> {
latch.countDown();
counter.incrementAndGet();
});
}
executor.shutdown();
Assert.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
redisson.shutdown();
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonBlockingFairQueueTest method testFairness.
@Test
public void testFairness() throws InterruptedException {
int size = 1000;
CountDownLatch latch = new CountDownLatch(size);
AtomicInteger t1Counter = new AtomicInteger();
AtomicInteger t2Counter = new AtomicInteger();
AtomicInteger t3Counter = new AtomicInteger();
AtomicInteger t4Counter = new AtomicInteger();
RedissonClient redisson1 = createInstance();
RBlockingFairQueue<String> queue1 = redisson1.getBlockingFairQueue("test");
Thread t1 = new Thread("test-thread1") {
public void run() {
while (true) {
try {
String a = queue1.poll(1, TimeUnit.SECONDS);
if (a == null) {
break;
}
latch.countDown();
t1Counter.incrementAndGet();
} catch (InterruptedException e) {
}
}
}
;
};
RedissonClient redisson2 = createInstance();
RBlockingFairQueue<String> queue2 = redisson2.getBlockingFairQueue("test");
Thread t2 = new Thread("test-thread2") {
public void run() {
while (true) {
try {
String a = queue2.poll(1, TimeUnit.SECONDS);
if (a == null) {
break;
}
Thread.sleep(50);
latch.countDown();
t2Counter.incrementAndGet();
} catch (InterruptedException e) {
}
}
}
;
};
RedissonClient redisson3 = createInstance();
RBlockingFairQueue<String> queue3 = redisson3.getBlockingFairQueue("test");
Thread t3 = new Thread("test-thread3") {
public void run() {
while (true) {
try {
String a = queue3.poll(1, TimeUnit.SECONDS);
if (a == null) {
break;
}
Thread.sleep(10);
latch.countDown();
t3Counter.incrementAndGet();
} catch (InterruptedException e) {
}
}
}
;
};
RedissonClient redisson4 = createInstance();
RBlockingFairQueue<String> queue4 = redisson4.getBlockingFairQueue("test");
Thread t4 = new Thread("test-thread4") {
public void run() {
while (true) {
try {
String a = queue4.poll(1, TimeUnit.SECONDS);
if (a == null) {
break;
}
latch.countDown();
t4Counter.incrementAndGet();
} catch (InterruptedException e) {
}
}
}
;
};
RBlockingQueue<String> queue = redisson.getBlockingFairQueue("test");
for (int i = 0; i < size; i++) {
queue.add("" + i);
}
t1.start();
t2.start();
t3.start();
t4.start();
t1.join();
t2.join();
t3.join();
t4.join();
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
queue1.destroy();
queue2.destroy();
queue3.destroy();
queue4.destroy();
redisson1.shutdown();
redisson2.shutdown();
redisson3.shutdown();
redisson4.shutdown();
assertThat(t1Counter.get()).isEqualTo(250);
assertThat(t2Counter.get()).isEqualTo(250);
assertThat(t3Counter.get()).isEqualTo(250);
assertThat(t4Counter.get()).isEqualTo(250);
assertThat(redisson.getKeys().count()).isEqualTo(1);
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonBlockingQueueTest method testTakeAsyncCancel.
@Test
public void testTakeAsyncCancel() {
Config config = createConfig();
config.useSingleServer().setConnectionMinimumIdleSize(1).setConnectionPoolSize(1);
RedissonClient redisson = Redisson.create(config);
RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("testTakeAsyncCancel");
for (int i = 0; i < 10; i++) {
RFuture<Integer> f = queue1.takeAsync();
f.cancel(true);
}
assertThat(queue1.add(1)).isTrue();
assertThat(queue1.add(2)).isTrue();
assertThat(queue1.size()).isEqualTo(2);
redisson.shutdown();
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class WeightedRoundRobinBalancerTest method testUseMasterForReadsIfNoConnectionsToSlaves.
@Test
public void testUseMasterForReadsIfNoConnectionsToSlaves() throws IOException, InterruptedException {
RedisProcess master = null;
RedisProcess slave = null;
RedissonClient client = null;
try {
master = redisTestInstance();
slave = redisTestInstance();
Map<String, Integer> weights = new HashMap<>();
weights.put(master.getRedisServerAddressAndPort(), 1);
weights.put(slave.getRedisServerAddressAndPort(), 2);
Config config = new Config();
config.useMasterSlaveServers().setReadMode(ReadMode.SLAVE).setMasterAddress(master.getRedisServerAddressAndPort()).addSlaveAddress(slave.getRedisServerAddressAndPort()).setLoadBalancer(new WeightedRoundRobinBalancer(weights, 1));
client = Redisson.create(config);
// To simulate network connection issues to slave, stop the slave
// after creating the client. Cannot create the client without the
// slave running. See https://github.com/mrniko/redisson/issues/580
slave.stop();
RedissonClient clientCopy = client;
assertThat(clientCopy.getBucket("key").get()).isNull();
} finally {
if (master != null) {
master.stop();
}
if (slave != null) {
slave.stop();
}
if (client != null) {
client.shutdown();
}
}
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonTopicTest method testHeavyLoad.
@Test
public void testHeavyLoad() throws InterruptedException {
final CountDownLatch messageRecieved = new CountDownLatch(1000);
RedissonClient redisson1 = BaseTest.createInstance();
RTopic<Message> topic1 = redisson1.getTopic("topic");
topic1.addListener((channel, msg) -> {
Assert.assertEquals(new Message("123"), msg);
messageRecieved.countDown();
counter++;
});
RedissonClient redisson2 = BaseTest.createInstance();
RTopic<Message> topic2 = redisson2.getTopic("topic");
topic2.addListener((channel, msg) -> {
Assert.assertEquals(new Message("123"), msg);
messageRecieved.countDown();
});
for (int i = 0; i < 5000; i++) {
topic2.publish(new Message("123"));
}
messageRecieved.await();
Thread.sleep(1000);
Assert.assertEquals(5000, counter);
redisson1.shutdown();
redisson2.shutdown();
}
Aggregations