private void populate(BlockingDrainingQueue<Integer> queue) { for (int i = 0; i < ITEM_COUNT; i++) { queue.add(i); } } }
/** * Performs Tail & Catchup reads by issuing a long read from the beginning to infinity on the target, processing the * output of that read as a 'tail read', then re-issuing concise reads from the store for those offsets. */ private CompletableFuture<Void> processTailReads() { return this.reader.readAll(this.streamName, this::processTailEvent, this.cancellationToken) .thenRun(() -> this.catchupQueue.add(new EndItem())); // Signal that we are done. }
/** * Processes the given Operation. This method returns when the given Operation has been added to the internal queue. * * @param operation The Operation to process. * @return A CompletableFuture that, when completed, will indicate the Operation has finished processing. If the * Operation completed successfully, the Future will contain the Sequence Number of the Operation. If the Operation * failed, it will contain the exception that caused the failure. * @throws IllegalContainerStateException If the OperationProcessor is not running. */ public CompletableFuture<Void> process(Operation operation) { CompletableFuture<Void> result = new CompletableFuture<>(); if (!isRunning()) { result.completeExceptionally(new IllegalContainerStateException("OperationProcessor is not running.")); } else { log.debug("{}: process {}.", this.traceObjectId, operation); try { this.operationQueue.add(new CompletableOperation(operation, result)); } catch (Throwable e) { if (Exceptions.mustRethrow(e)) { throw e; } result.completeExceptionally(e); } } return result; }
this.catchupQueue.add(readItem);
/** * Tests the basic ability dequeue items using poll() as they are added. */ @Test public void testAddSinglePoll() throws Exception { @Cleanup BlockingDrainingQueue<Integer> queue = new BlockingDrainingQueue<>(); for (int i = 0; i < ITEM_COUNT; i++) { queue.add(i); Queue<Integer> entries = queue.poll(MAX_READ_COUNT); Assert.assertEquals("Unexpected number of items polled.", 1, entries.size()); Assert.assertEquals("Unexpected value polled from queue.", i, (int) entries.peek()); } val remainingItems = queue.poll(1); Assert.assertEquals("poll() did not return an empty collection when queue was empty.", 0, remainingItems.size()); }
/** * Tests the basic ability to dequeue items using take() as they are added. */ @Test public void testAddSingleTake() throws Exception { @Cleanup BlockingDrainingQueue<Integer> queue = new BlockingDrainingQueue<>(); for (int i = 0; i < ITEM_COUNT; i++) { queue.add(i); val takeResult = queue.take(MAX_READ_COUNT); Assert.assertTrue("take() returned an incomplete Future when data is available.", Futures.isSuccessful(takeResult)); val entries = takeResult.join(); Assert.assertEquals("Unexpected number of items polled.", 1, entries.size()); Assert.assertEquals("Unexpected value polled from queue.", i, (int) entries.peek()); } val remainingItems = queue.take(1); Assert.assertFalse("take() did not return an incomplete future when queue was empty.", remainingItems.isDone()); }
/** * Tests the ability to cancel a pending take() operation. */ @Test public void testCancelPendingTake() throws Exception { final int valueToQueue = 1234; @Cleanup BlockingDrainingQueue<Integer> queue = new BlockingDrainingQueue<>(); val takeResult = queue.take(MAX_READ_COUNT); Assert.assertFalse("take() returned a completed future.", takeResult.isDone()); queue.cancelPendingTake(); Assert.assertTrue("cancelPendingTake() did not cancel a pending take() future.", takeResult.isCancelled()); val takeResult2 = queue.take(MAX_READ_COUNT); queue.add(valueToQueue); Assert.assertEquals("take() did not work again after being cancelled.", valueToQueue, (int) takeResult2.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).poll()); }
queue.add(valueToQueue);