Search in sources :

Example 1 with GrowableArrayBlockingQueue

use of org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue in project incubator-pulsar by apache.

the class GrowableArrayBlockingQueueTest method blockingTake.

@Test(timeOut = 10000)
public void blockingTake() throws Exception {
    BlockingQueue<Integer> queue = new GrowableArrayBlockingQueue<>();
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(() -> {
        try {
            int expected = 0;
            for (int i = 0; i < 100; i++) {
                int n = queue.take();
                assertEquals(n, expected++);
            }
            latch.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }).start();
    int n = 0;
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            queue.put(n);
            ++n;
        }
        // Wait until all the entries are consumed
        while (!queue.isEmpty()) {
            Thread.sleep(1);
        }
    }
    latch.await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GrowableArrayBlockingQueue(org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) NoSuchElementException(java.util.NoSuchElementException) Test(org.testng.annotations.Test)

Example 2 with GrowableArrayBlockingQueue

use of org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue in project incubator-pulsar by apache.

the class GrowableArrayBlockingQueueTest method simple.

@Test
public void simple() throws Exception {
    BlockingQueue<Integer> queue = new GrowableArrayBlockingQueue<>(4);
    assertEquals(queue.poll(), null);
    assertEquals(queue.remainingCapacity(), Integer.MAX_VALUE);
    assertEquals(queue.toString(), "[]");
    try {
        queue.element();
        fail("Should have thrown exception");
    } catch (NoSuchElementException e) {
    // Expected
    }
    try {
        queue.iterator();
        fail("Should have thrown exception");
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    // Test index rollover
    for (int i = 0; i < 100; i++) {
        queue.add(i);
        assertEquals(queue.take().intValue(), i);
    }
    queue.offer(1);
    assertEquals(queue.toString(), "[1]");
    queue.offer(2);
    assertEquals(queue.toString(), "[1, 2]");
    queue.offer(3);
    assertEquals(queue.toString(), "[1, 2, 3]");
    queue.offer(4);
    assertEquals(queue.toString(), "[1, 2, 3, 4]");
    AtomicInteger value = new AtomicInteger(1);
    queue.forEach(v -> {
        assertEquals(v.intValue(), value.get());
        value.incrementAndGet();
    });
    assertEquals(queue.size(), 4);
    List<Integer> list = new ArrayList<>();
    queue.drainTo(list, 3);
    assertEquals(queue.size(), 1);
    assertEquals(list, Lists.newArrayList(1, 2, 3));
    assertEquals(queue.toString(), "[4]");
    assertEquals(queue.peek().intValue(), 4);
    assertEquals(queue.element().intValue(), 4);
    assertEquals(queue.remove().intValue(), 4);
    try {
        queue.remove();
        fail("Should have thrown exception");
    } catch (NoSuchElementException e) {
    // Expected
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GrowableArrayBlockingQueue(org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException) Test(org.testng.annotations.Test)

Example 3 with GrowableArrayBlockingQueue

use of org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue in project incubator-pulsar by apache.

the class GrowableArrayBlockingQueueTest method pollTimeout2.

@Test(timeOut = 10000)
public void pollTimeout2() throws Exception {
    BlockingQueue<Integer> queue = new GrowableArrayBlockingQueue<>();
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(() -> {
        try {
            queue.poll(1, TimeUnit.HOURS);
            latch.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }).start();
    // Make sure background thread is waiting on poll
    Thread.sleep(100);
    queue.put(1);
    latch.await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GrowableArrayBlockingQueue(org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) NoSuchElementException(java.util.NoSuchElementException) Test(org.testng.annotations.Test)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 GrowableArrayBlockingQueue (org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue)3 Test (org.testng.annotations.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArrayList (java.util.ArrayList)1