// Tests that simple task scheduling and execution works.
  @Test
  public void schedule() throws Exception {
    final int THREAD_COUNT = 4, MEMORY_SIZE = 1024, TASK_COUNT = 100;
    final BoundedThreadPool pool = new BoundedThreadPool(THREAD_COUNT, MEMORY_SIZE);

    final LongAdder counter = new LongAdder();
    for (int i = 0; i < TASK_COUNT; i++) {
      Assert.assertTrue(pool.schedule(0, counter::increment));
    }
    pool.terminate(Terminate.AWAIT);
    Assert.assertEquals(TASK_COUNT, counter.intValue());
  }
  // Tests that scheduled timeouts are complied with.
  @Test
  public void timeout() throws Exception {
    final int THREAD_COUNT = 1, MEMORY_SIZE = 1024, TASK_COUNT = 10, TIMEOUT = 100;
    final BoundedThreadPool pool = new BoundedThreadPool(THREAD_COUNT, MEMORY_SIZE);

    final LongAdder counter = new LongAdder();
    for (int i = 0; i < TASK_COUNT; i++) {
      final boolean isScheduled =
          pool.schedule(
              0,
              TIMEOUT,
              () -> {
                counter.increment();
                try {
                  Thread.sleep(2 * TIMEOUT);
                } catch (InterruptedException e) {
                  throw new ProtocolException(e);
                }
              });
      Assert.assertTrue(isScheduled);
    }
    pool.terminate(Terminate.AWAIT);
    Assert.assertEquals(1, counter.intValue());
  }
Example #3
0
 public int getInventoryItemCount() {
   if (getSlottedObject("inventory") == null) return 0;
   LongAdder adder = new LongAdder();
   getSlottedObject("inventory").viewChildren(this, true, true, (obj) -> adder.increment());
   return adder.intValue();
 }