Search in sources :

Example 6 with Struct

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

the class PdxQueryCQDUnitTest method testCqAndInterestRegistrationsWithFailOver.

/**
   * Tests client-server query on PdxInstance.
   */
@Test
public void testCqAndInterestRegistrationsWithFailOver() throws CacheException {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    // where id > 5 (0-5)
    final int queryLimit = 6;
    final String[] queries = new String[] { "SELECT * FROM " + regName + " p WHERE p.ticker = 'vmware'", "SELECT * FROM " + regName + " WHERE id > 5" };
    // Start server1
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Start server2
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Start server3
    vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Client pool.
    final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port2 = vm2.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testCqPool";
    createPool(vm3, poolName, new String[] { host0, host0, host0 }, new int[] { port1, port0, port2 }, true, 1);
    final String cqName = "testCq";
    vm3.invoke(new CacheSerializableRunnable("init region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    SerializableRunnable subscribe = new CacheSerializableRunnable("subscribe") {

        public void run2() throws CacheException {
            // Register interest
            Region region = getRootRegion().getSubregion(regionName);
            List list = new ArrayList();
            for (int i = 1; i <= numberOfEntries * 3; i++) {
                if (i % 4 == 0) {
                    list.add("key-" + i);
                }
            }
            region.registerInterest(list);
            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.
            for (int i = 0; i < queries.length; i++) {
                CqAttributesFactory cqf = new CqAttributesFactory();
                CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
                ((CqQueryTestListener) cqListeners[0]).cqName = (cqName + i);
                cqf.initCqListeners(cqListeners);
                CqAttributes cqa = cqf.create();
                // Create CQ.
                try {
                    CqQuery cq = qService.newCq(cqName + i, queries[i], cqa);
                    SelectResults sr = cq.executeWithInitialResults();
                    for (Object o : sr.asSet()) {
                        Struct s = (Struct) o;
                        Object value = s.get("value");
                        if (!(value instanceof TestObject)) {
                            fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
                        }
                    }
                } catch (Exception ex) {
                    AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                    err.initCause(ex);
                    LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
                    throw err;
                }
            }
        }
    };
    vm3.invoke(subscribe);
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    // update
    vm3.invoke(new CacheSerializableRunnable("Update") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Validate CQs.
    for (int i = 0; i < queries.length; i++) {
        int expectedEvent = 0;
        int updateEvents = 0;
        if (i != 0) {
            expectedEvent = (numberOfEntries * 2) - queryLimit;
            updateEvents = numberOfEntries - queryLimit;
        } else {
            expectedEvent = numberOfEntries * 2;
            updateEvents = numberOfEntries;
        }
        validateCq(vm3, cqName + i, expectedEvent, numberOfEntries, updateEvents);
    }
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    // Update
    vm3.invokeAsync(new CacheSerializableRunnable("Update") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Kill server
    this.closeClient(vm0);
    // validate cq
    for (int i = 0; i < queries.length; i++) {
        int expectedEvent = 0;
        int updateEvents = 0;
        if (i != 0) {
            // Double the previous time
            expectedEvent = (numberOfEntries * 4) - (queryLimit * 2);
            updateEvents = (numberOfEntries * 3) - (queryLimit * 2);
        } else {
            expectedEvent = numberOfEntries * 4;
            updateEvents = numberOfEntries * 3;
        }
        validateCq(vm3, cqName + i, expectedEvent, numberOfEntries, updateEvents);
    }
    this.closeClient(vm1);
    // Check for TestObject instances on Server3.
    // It should be 0
    vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    this.closeClient(vm2);
    this.closeClient(vm3);
}
Also used : CacheException(org.apache.geode.cache.CacheException) ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) ArrayList(java.util.ArrayList) List(java.util.List) CqQuery(org.apache.geode.cache.query.CqQuery) CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) CqListener(org.apache.geode.cache.query.CqListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 7 with Struct

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

the class PdxQueryCQDUnitTest method testCqAndInterestRegistrations.

/**
   * Tests client-server query on PdxInstance.
   */
@Test
public void testCqAndInterestRegistrations() throws CacheException {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    // where id > 5 (0-5)
    final int queryLimit = 6;
    final String[] queries = new String[] { "SELECT * FROM " + regName + " p WHERE p.ticker = 'vmware'", "SELECT * FROM " + regName + " WHERE id > 5" };
    // Start server1
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Start server2
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Client pool.
    final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testCqPool";
    createPool(vm2, poolName, new String[] { host0, host0 }, new int[] { port0, port1 }, true);
    createPool(vm3, poolName, new String[] { host0, host0 }, new int[] { port1, port0 }, true);
    final String cqName = "testCq";
    vm3.invoke(new CacheSerializableRunnable("init region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    vm2.invoke(new CacheSerializableRunnable("init region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port0, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
        }
    });
    SerializableRunnable subscribe = new CacheSerializableRunnable("subscribe") {

        public void run2() throws CacheException {
            // Register interest
            Region region = getRootRegion().getSubregion(regionName);
            List list = new ArrayList();
            for (int i = 1; i <= numberOfEntries * 3; i++) {
                if (i % 4 == 0) {
                    list.add("key-" + i);
                }
            }
            region.registerInterest(list);
            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.
            for (int i = 0; i < queries.length; i++) {
                CqAttributesFactory cqf = new CqAttributesFactory();
                CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
                ((CqQueryTestListener) cqListeners[0]).cqName = (cqName + i);
                cqf.initCqListeners(cqListeners);
                CqAttributes cqa = cqf.create();
                // Create CQ.
                try {
                    CqQuery cq = qService.newCq(cqName + i, queries[i], cqa);
                    SelectResults sr = cq.executeWithInitialResults();
                    for (Object o : sr.asSet()) {
                        Struct s = (Struct) o;
                        Object value = s.get("value");
                        if (!(value instanceof TestObject)) {
                            fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
                        }
                    }
                } catch (Exception ex) {
                    AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                    err.initCause(ex);
                    LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
                    throw err;
                }
            }
        }
    };
    vm2.invoke(subscribe);
    vm3.invoke(subscribe);
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm3.invoke(new CacheSerializableRunnable("Update") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Validate CQs.
    for (int i = 0; i < queries.length; i++) {
        int expectedEvent = 0;
        int updateEvents = 0;
        if (i != 0) {
            expectedEvent = numberOfEntries * 2 - queryLimit;
            updateEvents = numberOfEntries - queryLimit;
        } else {
            expectedEvent = numberOfEntries * 2;
            updateEvents = numberOfEntries;
        }
        validateCq(vm2, cqName + i, expectedEvent, numberOfEntries, updateEvents);
        validateCq(vm3, cqName + i, expectedEvent, numberOfEntries, updateEvents);
    }
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    this.closeClient(vm2);
    this.closeClient(vm3);
    this.closeClient(vm1);
    this.closeClient(vm0);
}
Also used : CacheException(org.apache.geode.cache.CacheException) ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) ArrayList(java.util.ArrayList) List(java.util.List) CqQuery(org.apache.geode.cache.query.CqQuery) CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) CqListener(org.apache.geode.cache.query.CqListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with Struct

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

the class PdxQueryCQTestBase method printResults.

public void printResults(SelectResults results, String message) {
    Object r;
    Struct s;
    LogWriterI18n logger = GemFireCacheImpl.getInstance().getLoggerI18n();
    logger.fine(message);
    int row = 0;
    for (Iterator iter = results.iterator(); iter.hasNext(); ) {
        r = iter.next();
        row++;
        if (r instanceof Struct) {
            s = (Struct) r;
            String[] fieldNames = ((Struct) r).getStructType().getFieldNames();
            for (int i = 0; i < fieldNames.length; i++) {
                logger.fine("### Row " + row + "\n" + "Field: " + fieldNames[i] + " > " + s.get(fieldNames[i]).toString());
            }
        } else {
            logger.fine("#### Row " + row + "\n" + r);
        }
    }
}
Also used : Iterator(java.util.Iterator) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) Struct(org.apache.geode.cache.query.Struct)

