use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class RyaStreamsCommandsTest method listQueries.
@Test
public void listQueries() throws Exception {
// Mock the object that performs the rya streams operation.
final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
final ListQueries listQueries = mock(ListQueries.class);
when(mockClient.getListQueries()).thenReturn(listQueries);
final Set<StreamsQuery> queries = Sets.newHashSet(new StreamsQuery(UUID.fromString("33333333-3333-3333-3333-333333333333"), "SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false), new StreamsQuery(UUID.fromString("11111111-1111-1111-1111-111111111111"), "SELECT * WHERE { ?a ?b ?c . }", true, false), new StreamsQuery(UUID.fromString("22222222-2222-2222-2222-222222222222"), "SELECT * WHERE { ?d ?e ?f . }", false, false));
when(listQueries.all()).thenReturn(queries);
// Mock a shell state and connect it to a Rya instance.
final SharedShellState state = new SharedShellState();
state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
state.connectedToInstance("unitTest");
state.connectedToRyaStreams(mockClient);
// Execute the command.
final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
final String message = commands.listQueries();
// Verify the correct report is returned.
final String expected = "-----------------------------------------------\n" + " Query ID: 11111111-1111-1111-1111-111111111111\n" + "Is Active: true\n" + "Is Insert: false\n" + " SPARQL: select ?a ?b ?c\n" + " where {\n" + " ?a ?b ?c.\n" + " }\n" + "-----------------------------------------------\n" + " Query ID: 22222222-2222-2222-2222-222222222222\n" + "Is Active: false\n" + "Is Insert: false\n" + " SPARQL: select ?d ?e ?f\n" + " where {\n" + " ?d ?e ?f.\n" + " }\n" + "-----------------------------------------------\n" + " Query ID: 33333333-3333-3333-3333-333333333333\n" + "Is Active: true\n" + "Is Insert: false\n" + " SPARQL: select ?person ?business\n" + " where {\n" + " ?person <urn:worksAt> ?business.\n" + " }\n" + "-----------------------------------------------\n";
assertEquals(expected, message);
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class QueryEventWorkGeneratorTest method notifyUpdate_isNotActive.
@Test
public void notifyUpdate_isNotActive() 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", false, false);
final Thread notifyThread = new Thread(() -> {
final QueryChange change = QueryChange.update(queryId, false);
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.stopped("rya", queryId);
assertEquals(expected, event);
} finally {
shutdownSignal.set(true);
notifyThread.join();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class QueryEventWorkGeneratorTest method notifyCreate.
@Test
public void notifyCreate() 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 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 {
// 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 org.apache.rya.streams.api.entity.StreamsQuery 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();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class QueryManagerTest method testUpdateQuery.
/**
* Tests when the query manager is notified to update an existing query, the
* query is stopped.
*/
@Test
public void testUpdateQuery() throws Exception {
// The new QueryChangeLog
final QueryChangeLog newChangeLog = new InMemoryQueryChangeLog();
final StreamsQuery query = new StreamsQuery(UUID.randomUUID(), "some query", true, false);
final String ryaInstance = "ryaTestInstance";
// when the query executor is told to start the test query on the test
// rya instance, count down on the countdown latch
final QueryExecutor qe = mock(QueryExecutor.class);
when(qe.isRunning()).thenReturn(true);
final CountDownLatch queryStarted = new CountDownLatch(1);
final CountDownLatch queryDeleted = new CountDownLatch(1);
doAnswer(invocation -> {
queryDeleted.countDown();
return null;
}).when(qe).stopQuery(query.getQueryId());
final QueryChangeLogSource source = mock(QueryChangeLogSource.class);
// when the query executor is told to start the test query on the test
// rya instance, count down on the countdown latch
doAnswer(invocation -> {
queryStarted.countDown();
return null;
}).when(qe).startQuery(eq(ryaInstance), eq(query));
// When the QueryChangeLogSource is subscribed to in the QueryManager,
// mock notify of a new QueryChangeLog
// add the query, so it can be removed
doAnswer(invocation -> {
// The listener created by the Query Manager
final SourceListener listener = (SourceListener) invocation.getArguments()[0];
listener.notifyCreate(ryaInstance, newChangeLog);
Thread.sleep(1000);
newChangeLog.write(QueryChange.create(query.getQueryId(), query.getSparql(), query.isActive(), query.isInsert()));
queryStarted.await(5, TimeUnit.SECONDS);
newChangeLog.write(QueryChange.update(query.getQueryId(), false));
return null;
}).when(source).subscribe(any(SourceListener.class));
final QueryManager qm = new QueryManager(qe, source, 50, TimeUnit.MILLISECONDS);
try {
qm.startAndWait();
queryDeleted.await(10, TimeUnit.SECONDS);
verify(qe).stopQuery(query.getQueryId());
} finally {
qm.stopAndWait();
}
}
Aggregations