@Test public void fillAndDrainPool() throws Exception { OkBuffer buffer = new OkBuffer(); // Take 2 * MAX_SIZE segments. This will drain the pool, even if other tests filled it. buffer.write(new byte[(int) SegmentPool.MAX_SIZE]); buffer.write(new byte[(int) SegmentPool.MAX_SIZE]); assertEquals(0, SegmentPool.INSTANCE.byteCount); // Recycle MAX_SIZE segments. They're all in the pool. buffer.readByteString(SegmentPool.MAX_SIZE); assertEquals(SegmentPool.MAX_SIZE, SegmentPool.INSTANCE.byteCount); // Recycle MAX_SIZE more segments. The pool is full so they get garbage collected. buffer.readByteString(SegmentPool.MAX_SIZE); assertEquals(SegmentPool.MAX_SIZE, SegmentPool.INSTANCE.byteCount); // Take MAX_SIZE segments to drain the pool. buffer.write(new byte[(int) SegmentPool.MAX_SIZE]); assertEquals(0, SegmentPool.INSTANCE.byteCount); // Take MAX_SIZE more segments. The pool is drained so these will need to be allocated. buffer.write(new byte[(int) SegmentPool.MAX_SIZE]); assertEquals(0, SegmentPool.INSTANCE.byteCount); }
@Override public ByteString readByteString(int byteCount) throws IOException { require(byteCount); return buffer.readByteString(byteCount); }