Search in sources :

Example 76 with ArrayBlockingQueue

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();
    }
}
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)

Example 77 with ArrayBlockingQueue

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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) 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)

Example 78 with ArrayBlockingQueue

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();
    }
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) LogEvent(org.apache.rya.streams.querymanager.QueryManager.LogEvent) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) LogEventWorker(org.apache.rya.streams.querymanager.QueryManager.LogEventWorker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 79 with ArrayBlockingQueue

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));
}
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 80 with ArrayBlockingQueue

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();
    }
}
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)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)440 Test (org.junit.Test)158 ArrayList (java.util.ArrayList)75 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)74 IOException (java.io.IOException)66 CountDownLatch (java.util.concurrent.CountDownLatch)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 BlockingQueue (java.util.concurrent.BlockingQueue)34 ExecutorService (java.util.concurrent.ExecutorService)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 List (java.util.List)29 LocalAddress (io.netty.channel.local.LocalAddress)27 HashMap (java.util.HashMap)25 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)25 Subscription (rx.Subscription)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)23 File (java.io.File)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 LinkedList (java.util.LinkedList)21