use of org.apache.commons.lang3.mutable.MutableBoolean in project apex-malhar by apache.
the class SimpleDoneQueryQueueManagerTest method expiredTestBlockingValidFirstExpiredLast.
@Test
public void expiredTestBlockingValidFirstExpiredLast() throws Exception {
SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();
sdqqm.setup(null);
sdqqm.beginWindow(0);
Query query = new MockQuery("1");
MutableBoolean queueContext = new MutableBoolean(false);
sdqqm.enqueue(query, null, queueContext);
Query query1 = new MockQuery("2");
MutableBoolean queueContext1 = new MutableBoolean(false);
sdqqm.enqueue(query1, null, queueContext1);
Assert.assertEquals(2, sdqqm.getNumLeft());
Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeueBlock();
Assert.assertEquals(1, sdqqm.getNumLeft());
Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
sdqqm.endWindow();
sdqqm.beginWindow(1);
Assert.assertEquals(2, sdqqm.getNumLeft());
Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
queueContext1.setValue(true);
qb = sdqqm.dequeueBlock();
Assert.assertEquals(1, sdqqm.getNumLeft());
Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
testBlocking(sdqqm);
Assert.assertEquals(0, sdqqm.getNumLeft());
Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
sdqqm.endWindow();
sdqqm.beginWindow(2);
Assert.assertEquals(1, sdqqm.getNumLeft());
Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
qb = sdqqm.dequeueBlock();
testBlocking(sdqqm);
sdqqm.endWindow();
sdqqm.teardown();
}
use of org.apache.commons.lang3.mutable.MutableBoolean in project apex-malhar by apache.
the class SimpleDoneQueryQueueManagerTest method simpleExpireBlockThenUnblock.
@Test
public void simpleExpireBlockThenUnblock() throws Exception {
SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();
sdqqm.setup(null);
sdqqm.beginWindow(0);
Query query = new MockQuery("1");
MutableBoolean expire = new MutableBoolean(false);
sdqqm.enqueue(query, null, expire);
sdqqm.endWindow();
sdqqm.beginWindow(1);
// Expire
expire.setValue(true);
ExceptionSaverExceptionHandler eseh = new ExceptionSaverExceptionHandler();
testBlockingNoStop(sdqqm, eseh);
query = new MockQuery("2");
sdqqm.enqueue(query, null, new MutableBoolean(false));
Thread.sleep(1000);
Assert.assertNull(eseh.getCaughtThrowable());
sdqqm.endWindow();
sdqqm.teardown();
}
use of org.apache.commons.lang3.mutable.MutableBoolean in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldStreamRowsCorrectly.
@Test
public void shouldStreamRowsCorrectly() throws Throwable {
final int NUM_ROWS = 5;
final AtomicReference<Throwable> threadException = new AtomicReference<>(null);
final Thread.UncaughtExceptionHandler threadExceptionHandler = (thread, exception) -> threadException.compareAndSet(null, exception);
final String queryString = "SELECT * FROM test_stream;";
final SynchronousQueue<KeyValueMetadata<List<?>, GenericRow>> rowQueue = new SynchronousQueue<>();
final LinkedList<GenericRow> writtenRows = new LinkedList<>();
final Thread rowQueuePopulatorThread = new Thread(() -> {
try {
for (int i = 0; i != NUM_ROWS; i++) {
final GenericRow value = genericRow(i);
synchronized (writtenRows) {
writtenRows.add(value);
}
rowQueue.put(new KeyValueMetadata<>(KeyValue.keyValue(null, value)));
}
} catch (final InterruptedException exception) {
// This should happen during the test, so it's fine
}
}, "Row Queue Populator");
rowQueuePopulatorThread.setUncaughtExceptionHandler(threadExceptionHandler);
rowQueuePopulatorThread.start();
final KafkaStreams mockKafkaStreams = mock(KafkaStreams.class);
when(mockStatementParser.<Query>parseSingleStatement(queryString)).thenReturn(query);
final Map<String, Object> requestStreamsProperties = Collections.emptyMap();
final KafkaStreamsBuilder kafkaStreamsBuilder = mock(KafkaStreamsBuilder.class);
when(kafkaStreamsBuilder.build(any(), any())).thenReturn(mockKafkaStreams);
MutableBoolean closed = new MutableBoolean(false);
when(mockKafkaStreams.close(any())).thenAnswer(i -> {
closed.setValue(true);
return true;
});
when(mockKafkaStreams.state()).thenAnswer(i -> closed.getValue() ? State.NOT_RUNNING : State.RUNNING);
final TransientQueryMetadata transientQueryMetadata = new TransientQueryMetadata(queryString, SOME_SCHEMA, Collections.emptySet(), "", new TestRowQueue(rowQueue), queryId, "appId", mock(Topology.class), kafkaStreamsBuilder, Collections.emptyMap(), Collections.emptyMap(), closeTimeout, 10, ResultType.STREAM, 0L, 0L, listener);
transientQueryMetadata.initialize();
when(queryMetadataHolder.getPushQueryMetadata()).thenReturn(Optional.of(transientQueryMetadata));
final EndpointResponse response = testResource.streamQuery(securityContext, new KsqlRequest(queryString, requestStreamsProperties, Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context);
final PipedOutputStream responseOutputStream = new EOFPipedOutputStream();
final PipedInputStream responseInputStream = new PipedInputStream(responseOutputStream, 1);
final StreamingOutput responseStream = (StreamingOutput) response.getEntity();
final Thread queryWriterThread = new Thread(() -> {
try {
responseStream.write(responseOutputStream);
} catch (final EOFException exception) {
// It's fine
} catch (final IOException exception) {
throw new RuntimeException(exception);
}
}, "Query Writer");
queryWriterThread.setUncaughtExceptionHandler(threadExceptionHandler);
queryWriterThread.start();
final Scanner responseScanner = new Scanner(responseInputStream, "UTF-8");
final ObjectMapper objectMapper = ApiJsonMapper.INSTANCE.get();
for (int i = 0; i != NUM_ROWS; i++) {
if (!responseScanner.hasNextLine()) {
throw new Exception("Response input stream failed to have expected line available");
}
final String responseLine = responseScanner.nextLine();
String jsonLine = StringUtils.stripStart(responseLine, "[");
jsonLine = StringUtils.stripEnd(jsonLine, ",");
jsonLine = StringUtils.stripEnd(jsonLine, "]");
if (jsonLine.isEmpty()) {
i--;
continue;
}
if (i == 0) {
// Header:
assertThat(jsonLine, is("{\"header\":{\"queryId\":\"queryId\",\"schema\":\"`f1` INTEGER\"}}"));
continue;
}
final GenericRow expectedRow;
synchronized (writtenRows) {
expectedRow = writtenRows.poll();
}
final DataRow testRow = objectMapper.readValue(jsonLine, StreamedRow.class).getRow().get();
assertThat(testRow.getColumns(), is(expectedRow.values()));
}
responseOutputStream.close();
queryWriterThread.join();
rowQueuePopulatorThread.interrupt();
rowQueuePopulatorThread.join();
// Definitely want to make sure that the Kafka Streams instance has been closed and cleaned up
verify(mockKafkaStreams).start();
// called on init and when setting uncaught exception handler manually
verify(mockKafkaStreams, times(2)).setUncaughtExceptionHandler(any(StreamsUncaughtExceptionHandler.class));
verify(mockKafkaStreams).cleanUp();
verify(mockKafkaStreams).close(Duration.ofMillis(closeTimeout));
// If one of the other threads has somehow managed to throw an exception without breaking things up until this
// point, we throw that exception now in the main thread and cause the test to fail
final Throwable exception = threadException.get();
if (exception != null) {
throw exception;
}
}
use of org.apache.commons.lang3.mutable.MutableBoolean in project neo4j by neo4j.
the class RunnableBoltWorkerTest method runDoesNothingAfterHalt.
@Test
public void runDoesNothingAfterHalt() {
RunnableBoltWorker worker = new RunnableBoltWorker(machine, logService);
MutableBoolean jobWasExecuted = new MutableBoolean();
worker.enqueue(machine1 -> {
jobWasExecuted.setTrue();
fail("Should not be executed");
});
worker.halt();
worker.run();
assertFalse(jobWasExecuted.booleanValue());
verify(machine).close();
}
use of org.apache.commons.lang3.mutable.MutableBoolean in project neo4j by neo4j.
the class SeekCursorTest method shouldCatchupRootWhenNodeHasTooNewGenerationWhileTraversingLeaves.
@Test
public void shouldCatchupRootWhenNodeHasTooNewGenerationWhileTraversingLeaves() throws Exception {
// given
MutableBoolean triggered = new MutableBoolean(false);
// a newer right leaf
long rightChild = cursor.getCurrentPageId();
node.initializeLeaf(cursor, stableGeneration, unstableGeneration);
cursor.next();
Supplier<Root> rootCatchup = () -> {
try {
// Use right child as new start over root to terminate test
cursor.next(rightChild);
triggered.setTrue();
return new Root(cursor.getCurrentPageId(), node.generation(cursor));
} catch (IOException e) {
throw new RuntimeException(e);
}
};
// a left leaf
long leftChild = cursor.getCurrentPageId();
node.initializeLeaf(cursor, stableGeneration - 1, unstableGeneration - 1);
// with an old pointer to right sibling
node.setRightSibling(cursor, rightChild, stableGeneration - 1, unstableGeneration - 1);
cursor.next();
// a root
node.initializeInternal(cursor, stableGeneration - 1, unstableGeneration - 1);
long keyInRoot = 10L;
insertKey.setValue(keyInRoot);
node.insertKeyAt(cursor, insertKey, 0, 0);
node.setKeyCount(cursor, 1);
// with old pointer to child (simulating reuse of internal node)
node.setChildAt(cursor, leftChild, 0, stableGeneration, unstableGeneration);
// when
from.setValue(1L);
to.setValue(20L);
try (SeekCursor<MutableLong, MutableLong> seek = new SeekCursor<>(cursor, node, from, to, layout, stableGeneration - 1, unstableGeneration - 1, generationSupplier, rootCatchup, unstableGeneration)) {
while (seek.next()) {
seek.get();
}
}
// then
assertTrue(triggered.getValue());
}
Aggregations