SpillingQueueElement { SpillingQueueElement(final TransferEnvelope transferEnvelope) { if (transferEnvelope == null) { throw new IllegalArgumentException("Argument transferEnvelope must not be null"); } this.jobID = transferEnvelope.getJobID(); this.source = transferEnvelope.getSource(); add(transferEnvelope); } SpillingQueueElement(final TransferEnvelope transferEnvelope);   }
@Test public void testSpillingQueueElement() { final ByteBuffer byteBuf1 = ByteBuffer.allocate(BUFFER_SIZE); final ByteBuffer byteBuf2 = ByteBuffer.allocate(BUFFER_SIZE); final Queue<ByteBuffer> queue = new ArrayDeque<ByteBuffer>(); final JobID jobID = new JobID(); final ChannelID source = new ChannelID(); final BufferPoolConnector connector = new BufferPoolConnector(queue); final Buffer buf1 = BufferFactory.createFromMemory(BUFFER_SIZE, byteBuf1, connector); final Buffer buf2 = BufferFactory.createFromMemory(BUFFER_SIZE, byteBuf2, connector); final TransferEnvelope te1 = new TransferEnvelope(0, jobID, source); te1.setBuffer(buf1); final TransferEnvelope te2 = new TransferEnvelope(1, jobID, source); final TransferEnvelope te3 = new TransferEnvelope(2, jobID, source); te3.setBuffer(buf2); assertTrue(te1.equals(te1)); final SpillingQueueElement elem = new SpillingQueueElement(te1); assertFalse(elem.canBeAdded(te1)); assertTrue(elem.canBeAdded(te2)); assertFalse(elem.canBeAdded(te3)); elem.add(te2); elem.add(te3); assertEquals(3, elem.size()); assertEquals(te1, elem.poll()); assertEquals(2, elem.size()); assertEquals(te2, elem.poll()); assertEquals(1, elem.size()); assertEquals(te3, elem.poll()); assertEquals(0, elem.size()); assertTrue(elem.poll() == null); elem.add(te1); elem.add(te2); assertEquals(te1, elem.poll()); elem.add(te3); assertEquals(te2, elem.poll()); assertEquals(te3, elem.poll()); assertEquals(0, elem.size()); assertEquals(0, elem.size()); elem.add(te2); elem.add(te3); assertEquals(te2, elem.peek()); assertEquals(2, elem.size()); assertEquals(te2, elem.poll()); assertEquals(te3, elem.peek()); assertEquals(te3, elem.poll()); assertEquals(0, elem.size()); elem.add(te1); elem.add(te2); elem.add(te3); final Iterator<TransferEnvelope> it = elem.iterator(); int count = 0; while (it.hasNext()) { switch (count++) { case 0: assertEquals(te1, it.next()); break; case 1: assertEquals(te2, it.next()); break; case 2: assertEquals(te3, it.next()); break; default: fail("Iterator moved to forth element which does not exist"); } } assertTrue(queue.isEmpty()); elem.clear(); assertEquals(2, queue.size()); }