コード例 #1
0
  public void testTakeSnapshot() throws InterruptedException {
    CoalescedPipe<String> coalescedPipe = new CoalescedPipe<String>();

    Object[] snapShot = coalescedPipe.takeSnapshot();

    assertEquals(0, snapShot.length);

    coalescedPipe.put("test1");

    snapShot = coalescedPipe.takeSnapshot();

    assertEquals(1, snapShot.length);
    assertEquals("test1", snapShot[0]);

    coalescedPipe.put("test2");

    snapShot = coalescedPipe.takeSnapshot();

    assertEquals(2, snapShot.length);
    assertEquals("test1", snapShot[0]);
    assertEquals("test2", snapShot[1]);
  }
コード例 #2
0
  public void testNonBlockingTake() throws InterruptedException {
    CoalescedPipe<String> coalescedPipe = new CoalescedPipe<String>();

    coalescedPipe.put("test2");
    coalescedPipe.put("test3");

    long startTime = System.currentTimeMillis();

    assertEquals("test2", coalescedPipe.take());
    assertTrue((System.currentTimeMillis() - startTime) < 100);

    startTime = System.currentTimeMillis();

    assertEquals("test3", coalescedPipe.take());
    assertTrue((System.currentTimeMillis() - startTime) < 100);
  }
コード例 #3
0
  public void testPut() throws InterruptedException {

    // Without comparator

    CoalescedPipe<String> coalescedPipe = new CoalescedPipe<String>();

    // Null

    try {
      coalescedPipe.put(null);

      fail();
    } catch (NullPointerException npe) {
    }

    // Normal

    coalescedPipe.put("test1");

    assertEquals(1, coalescedPipe.pendingCount());
    assertEquals(0, coalescedPipe.coalescedCount());

    coalescedPipe.put("test2");

    assertEquals(2, coalescedPipe.pendingCount());
    assertEquals(0, coalescedPipe.coalescedCount());

    // Coalesce

    coalescedPipe.put("test1");

    assertEquals(2, coalescedPipe.pendingCount());
    assertEquals(1, coalescedPipe.coalescedCount());

    coalescedPipe.put("test2");

    assertEquals(2, coalescedPipe.pendingCount());
    assertEquals(2, coalescedPipe.coalescedCount());

    // With comparator

    coalescedPipe =
        new CoalescedPipe<String>(
            new Comparator<String>() {

              public int compare(String o1, String o2) {
                return o1.length() - o2.length();
              }
            });

    // Null

    try {
      coalescedPipe.put(null);

      fail();
    } catch (NullPointerException npe) {
    }

    // Normal

    coalescedPipe.put("a");

    assertEquals(1, coalescedPipe.pendingCount());
    assertEquals(0, coalescedPipe.coalescedCount());

    coalescedPipe.put("ab");

    assertEquals(2, coalescedPipe.pendingCount());
    assertEquals(0, coalescedPipe.coalescedCount());

    // Coalesce

    coalescedPipe.put("c");

    assertEquals(2, coalescedPipe.pendingCount());
    assertEquals(1, coalescedPipe.coalescedCount());

    coalescedPipe.put("cd");

    assertEquals(2, coalescedPipe.pendingCount());
    assertEquals(2, coalescedPipe.coalescedCount());
  }