use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqQueryDUnitTest method executeAndCloseAndExecuteIRMultipleTimes.
// helps test case where executeIR is called multiple times as well as after close
public void executeAndCloseAndExecuteIRMultipleTimes(VM vm, final String cqName, final String queryStr) {
vm.invoke(new CacheSerializableRunnable("Create CQ :" + cqName) {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCQService.");
}
// Create CQ Attributes.
CqAttributesFactory cqf = new CqAttributesFactory();
CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
cqf.initCqListeners(cqListeners);
CqAttributes cqa = cqf.create();
CqQuery cq1;
// Create CQ.
try {
cq1 = cqService.newCq(cqName, queryStr, cqa);
assertTrue("newCq() state mismatch", cq1.getState().isStopped());
} catch (Exception ex) {
AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
err.initCause(ex);
LogWriterUtils.getLogWriter().info("CqService is :" + cqService, err);
throw err;
}
try {
cq1.executeWithInitialResults();
try {
cq1.executeWithInitialResults();
} catch (IllegalStateException e) {
// expected
}
cq1.close();
try {
cq1.executeWithInitialResults();
} catch (CqClosedException e) {
// expected
return;
}
fail("should have received cqClosedException");
} catch (Exception e) {
fail("exception not expected here " + e);
}
}
});
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqQueryDUnitTest method stopExecCQ.
// Stop and execute CQ repeatedly
/* Stop/pause CQ */
private void stopExecCQ(VM vm, final String cqName, final int count) throws Exception {
vm.invoke(new CacheSerializableRunnable("Stop CQ :" + cqName) {
public void run2() throws CacheException {
CqQuery cq1 = null;
LogWriterUtils.getLogWriter().info("### Stop and Exec CQ. ###" + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCqService.");
}
// Get CQ.
try {
cq1 = cqService.getCq(cqName);
} catch (Exception ex) {
ex.printStackTrace();
fail("Failed to get CQ " + cqName + " . " + ex.getMessage());
}
for (int i = 0; i < count; ++i) {
// Stop CQ.
try {
cq1.stop();
} catch (Exception ex) {
ex.printStackTrace();
fail("Count = " + i + "Failed to stop CQ " + cqName + " . " + ex.getMessage());
}
assertTrue("Stop CQ state mismatch, count = " + i, cq1.getState().isStopped());
LogWriterUtils.getLogWriter().info("After stop in Stop and Execute loop, ran successfully, loop count: " + i);
LogWriterUtils.getLogWriter().info("CQ state: " + cq1.getState());
// Re-execute CQ
try {
cq1.execute();
} catch (Exception ex) {
ex.printStackTrace();
fail("Count = " + i + "Failed to execute CQ " + cqName + " . " + ex.getMessage());
}
assertTrue("Execute CQ state mismatch, count = " + i, cq1.getState().isRunning());
LogWriterUtils.getLogWriter().info("After execute in Stop and Execute loop, ran successfully, loop count: " + i);
LogWriterUtils.getLogWriter().info("CQ state: " + cq1.getState());
}
}
});
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqQueryDUnitTest method clearCQListenerEvents.
public void clearCQListenerEvents(VM vm, final String cqName) {
vm.invoke(new CacheSerializableRunnable("validate cq count") {
public void run2() throws CacheException {
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCQService.");
}
CqQuery cQuery = cqService.getCq(cqName);
if (cQuery == null) {
fail("Failed to get CqQuery for CQ : " + cqName);
}
CqAttributes cqAttr = cQuery.getCqAttributes();
CqListener cqListener = cqAttr.getCqListener();
CqQueryTestListener listener = (CqQueryTestListener) cqListener;
listener.getEventHistory();
}
});
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqQueryDUnitTest method waitForCqState.
/**
* Waits till the CQ state is same as the expected. Waits for max time, if the CQ state is not
* same as expected throws exception.
*/
public void waitForCqState(VM vm, final String cqName, final int state) {
vm.invoke(new CacheSerializableRunnable("Wait For cq State") {
public void run2() throws CacheException {
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCQService.");
}
CqQuery cQuery = cqService.getCq(cqName);
if (cQuery == null) {
fail("Failed to get CqQuery for CQ : " + cqName);
}
// Get CQ State.
final CqStateImpl cqState = (CqStateImpl) cQuery.getState();
// Wait max time, till the CQ state is as expected.
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return cqState.getState() == state;
}
public String description() {
return "cqState never became " + state;
}
};
Wait.waitForCriterion(ev, MAX_TIME, 200, true);
}
});
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqQueryDUnitTest method executeCQ.
/**
* Execute/register CQ as running.
*
* @param initialResults true if initialResults are requested
* @param expectedResultsSize if >= 0, validate results against this size
* @param expectedErr if not null, an error we expect
*/
private void executeCQ(VM vm, final String cqName, final boolean initialResults, final int expectedResultsSize, final String expectedErr) {
vm.invoke(new CacheSerializableRunnable("Execute CQ :" + cqName) {
private void work() throws CacheException {
// pause(60 * 1000);
LogWriterUtils.getLogWriter().info("### DEBUG EXECUTE CQ START ####");
// pause(20 * 1000);
// Get CQ Service.
QueryService cqService = null;
CqQuery cq1 = null;
// try {
cqService = getCache().getQueryService();
// Get CqQuery object.
try {
cq1 = cqService.getCq(cqName);
if (cq1 == null) {
LogWriterUtils.getLogWriter().info("Failed to get CqQuery object for CQ name: " + cqName);
fail("Failed to get CQ " + cqName);
} else {
LogWriterUtils.getLogWriter().info("Obtained CQ, CQ name: " + cq1.getName());
assertTrue("newCq() state mismatch", cq1.getState().isStopped());
}
} catch (Exception ex) {
LogWriterUtils.getLogWriter().info("CqService is :" + cqService);
LogWriterUtils.getLogWriter().error(ex);
AssertionError err = new AssertionError("Failed to execute CQ " + cqName);
err.initCause(ex);
throw err;
}
if (initialResults) {
SelectResults cqResults = null;
try {
cqResults = cq1.executeWithInitialResults();
} catch (Exception ex) {
LogWriterUtils.getLogWriter().info("CqService is :" + cqService);
ex.printStackTrace();
AssertionError err = new AssertionError("Failed to execute CQ " + cqName);
err.initCause(ex);
throw err;
}
LogWriterUtils.getLogWriter().info("initial result size = " + cqResults.size());
assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
if (expectedResultsSize >= 0) {
assertEquals("unexpected results size", expectedResultsSize, cqResults.size());
}
} else {
try {
cq1.execute();
} catch (Exception ex) {
AssertionError err = new AssertionError("Failed to execute CQ " + cqName);
err.initCause(ex);
if (expectedErr == null) {
LogWriterUtils.getLogWriter().info("CqService is :" + cqService, err);
}
throw err;
}
assertTrue("execute() state mismatch", cq1.getState().isRunning());
}
}
public void run2() throws CacheException {
if (expectedErr != null) {
getCache().getLogger().info("<ExpectedException action=add>" + expectedErr + "</ExpectedException>");
}
try {
work();
} finally {
if (expectedErr != null) {
getCache().getLogger().info("<ExpectedException action=remove>" + expectedErr + "</ExpectedException>");
}
}
}
});
}
Aggregations