public static void waitForCommandSequenceNumber( final CommandQueue commandQueue, final KsqlRequest request, final Duration timeout ) throws InterruptedException, TimeoutException { final Optional<Long> commandSequenceNumber = request.getCommandSequenceNumber(); if (commandSequenceNumber.isPresent()) { final long seqNum = commandSequenceNumber.get(); commandQueue.ensureConsumedPast(seqNum, timeout); } } }
@Test public void shouldHandleNullCommandNumber() { assertThat(new KsqlRequest("sql", SOME_PROPS, null).getCommandSequenceNumber(), is(Optional.empty())); }
@Test public void shouldWaitIfSequenceNumberSpecified() throws Exception { // Given: when(request.getCommandSequenceNumber()).thenReturn(Optional.of(SEQUENCE_NUMBER)); // When: CommandStoreUtil.waitForCommandSequenceNumber(commandQueue, request, TIMEOUT); // Then: verify(commandQueue).ensureConsumedPast(SEQUENCE_NUMBER, TIMEOUT); }
@Test public void shouldNotWaitIfNoSequenceNumberSpecified() throws Exception { // Given: when(request.getCommandSequenceNumber()).thenReturn(Optional.empty()); // When: CommandStoreUtil.waitForCommandSequenceNumber(commandQueue, request, TIMEOUT); // Then: verify(commandQueue, never()).ensureConsumedPast(anyLong(), any()); }
@Test public void shouldThrowKsqlRestExceptionOnTimeout() throws Exception { // Given: when(request.getCommandSequenceNumber()).thenReturn(Optional.of(SEQUENCE_NUMBER)); doThrow(new TimeoutException("uh oh")) .when(commandQueue).ensureConsumedPast(SEQUENCE_NUMBER, TIMEOUT); // Expect: expectedException.expect(KsqlRestException.class); expectedException.expect(exceptionStatusCode(is(Code.SERVICE_UNAVAILABLE))); expectedException.expect(exceptionKsqlErrorMessage(errorMessage(is("uh oh")))); // When: CommandStoreUtil.httpWaitForCommandSequenceNumber(commandQueue, request, TIMEOUT); } }