Search in sources :

Example 1 with RBoundedBlockingQueue

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

the class RedissonBoundedBlockingQueueTest method testOfferTimeout.

@Test
public void testOfferTimeout() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("bounded-queue");
    queue.trySetCapacity(5);
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    queue.add(5);
    long start = System.currentTimeMillis();
    assertThat(queue.offer(6, 2, TimeUnit.SECONDS)).isFalse();
    assertThat(System.currentTimeMillis() - start).isGreaterThan(1900);
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    final AtomicBoolean executed = new AtomicBoolean();
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue");
            assertThat(queue1.remove()).isEqualTo(1);
            executed.set(true);
        }
    }, 1, TimeUnit.SECONDS);
    start = System.currentTimeMillis();
    assertThat(queue.offer(6, 3, TimeUnit.SECONDS)).isTrue();
    assertThat(System.currentTimeMillis() - start).isBetween(1000L, 2000L);
    await().atMost(2, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    assertThat(queue).containsExactly(2, 3, 4, 5, 6);
    executor.shutdown();
    assertThat(executor.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RBoundedBlockingQueue(org.redisson.api.RBoundedBlockingQueue) Test(org.junit.Test)

Example 2 with RBoundedBlockingQueue

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

the class RedissonBoundedBlockingQueueTest method testPut.

@Test
public void testPut() throws InterruptedException {
    final RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue");
    queue1.trySetCapacity(3);
    queue1.add(1);
    queue1.add(2);
    queue1.add(3);
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    final AtomicBoolean executed = new AtomicBoolean();
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue");
            assertThat(queue1.poll()).isEqualTo(1);
            executed.set(true);
        }
    }, 1, TimeUnit.SECONDS);
    queue1.put(4);
    await().atMost(5, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    assertThat(queue1).containsExactly(2, 3, 4);
    executor.shutdown();
    assertThat(executor.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RBoundedBlockingQueue(org.redisson.api.RBoundedBlockingQueue) Test(org.junit.Test)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 RBoundedBlockingQueue (org.redisson.api.RBoundedBlockingQueue)2