@Test public void testOneExpiredAnotherServed() throws Exception { final int size = 1024; final long capacity = 1024; final int numOfRequests = 3; final long[] blockTimeInMs = {10, 10, 1000}; SimpleByteBufferPool pool = new SimpleByteBufferPool(capacity); CountDownLatch[] allocated = new CountDownLatch[numOfRequests]; CountDownLatch[] used = new CountDownLatch[numOfRequests]; BufferConsumer[] consumers = new BufferConsumer[numOfRequests]; Thread[] consumerThreads = new Thread[numOfRequests]; for (int i = 0; i < numOfRequests; i++) { allocated[i] = new CountDownLatch(1); used[i] = new CountDownLatch(1); consumers[i] = new BufferConsumer(size, blockTimeInMs[i], pool, allocated[i], used[i]); consumerThreads[i] = new Thread(consumers[i]); } consumerThreads[0].start(); if (!allocated[0].await(100, TimeUnit.MILLISECONDS)) { fail("SimpleByteBufferPool takes too long time to allocate a buffer to BufferConsumer[0]."); } consumerThreads[1].start(); consumerThreads[2].start(); consumerThreads[1].join(); if (!(consumers[1].exception instanceof TimeoutException)) { throw consumers[1].exception; } used[0].countDown(); used[2].countDown(); consumerThreads[0].join(); consumerThreads[2].join(); if (consumers[0].exception != null) { throw consumers[0].exception; } else if (consumers[2].exception != null) { throw consumers[2].exception; } assertEquals(capacity, pool.availableMemory()); }
public long availableMemory() { return availableMemory; }
SimpleByteBufferPool implements ByteBufferPool { public long availableMemory() { return availableMemory; } }
SimpleByteBufferPool implements ByteBufferPool { public long availableMemory() { return availableMemory; } SimpleByteBufferPool(long capacity); }
SimpleByteBufferPool implements ByteBufferPool { public long availableMemory() { return availableMemory; } SimpleByteBufferPool(long capacity); @Override ByteBuffer allocate(int size, final long timeToBlockInMs); @Override void deallocate(ByteBuffer buffer); long availableMemory(); long capacity(); }
SimpleByteBufferPool implements ByteBufferPool { public long availableMemory() { return availableMemory; } SimpleByteBufferPool(long capacity); @Override ByteBuffer allocate(int size, final long timeToBlockInMs); @Override void deallocate(ByteBuffer buffer); long availableMemory(); long capacity();  }