Пример #1
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);
  }
Пример #2
0
  public void testBlockingTake() throws InterruptedException {
    final CoalescedPipe<String> coalescedPipe = new CoalescedPipe<String>();

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    scheduledExecutorService.schedule(
        new Runnable() {

          public void run() {
            try {
              coalescedPipe.put("test1");
            } catch (InterruptedException ie) {
              fail(ie.getMessage());
            }
          }
        },
        500,
        TimeUnit.MILLISECONDS);

    long startTime = System.currentTimeMillis();

    assertEquals("test1", coalescedPipe.take());
    assertTrue((System.currentTimeMillis() - startTime) > 250L);

    scheduledExecutorService.shutdownNow();
    scheduledExecutorService.awaitTermination(120, TimeUnit.SECONDS);
  }