use of java.util.concurrent.ArrayBlockingQueue in project incubator-rya by apache.
the class QueryEventWorkGeneratorTest method notifyUpdate_isActive.
@Test
public void notifyUpdate_isActive() throws Exception {
// The signal that will kill the notifying thread.
final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
// The queue generated work is offered to.
final BlockingQueue<QueryEvent> queue = new ArrayBlockingQueue<>(1);
// The listener that will perform the QueryEventWorkGenerator work.
final CountDownLatch latch = new CountDownLatch(1);
latch.countDown();
final QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", latch, queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
// A thread that will attempt to notify the generator with an update query change.
final UUID queryId = UUID.randomUUID();
final StreamsQuery query = new StreamsQuery(queryId, "query", true, false);
final Thread notifyThread = new Thread(() -> {
final QueryChange change = QueryChange.update(queryId, true);
final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
generator.notify(entry, Optional.of(query));
});
// Start the thread.
notifyThread.start();
try {
// Show work was added to the queue and the notifying thread died.
final QueryEvent event = queue.poll(500, TimeUnit.MILLISECONDS);
final QueryEvent expected = QueryEvent.executing("rya", new StreamsQuery(queryId, query.getSparql(), query.isActive(), query.isInsert()));
assertEquals(expected, event);
} finally {
shutdownSignal.set(true);
notifyThread.join();
}
}
use of java.util.concurrent.ArrayBlockingQueue in project incubator-rya by apache.
the class QueryEventWorkGeneratorTest method notifyDelete.
@Test
public void notifyDelete() throws Exception {
// The signal that will kill the notifying thread.
final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
// The queue generated work is offered to.
final BlockingQueue<QueryEvent> queue = new ArrayBlockingQueue<>(1);
// The listener that will perform the QueryEventWorkGenerator work.
final CountDownLatch latch = new CountDownLatch(1);
latch.countDown();
final QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", latch, queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
// A thread that will attempt to notify the generator with a deleted query.
final UUID queryId = UUID.randomUUID();
final Thread notifyThread = new Thread(() -> {
final QueryChange change = QueryChange.delete(queryId);
final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
generator.notify(entry, Optional.empty());
});
// Start the thread.
notifyThread.start();
try {
// Show work was added to the queue and the notifying thread died.
final QueryEvent event = queue.poll(500, TimeUnit.MILLISECONDS);
final QueryEvent expected = QueryEvent.stopped("rya", queryId);
assertEquals(expected, event);
} finally {
shutdownSignal.set(true);
notifyThread.join();
}
}
use of java.util.concurrent.ArrayBlockingQueue in project incubator-rya by apache.
the class LogEventWorkerTest method nofity_logCreated_exists.
@Test
public void nofity_logCreated_exists() throws Exception {
// The signal that will kill the working thread.
final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
// The queue used to feed work.
final BlockingQueue<LogEvent> logEventQueue = new ArrayBlockingQueue<>(10);
// The queue work is written to.
final BlockingQueue<QueryEvent> queryEventQueue = new ArrayBlockingQueue<>(10);
// The Query Change Log that will be watched.
final QueryChangeLog changeLog = new InMemoryQueryChangeLog();
// Write a message that indicates a new query should be active.
final UUID firstQueryId = UUID.randomUUID();
changeLog.write(QueryChange.create(firstQueryId, "select * where { ?a ?b ?c . }", true, false));
// Start the worker that will be tested.
final Thread logEventWorker = new Thread(new LogEventWorker(logEventQueue, queryEventQueue, 50, TimeUnit.MILLISECONDS, shutdownSignal));
logEventWorker.start();
try {
// Write a unit of work that indicates a log was created.
final LogEvent createLogEvent = LogEvent.create("rya", changeLog);
logEventQueue.offer(createLogEvent);
// Say the same log was created a second time.
logEventQueue.offer(createLogEvent);
// Show that only a single unit of work was added for the log. This indicates the
// second message was effectively skipped as it would have add its work added twice otherwise.
final Set<QueryEvent> expectedEvents = new HashSet<>();
expectedEvents.add(QueryEvent.executing("rya", new StreamsQuery(firstQueryId, "select * where { ?a ?b ?c . }", true, false)));
final Set<QueryEvent> queryEvents = new HashSet<>();
queryEvents.add(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
assertNull(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
assertEquals(expectedEvents, queryEvents);
} finally {
shutdownSignal.set(true);
logEventWorker.join();
}
}
use of java.util.concurrent.ArrayBlockingQueue in project incubator-rya by apache.
the class QueryEventWorkerTest method shutdownSignalKillsThread.
@Test
public void shutdownSignalKillsThread() {
// The signal that will kill the working thread.
final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
// The thread that will perform the QueryEventWorker task.
final Thread queryEventWorker = new Thread(new QueryEventWorker(new ArrayBlockingQueue<>(1), mock(QueryExecutor.class), 50, TimeUnit.MILLISECONDS, shutdownSignal));
queryEventWorker.start();
// Wait longer than the poll time to see if the thread died. Show that it is still running.
assertTrue(ThreadUtil.stillAlive(queryEventWorker, 200));
// Set the shutdown signal to true and join the thread. If we were able to join, then it shut down.
shutdownSignal.set(true);
assertFalse(ThreadUtil.stillAlive(queryEventWorker, 1000));
}
use of java.util.concurrent.ArrayBlockingQueue in project incubator-rya by apache.
the class QueryEventWorkerTest method stopAllWork.
@Test
public void stopAllWork() throws Exception {
// The signal that will kill the working thread.
final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
// The queue used to send the execute work to the thread.
final BlockingQueue<QueryEvent> queue = new ArrayBlockingQueue<>(1);
// The message that indicates all queries for a rya instance need to be stopped.
final String ryaInstance = "rya";
final QueryEvent stopAllEvent = QueryEvent.stopALL(ryaInstance);
// Release a latch if the stopQuery method on the queryExecutor is invoked with the correct values.
final CountDownLatch testMethodInvoked = new CountDownLatch(1);
final QueryExecutor queryExecutor = mock(QueryExecutor.class);
doAnswer(invocation -> {
testMethodInvoked.countDown();
return null;
}).when(queryExecutor).stopAll(ryaInstance);
final Thread queryEventWorker = new Thread(new QueryEventWorker(queue, queryExecutor, 50, TimeUnit.MILLISECONDS, shutdownSignal));
try {
// The thread that will perform the QueryEventWorker task.
queryEventWorker.start();
// Provide a message indicating a query needs to be executing.
queue.put(stopAllEvent);
// Verify the Query Executor was told to stop all the queries.
assertTrue(testMethodInvoked.await(150, TimeUnit.MILLISECONDS));
} finally {
shutdownSignal.set(true);
queryEventWorker.join();
}
}
Aggregations