Search in sources :

Example 31 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class CqQueryUsingPoolDUnitTest 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
   */
public void executeCQ(VM vm, final String cqName, final boolean initialResults, final int expectedResultsSize, final String[] expectedKeys, final String expectedErr) {
    vm.invoke(new CacheSerializableRunnable("Execute CQ :" + cqName) {

        private void work() throws CacheException {
            LogWriterUtils.getLogWriter().info("### DEBUG EXECUTE CQ START ####");
            // Get CQ Service.
            QueryService cqService = null;
            CqQuery cq1 = null;
            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);
                Assert.fail("Failed to execute  CQ " + cqName, ex);
            }
            if (initialResults) {
                SelectResults cqResults = null;
                try {
                    cqResults = cq1.executeWithInitialResults();
                } catch (Exception ex) {
                    fail("Failed to execute  CQ " + cqName, ex);
                }
                LogWriterUtils.getLogWriter().info("initial result size = " + cqResults.size());
                assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
                if (expectedResultsSize >= 0) {
                    assertEquals("Unexpected results size for CQ: " + cqName + " CQ Query :" + cq1.getQueryString(), expectedResultsSize, cqResults.size());
                }
                if (expectedKeys != null) {
                    HashSet resultKeys = new HashSet();
                    for (Object o : cqResults.asList()) {
                        Struct s = (Struct) o;
                        resultKeys.add(s.get("key"));
                    }
                    for (int i = 0; i < expectedKeys.length; i++) {
                        assertTrue("Expected key :" + expectedKeys[i] + " Not found in CqResults for CQ: " + cqName + " CQ Query :" + cq1.getQueryString() + " Keys in CqResults :" + resultKeys, resultKeys.contains(expectedKeys[i]));
                    }
                }
            } else {
                try {
                    cq1.execute();
                } catch (Exception ex) {
                    if (expectedErr == null) {
                        LogWriterUtils.getLogWriter().info("CqService is :" + cqService, ex);
                    }
                    Assert.fail("Failed to execute  CQ " + cqName, ex);
                }
                assertTrue("execute() state mismatch", cq1.getState().isRunning());
            }
        }

        @Override
        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>");
                }
            }
        }
    });
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CqQuery(org.apache.geode.cache.query.CqQuery) CqExistsException(org.apache.geode.cache.query.CqExistsException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RMIException(org.apache.geode.test.dunit.RMIException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) HashSet(java.util.HashSet) Struct(org.apache.geode.cache.query.Struct)

Example 32 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class CqQueryUsingPoolDUnitTest method createCQ.

/* Register CQs */
public void createCQ(VM vm, final String poolName, final String cqName, final String queryStr) {
    vm.invoke(new CacheSerializableRunnable("Create CQ :" + cqName) {

        @Override
        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
            // Get CQ Service.
            QueryService qService = null;
            try {
                qService = (PoolManager.find(poolName)).getQueryService();
            } catch (Exception cqe) {
                Assert.fail("Failed to getCQService.", cqe);
            }
            // Create CQ Attributes.
            CqAttributesFactory cqf = new CqAttributesFactory();
            CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
            ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
            cqf.initCqListeners(cqListeners);
            CqAttributes cqa = cqf.create();
            // Create CQ.
            try {
                CqQuery cq1 = qService.newCq(cqName, queryStr, cqa);
                assertTrue("newCq() state mismatch", cq1.getState().isStopped());
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("QueryService is :" + qService, ex);
                Assert.fail("Failed to create CQ " + cqName + " . ", ex);
            }
        }
    });
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) CqExistsException(org.apache.geode.cache.query.CqExistsException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RMIException(org.apache.geode.test.dunit.RMIException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 33 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class DurableClientSimpleDUnitTest method testReadyForEventsNotCalledImplicitlyWithCacheXML.

/**
   * This test method is disabled because it is failing periodically and causing cruise control
   * failures See bug #47060 (test seems to be enabled now!)
   */
@Test
public void testReadyForEventsNotCalledImplicitlyWithCacheXML() {
    try {
        setPeriodicACKObserver(durableClientVM);
        final String cqName = "cqTest";
        // Start a server
        int serverPort = (Integer) this.server1VM.invoke(() -> CacheServerTestUtil.createCacheServerFromXml(DurableClientTestCase.class.getResource("durablecq-server-cache.xml")));
        // Start a durable client that is not kept alive on the server when it
        // stops normally
        final String durableClientId = getName() + "_client";
        // create client cache from xml
        this.durableClientVM.invoke(() -> CacheServerTestUtil.createCacheClientFromXml(DurableClientTestCase.class.getResource("durablecq-client-cache.xml"), "client", durableClientId, 45, Boolean.FALSE));
        // verify that readyForEvents has not yet been called on all the client's pools
        this.durableClientVM.invoke(new CacheSerializableRunnable("check readyForEvents not called") {

            @Override
            public void run2() throws CacheException {
                for (Pool p : PoolManager.getAll().values()) {
                    assertEquals(false, ((PoolImpl) p).getReadyForEventsCalled());
                }
            }
        });
        // Send clientReady message
        this.durableClientVM.invoke(new CacheSerializableRunnable("Send clientReady") {

            @Override
            public void run2() throws CacheException {
                CacheServerTestUtil.getCache().readyForEvents();
            }
        });
        registerDurableCq(cqName);
        // Verify durable client on server1
        this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {

            @Override
            public void run2() throws CacheException {
                // Find the proxy
                checkNumberOfClientProxies(1);
                CacheClientProxy proxy = getClientProxy();
                assertNotNull(proxy);
                // Verify that it is durable
                assertTrue(proxy.isDurable());
                assertEquals(durableClientId, proxy.getDurableId());
            }
        });
        // Start normal publisher client
        this.publisherClientVM.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(getServerHostName(publisherClientVM.getHost()), serverPort, false), regionName));
        // Publish some entries
        final int numberOfEntries = 10;
        publishEntries(numberOfEntries);
        // Verify the durable client received the updates
        this.durableClientVM.invoke(new CacheSerializableRunnable("Verify updates") {

            @Override
            public void run2() throws CacheException {
                // Get the region
                Region region = CacheServerTestUtil.getCache().getRegion(regionName);
                assertNotNull(region);
                // Get the listener and wait for the appropriate number of events
                QueryService queryService = CacheServerTestUtil.getPool().getQueryService();
                CqQuery cqQuery = queryService.getCq(cqName);
                CacheServerTestUtil.ControlCqListener cqlistener = (CacheServerTestUtil.ControlCqListener) cqQuery.getCqAttributes().getCqListener();
                cqlistener.waitWhileNotEnoughEvents(30000, numberOfEntries);
                assertEquals(numberOfEntries, cqlistener.events.size());
            }
        });
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            fail("interrupted", e);
        }
        // Stop the durable client
        this.durableClientVM.invoke(() -> CacheServerTestUtil.closeCache(new Boolean(true)));
        // Verify the durable client still exists on the server
        this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {

            @Override
            public void run2() throws CacheException {
                // Find the proxy
                CacheClientProxy proxy = getClientProxy();
                assertNotNull(proxy);
            }
        });
        // Publish some more entries
        publishEntries(numberOfEntries);
        this.publisherClientVM.invoke(() -> CacheServerTestUtil.closeCache());
        // Re-start the durable client
        this.durableClientVM.invoke(() -> CacheServerTestUtil.createCacheClientFromXml(DurableClientTestCase.class.getResource("durablecq-client-cache.xml"), "client", durableClientId, 45, Boolean.FALSE));
        // Durable client registers durable cq on server
        this.durableClientVM.invoke(new CacheSerializableRunnable("Register cq") {

            @Override
            public void run2() throws CacheException {
                // Get the region
                Region region = CacheServerTestUtil.getCache().getRegion(regionName);
                assertNotNull(region);
                // Create CQ Attributes.
                CqAttributesFactory cqAf = new CqAttributesFactory();
                // Initialize and set CqListener.
                CqListener[] cqListeners = { new CacheServerTestUtil.ControlCqListener() };
                cqAf.initCqListeners(cqListeners);
                CqAttributes cqa = cqAf.create();
                // Create cq's
                // Get the query service for the Pool
                QueryService queryService = CacheServerTestUtil.getPool().getQueryService();
                try {
                    CqQuery query = queryService.newCq(cqName, "Select * from /" + regionName, cqa, true);
                    query.execute();
                } catch (CqExistsException e) {
                    fail("Failed due to ", e);
                } catch (CqException e) {
                    fail("Failed due to ", e);
                } catch (RegionNotFoundException e) {
                    fail("Could not find specified region:" + regionName + ":", e);
                }
            }
        });
        // Send clientReady message
        this.durableClientVM.invoke(new CacheSerializableRunnable("Send clientReady") {

            @Override
            public void run2() throws CacheException {
                CacheServerTestUtil.getCache().readyForEvents();
            }
        });
        // Verify durable client on server
        this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {

            @Override
            public void run2() throws CacheException {
                // Find the proxy
                checkNumberOfClientProxies(1);
                CacheClientProxy proxy = getClientProxy();
                assertNotNull(proxy);
                // Verify that it is durable and its properties are correct
                assertTrue(proxy.isDurable());
                assertEquals(durableClientId, proxy.getDurableId());
            }
        });
        // Verify the durable client received the updates held for it on the server
        this.durableClientVM.invoke(new CacheSerializableRunnable("Verify updates") {

            @Override
            public void run2() throws CacheException {
                // Get the region
                Region region = CacheServerTestUtil.getCache().getRegion(regionName);
                assertNotNull(region);
                QueryService queryService = CacheServerTestUtil.getPool().getQueryService();
                CqQuery cqQuery = queryService.getCq(cqName);
                CacheServerTestUtil.ControlCqListener cqlistener = (CacheServerTestUtil.ControlCqListener) cqQuery.getCqAttributes().getCqListener();
                cqlistener.waitWhileNotEnoughEvents(30000, numberOfEntries);
                assertEquals(numberOfEntries, cqlistener.events.size());
            }
        });
        // Stop the durable client
        this.durableClientVM.invoke(() -> CacheServerTestUtil.closeCache());
        // Stop the server
        this.server1VM.invoke(() -> CacheServerTestUtil.closeCache());
    } finally {
        unsetPeriodicACKObserver(durableClientVM);
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) CqException(org.apache.geode.cache.query.CqException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqExistsException(org.apache.geode.cache.query.CqExistsException) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Pool(org.apache.geode.cache.client.Pool) CqQuery(org.apache.geode.cache.query.CqQuery) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 34 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class CQListGIIDUnitTest method waitForEvent.

public static void waitForEvent(int event, String cqName, String key) {
    // Get CQ Service.
    QueryService cqService = null;
    try {
        cqService = cache.getQueryService();
    } catch (Exception cqe) {
        Assert.fail("Failed to getCQService.", cqe);
    }
    CqQuery cQuery = cqService.getCq(cqName);
    if (cQuery == null) {
        Assert.fail("Failed to get CqQuery for CQ : " + cqName, new Exception("Failed to get CqQuery for CQ : " + cqName));
    }
    CqAttributes cqAttr = cQuery.getCqAttributes();
    CqListener[] cqListener = cqAttr.getCqListeners();
    CqQueryTestListener listener = (CqQueryTestListener) cqListener[0];
    switch(event) {
        case CREATE:
            listener.waitForCreated(key);
            break;
        case UPDATE:
            listener.waitForUpdated(key);
            break;
        case DESTROY:
            listener.waitForDestroyed(key);
            break;
        case INVALIDATE:
            listener.waitForInvalidated(key);
            break;
        case CLOSE:
            listener.waitForClose();
            break;
        case REGION_CLEAR:
            listener.waitForRegionClear();
            break;
        case REGION_INVALIDATE:
            listener.waitForRegionInvalidate();
            break;
    }
}
Also used : CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) CqQuery(org.apache.geode.cache.query.CqQuery) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 35 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class CQListGIIDUnitTest method executeCQ.

