use of org.redisson.api.RedissonClient in project redisson by redisson.
the class ConcurrentRedissonSortedSetTest method testAddRemove_SingleInstance.
@Test
public void testAddRemove_SingleInstance() throws InterruptedException, NoSuchAlgorithmException {
final String name = "testAddNegative_SingleInstance";
RedissonClient r = BaseTest.createInstance();
RSortedSet<Integer> map = r.getSortedSet(name);
map.clear();
int length = 1000;
for (int i = 0; i < length; i++) {
map.add(i);
}
final AtomicInteger counter = new AtomicInteger(length);
final Random rnd = SecureRandom.getInstanceStrong();
testSingleInstanceConcurrency(length, rc -> {
RSortedSet<Integer> set = rc.getSortedSet(name);
int c = counter.incrementAndGet();
Assert.assertTrue(set.add(c));
set.remove(rnd.nextInt(length));
});
Assert.assertEquals(counter.get(), length * 2);
Integer prevVal = null;
for (Integer val : map) {
if (prevVal == null) {
prevVal = val;
continue;
}
if (val < prevVal) {
Assert.fail();
}
}
r.shutdown();
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class ConcurrentRedissonSortedSetTest method testAdd_SingleInstance.
@Test
public void testAdd_SingleInstance() throws InterruptedException {
final String name = "testAdd_SingleInstance";
RedissonClient r = BaseTest.createInstance();
RSortedSet<Integer> map = r.getSortedSet(name);
map.clear();
int length = 5000;
final List<Integer> elements = new ArrayList<Integer>();
for (int i = 1; i < length + 1; i++) {
elements.add(i);
}
Collections.shuffle(elements);
final AtomicInteger counter = new AtomicInteger(-1);
testSingleInstanceConcurrency(length, rc -> {
RSortedSet<Integer> set = rc.getSortedSet(name);
int c = counter.incrementAndGet();
Integer element = elements.get(c);
Assert.assertTrue(set.add(element));
});
Collections.sort(elements);
Integer[] p = elements.toArray(new Integer[elements.size()]);
assertThat(map).containsExactly(p);
map.clear();
r.shutdown();
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class BaseConcurrentTest method testMultiInstanceConcurrencySequentiallyLaunched.
protected void testMultiInstanceConcurrencySequentiallyLaunched(int iterations, final RedissonRunnable runnable) throws InterruptedException {
System.out.println("Multi Instance Concurrent Job Interation: " + iterations);
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
final Map<Integer, RedissonClient> instances = new HashMap<Integer, RedissonClient>();
for (int i = 0; i < iterations; i++) {
instances.put(i, BaseTest.createInstance());
}
long watch = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
final int n = i;
executor.execute(() -> runnable.run(instances.get(n)));
}
executor.shutdown();
Assert.assertTrue(executor.awaitTermination(5, TimeUnit.MINUTES));
System.out.println("multi: " + (System.currentTimeMillis() - watch));
executor = Executors.newCachedThreadPool();
for (final RedissonClient redisson : instances.values()) {
executor.execute(() -> redisson.shutdown());
}
executor.shutdown();
Assert.assertTrue(executor.awaitTermination(5, TimeUnit.MINUTES));
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonBlockingFairQueueTest method testTimeout.
@Test
public void testTimeout() throws InterruptedException {
int size = 1000;
CountDownLatch latch = new CountDownLatch(size);
AtomicInteger t1Counter = new AtomicInteger();
AtomicInteger t2Counter = new AtomicInteger();
AtomicInteger t3Counter = 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(5, TimeUnit.SECONDS);
if (latch.getCount() == 0) {
break;
}
if (a == null) {
continue;
}
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() {
try {
String a = queue2.poll(2, TimeUnit.SECONDS);
if (a != null) {
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(5, TimeUnit.SECONDS);
if (latch.getCount() == 0) {
break;
}
if (a == null) {
continue;
}
latch.countDown();
t3Counter.incrementAndGet();
} catch (InterruptedException e) {
}
}
}
;
};
t1.start();
t1.join(500);
t2.start();
t2.join(500);
t3.start();
t3.join(500);
RBlockingQueue<String> queue = redisson.getBlockingFairQueue("test");
assertThat(redisson.getList("{" + queue.getName() + "}:list").size()).isEqualTo(3);
for (int i = 0; i < size; i++) {
queue.add("" + i);
}
t1.join();
t2.join();
t3.join();
assertThat(latch.await(50, TimeUnit.SECONDS)).isTrue();
assertThat(t1Counter.get()).isBetween(499, 500);
assertThat(t2Counter.get()).isEqualTo(1);
assertThat(t3Counter.get()).isBetween(499, 500);
assertThat(redisson.getList("{" + queue.getName() + "}:list").size()).isEqualTo(2);
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonBlockingQueueTest 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 = redisson.getBlockingQueue("queue:pollany");
RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
f.await(1, TimeUnit.SECONDS);
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);
}
assertThat(queue1.size()).isEqualTo(10);
Integer result = f.get(1, TimeUnit.SECONDS);
assertThat(result).isEqualTo(123);
redisson.shutdown();
runner.stop();
}
Aggregations