Search in sources :

Example 6 with QueryEventWorkGenerator

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

the class QueryEventWorkGeneratorTest method waitsForSubscriptionWork.

@Test
public void waitsForSubscriptionWork() 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);
    final QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", latch, queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
    // A thread that will attempt to notify the generator with a created query.
    final UUID queryId = UUID.randomUUID();
    final StreamsQuery query = new StreamsQuery(queryId, "query", true, false);
    final Thread notifyThread = new Thread(() -> {
        final QueryChange change = QueryChange.create(queryId, query.getSparql(), query.isActive(), query.isInsert());
        final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
        generator.notify(entry, Optional.of(query));
    });
    // Start the thread.
    notifyThread.start();
    try {
        // Wait longer than the blocking period and show the thread is still alive and nothing has been added
        // to the work queue.
        Thread.sleep(150);
        assertTrue(notifyThread.isAlive());
        // Count down the latch.
        latch.countDown();
        // 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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEventWorkGenerator(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ChangeLogEntry (org.apache.rya.streams.api.queries.ChangeLogEntry)6 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)6 QueryEventWorkGenerator (org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator)6 Test (org.junit.Test)6 UUID (java.util.UUID)5 QueryChange (org.apache.rya.streams.api.queries.QueryChange)5 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)4