Search in sources :

Example 1 with QueryEventWorker

use of org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker 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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueryEventWorker(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker) Test(org.junit.Test)

Example 2 with QueryEventWorker

use of org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker 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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueryEventWorker(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with QueryEventWorker

use of org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker in project incubator-rya by apache.

the class QueryEventWorkerTest method executingWork.

@Test
public void executingWork() 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 a query needs to be executed.
    final String ryaInstance = "rya";
    final StreamsQuery query = new StreamsQuery(UUID.randomUUID(), "sparql", true, false);
    final QueryEvent executingEvent = QueryEvent.executing(ryaInstance, query);
    // Release a latch if the startQuery method on the queryExecutor is invoked with the correct values.
    final CountDownLatch startQueryInvoked = new CountDownLatch(1);
    final QueryExecutor queryExecutor = mock(QueryExecutor.class);
    doAnswer(invocation -> {
        startQueryInvoked.countDown();
        return null;
    }).when(queryExecutor).startQuery(ryaInstance, query);
    // The thread that will perform the QueryEventWorker task.
    final Thread queryEventWorker = new Thread(new QueryEventWorker(queue, queryExecutor, 50, TimeUnit.MILLISECONDS, shutdownSignal));
    try {
        queryEventWorker.start();
        // Provide a message indicating a query needs to be executing.
        queue.put(executingEvent);
        // Verify the Query Executor was told to start the query.
        assertTrue(startQueryInvoked.await(150, TimeUnit.MILLISECONDS));
    } finally {
        shutdownSignal.set(true);
        queryEventWorker.join();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEventWorker(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with QueryEventWorker

use of org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker in project incubator-rya by apache.

the class QueryEventWorkerTest method stoppedWork.

@Test
public void stoppedWork() 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 a query needs to be stopped.
    final UUID queryId = UUID.randomUUID();
    final QueryEvent stoppedEvent = QueryEvent.stopped("rya", queryId);
    // Release a latch if the stopQuery method on the queryExecutor is invoked with the correct values.
    final CountDownLatch stopQueryInvoked = new CountDownLatch(1);
    final QueryExecutor queryExecutor = mock(QueryExecutor.class);
    doAnswer(invocation -> {
        stopQueryInvoked.countDown();
        return null;
    }).when(queryExecutor).stopQuery(queryId);
    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(stoppedEvent);
        // Verify the Query Executor was told to stop the query.
        assertTrue(stopQueryInvoked.await(150, TimeUnit.MILLISECONDS));
    } finally {
        shutdownSignal.set(true);
        queryEventWorker.join();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueryEventWorker(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 QueryEventWorker (org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)3 UUID (java.util.UUID)1 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)1