Example 9 with Struct

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

the class PrCqUsingPoolDUnitTest method testEventsDuringQueryExecution.

/**
   * Test for events created during the CQ query execution. When CQs are executed using
   * executeWithInitialResults there may be possibility that the region changes during that time may
   * not be reflected in the query result set thus making the query data and region data
   * inconsistent.
   * 
   * @throws Exception
   */
// GEODE-1181, 1253: random ports, eats exceptions (fixed some), async
@Category(FlakyTest.class)
// behavior
@Test
public void testEventsDuringQueryExecution() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    final String cqName = "testEventsDuringQueryExecution_0";
    // Server.
    createServer(server1);
    createServer(server2);
    final int port = server1.invoke(() -> PrCqUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    String poolName1 = "testEventsDuringQueryExecution";
    createPool(client, poolName1, host0, port);
    // create CQ.
    createCQ(client, poolName1, cqName, cqs[0]);
    final int numObjects = 200;
    final int totalObjects = 500;
    // initialize Region.
    server1.invoke(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + regions[0]);
            for (int i = 1; i <= numObjects; i++) {
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
        }
    });
    // Keep updating region (async invocation).
    server1.invokeAsync(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + regions[0]);
            for (int i = numObjects + 1; i <= totalObjects; i++) {
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
        }
    });
    // Execute CQ while update is in progress.
    client.invoke(new CacheSerializableRunnable("Execute CQ") {

        public void run2() throws CacheException {
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            SelectResults cqResults = null;
            try {
                cqResults = cq1.executeWithInitialResults();
            } catch (Exception ex) {
                throw new AssertionError("Failed to execute  CQ " + cqName, ex);
            }
            // getLogWriter().info("initial result size = " + cqResults.size());
            CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
            // Wait for the last key to arrive.
            for (int i = 0; i < 4; i++) {
                try {
                    cqListener.waitForCreated("" + totalObjects);
                    // Found skip from the loop.
                    break;
                } catch (CacheException ex) {
                    if (i == 3) {
                        throw ex;
                    }
                }
            }
            // Check if the events from CqListener are in order.
            int oldId = 0;
            for (Object cqEvent : cqListener.events.toArray()) {
                int newId = new Integer(cqEvent.toString()).intValue();
                if (oldId > newId) {
                    fail("Queued events for CQ Listener during execution with " + "Initial results is not in the order in which they are created.");
                }
                oldId = newId;
            }
            // Check if all the IDs are present as part of Select Results and CQ Events.
            HashSet ids = new HashSet(cqListener.events);
            for (Object o : cqResults.asList()) {
                Struct s = (Struct) o;
                ids.add(s.get("key"));
            }
            // Iterator iter = cqResults.asSet().iterator();
            // while (iter.hasNext()) {
            // Portfolio p = (Portfolio)iter.next();
            // ids.add(p.getPk());
            // //getLogWriter().info("Result set value : " + p.getPk());
            // }
            HashSet missingIds = new HashSet();
            String key = "";
            for (int i = 1; i <= totalObjects; i++) {
                key = "" + i;
                if (!(ids.contains(key))) {
                    missingIds.add(key);
                }
            }
            if (!missingIds.isEmpty()) {
                fail("Missing Keys in either ResultSet or the Cq Event list. " + " Missing keys : [size : " + missingIds.size() + "]" + missingIds + " Ids in ResultSet and CQ Events :" + ids);
            }
        }
    });
    cqHelper.closeClient(client);
    cqHelper.closeServer(server2);
    cqHelper.closeServer(server1);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqQuery(org.apache.geode.cache.query.CqQuery) HashSet(java.util.HashSet) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 10 with Struct

use of org.apache.geode.cache.query.Struct 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)

Aggregations

Struct (org.apache.geode.cache.query.Struct)115 SelectResults (org.apache.geode.cache.query.SelectResults)81 QueryService (org.apache.geode.cache.query.QueryService)70 Test (org.junit.Test)68 Region (org.apache.geode.cache.Region)67 Query (org.apache.geode.cache.query.Query)57 Iterator (java.util.Iterator)54 Portfolio (org.apache.geode.cache.query.data.Portfolio)46 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)30 ObjectType (org.apache.geode.cache.query.types.ObjectType)30 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)27 StructType (org.apache.geode.cache.query.types.StructType)26 HashSet (java.util.HashSet)19 List (java.util.List)17 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)17 CacheException (org.apache.geode.cache.CacheException)16 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)16 ArrayList (java.util.ArrayList)15 Host (org.apache.geode.test.dunit.Host)14 VM (org.apache.geode.test.dunit.VM)14