Esempio n. 1
0
  @Test
  public void testOfferPoll() throws IOException, InterruptedException {

    final IQueue q = client.getQueue(queueForTestOfferPoll);

    for (int i = 0; i < 10; i++) {
      boolean result = q.offer("item");
      if (i < maxSizeForQueue) {
        assertTrue(result);
      } else {
        assertFalse(result);
      }
    }
    assertEquals(maxSizeForQueue, q.size());

    final Thread t1 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.poll();
          }
        };
    t1.start();

    boolean result = q.offer("item", 5, TimeUnit.SECONDS);
    assertTrue(result);

    for (int i = 0; i < 10; i++) {
      Object o = q.poll();
      if (i < maxSizeForQueue) {
        assertNotNull(o);
      } else {
        assertNull(o);
      }
    }
    assertEquals(0, q.size());

    final Thread t2 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.offer("item1");
          }
        };
    t2.start();

    Object o = q.poll(5, TimeUnit.SECONDS);
    assertEquals("item1", o);
    t1.join(10000);
    t2.join(10000);
  }
Esempio n. 2
0
  @Test
  public void testQueueWithSizeLimit() {
    final IQueue q = client.getQueue(queueForTestQueueWithSizeLimit);

    for (int i = 0; i < maxSizeForQueue; i++) {
      q.offer(i);
    }
    assertFalse(q.offer(maxSizeForQueue));
  }
Esempio n. 3
0
  @Test
  public void testContains() {
    final IQueue q = client.getQueue(randomString());

    q.offer(1);
    assertTrue(q.contains(1));
    assertFalse(q.contains(2));
  }
Esempio n. 4
0
  @Test
  public void testRemove() throws IOException {
    final IQueue q = client.getQueue(randomString());

    q.offer(1);
    assertTrue(q.remove(1));
    assertFalse(q.remove(2));
  }
Esempio n. 5
0
  @Test
  public void testRemainingCapacity() throws IOException {
    final IQueue q = client.getQueue(randomString());

    assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
    q.offer("one");
    assertEquals(Integer.MAX_VALUE - 1, q.remainingCapacity());
  }
Esempio n. 6
0
 @Test
 public void testPeak() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.peek());
   assertEquals(1, q.peek());
   assertEquals(1, q.size());
 }
Esempio n. 7
0
  @Test
  public void testToArray() {
    final int maxItems = 19;
    final IQueue q = client.getQueue(randomString());

    Object[] offered = new Object[maxItems];
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      offered[i] = i;
    }

    Object[] result = q.toArray();
    assertEquals(offered, result);
  }
Esempio n. 8
0
  @Test
  public void testIterator() {
    final int maxItems = 18;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
    }

    int i = 0;
    for (Object o : q) {
      assertEquals(i++, o);
    }
  }
Esempio n. 9
0
  @Test
  public void testContainsAll() {
    final int maxItems = 11;
    final IQueue q = client.getQueue(randomString());

    List trueList = new ArrayList();
    List falseList = new ArrayList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      trueList.add(i);
      falseList.add(i + 1);
    }
    assertTrue(q.containsAll(trueList));
    assertFalse(q.containsAll(falseList));
  }
Esempio n. 10
0
  @Test
  public void testDrain() {
    final int maxItems = 12;
    final IQueue q = client.getQueue(randomString());

    List offeredList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      offeredList.add(i);
    }
    List drainedList = new LinkedList();
    int totalDrained = q.drainTo(drainedList);

    assertEquals(maxItems, totalDrained);
    assertEquals(offeredList, drainedList);
  }
Esempio n. 11
0
  @Test
  public void testPartialDrain() {
    final int maxItems = 15;
    final int itemsToDrain = maxItems / 2;

    final IQueue q = client.getQueue(randomString());

    List expectedList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      if (i < itemsToDrain) {
        expectedList.add(i);
      }
    }
    List drainedList = new LinkedList();
    int totalDrained = q.drainTo(drainedList, itemsToDrain);

    assertEquals(itemsToDrain, totalDrained);
    assertEquals(expectedList, drainedList);
  }
Esempio n. 12
0
 @Test
 public void testOffer() {
   final IQueue q = client.getQueue(randomString());
   assertTrue(q.offer(1));
   assertEquals(1, q.size());
 }
Esempio n. 13
0
 @Test
 public void testNotIsEmpty() {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertFalse(q.isEmpty());
 }
Esempio n. 14
0
 @Test
 public void testRemoveTop() throws IOException, InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.remove());
 }
Esempio n. 15
0
 @Test
 public void testOfferWithTimeOut() throws IOException, InterruptedException {
   final IQueue q = client.getQueue(randomString());
   boolean result = q.offer(1, 50, TimeUnit.MILLISECONDS);
   assertTrue(result);
 }