public static void executeCQ(String cqName, Boolean initialResults) {
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("### DEBUG EXECUTE CQ START ####");
    // Get CQ Service.
    QueryService cqService = null;
    CqQuery cq1 = null;
    cqService = cache.getQueryService();
    // Get CqQuery object.
    try {
        cq1 = cqService.getCq(cqName);
        if (cq1 == null) {
            org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Failed to get CqQuery object for CQ name: " + cqName);
            Assert.fail("Failed to get CQ " + cqName, new Exception("Failed to get CQ " + cqName));
        } else {
            org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Obtained CQ, CQ name: " + cq1.getName());
            assertTrue("newCq() state mismatch", cq1.getState().isStopped());
        }
    } catch (Exception ex) {
        fail("Failed to execute  CQ " + cqName, ex);
    }
    if (initialResults.booleanValue()) {
        SelectResults cqResults = null;
        try {
            cqResults = cq1.executeWithInitialResults();
        } catch (Exception ex) {
            fail("Failed to execute  CQ " + cqName, ex);
        }
        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("initial result size = " + cqResults.size());
        assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
    } else {
        try {
            cq1.execute();
        } catch (Exception ex) {
            fail("Failed to execute  CQ " + cqName, ex);
        }
        assertTrue("execute() state mismatch", cq1.getState().isRunning());
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CqQuery(org.apache.geode.cache.query.CqQuery) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Aggregations

CqQuery (org.apache.geode.cache.query.CqQuery)64 QueryService (org.apache.geode.cache.query.QueryService)49 CacheException (org.apache.geode.cache.CacheException)38 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)36 CqAttributes (org.apache.geode.cache.query.CqAttributes)35 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)31 IOException (java.io.IOException)27 CqExistsException (org.apache.geode.cache.query.CqExistsException)26 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)24 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)22 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)21 Test (org.junit.Test)21 CqClosedException (org.apache.geode.cache.query.CqClosedException)20 Region (org.apache.geode.cache.Region)17 SelectResults (org.apache.geode.cache.query.SelectResults)17 Host (org.apache.geode.test.dunit.Host)14 VM (org.apache.geode.test.dunit.VM)14 CqListener (org.apache.geode.cache.query.CqListener)12 Struct (org.apache.geode.cache.query.Struct)11 CqEvent (org.apache.geode.cache.query.CqEvent)10