Search in sources :

Example 1 with SerializableRunnableIF

use of org.apache.geode.test.dunit.SerializableRunnableIF in project geode by apache.

the class PartitionedRegionQueryDUnitTest method createIndexDoesNotDerializePdxObjects.

private void createIndexDoesNotDerializePdxObjects(final SerializableRunnableIF createIndex, final String queryString, PdxAssetFactory valueSupplier) {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    SerializableRunnableIF createPR = () -> {
        Cache cache = getCache();
        PartitionAttributesFactory paf = new PartitionAttributesFactory();
        paf.setTotalNumBuckets(10);
        cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(paf.create()).create("region");
    };
    vm0.invoke(createPR);
    vm1.invoke(createPR);
    // Do Puts. These objects can't be deserialized because they throw
    // and exception from the constructor
    vm0.invoke(() -> {
        Cache cache = getCache();
        Region region = cache.getRegion("region");
        region.put(0, new PdxNotDeserializableAsset(0, "B"));
        region.put(10, new PdxNotDeserializableAsset(1, "B"));
        region.put(1, new PdxNotDeserializableAsset(1, "B"));
        IntStream.range(11, 100).forEach(i -> region.put(i, valueSupplier.getAsset(i)));
    });
    // If this tries to deserialize the assets, it will fail
    vm0.invoke(createIndex);
    vm0.invoke(() -> {
        QueryService qs = getCache().getQueryService();
        SelectResults<Struct> results = (SelectResults) qs.newQuery(queryString).execute();
        assertEquals(3, results.size());
        final Index index = qs.getIndex(getCache().getRegion("region"), "ContractDocumentIndex");
        assertEquals(1, index.getStatistics().getTotalUses());
    });
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Struct(org.apache.geode.cache.query.Struct)

Example 2 with SerializableRunnableIF

use of org.apache.geode.test.dunit.SerializableRunnableIF in project geode by apache.

the class PartitionedRegionQueryDUnitTest method testHashIndexDoesNotDeserializePdxObjects.

@Test
public void testHashIndexDoesNotDeserializePdxObjects() {
    SerializableRunnableIF createIndex = () -> {
        Cache cache = getCache();
        cache.getQueryService().createHashIndex("ContractDocumentIndex", "document", "/region");
    };
    String queryString = "select assetId,document from /region where document='B' limit 1000";
    PdxAssetFactory value = i -> new PdxNotDeserializableAsset(i, Integer.toString(i));
    createIndexDoesNotDerializePdxObjects(createIndex, queryString, value);
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Arrays(java.util.Arrays) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) DUnitEnv(org.apache.geode.test.dunit.DUnitEnv) JSONFormatter(org.apache.geode.pdx.JSONFormatter) Assert(org.apache.geode.test.dunit.Assert) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) Cache(org.apache.geode.cache.Cache) RebalanceResults(org.apache.geode.cache.control.RebalanceResults) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) CacheServer(org.apache.geode.cache.server.CacheServer) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) CancellationException(java.util.concurrent.CancellationException) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) Collection(java.util.Collection) DataSerializable(org.apache.geode.DataSerializable) ClientRegionShortcut(org.apache.geode.cache.client.ClientRegionShortcut) Category(org.junit.experimental.categories.Category) Serializable(java.io.Serializable) IndexManager(org.apache.geode.cache.query.internal.index.IndexManager) List(java.util.List) DataInput(java.io.DataInput) ClientCache(org.apache.geode.cache.client.ClientCache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IntStream(java.util.stream.IntStream) Struct(org.apache.geode.cache.query.Struct) Index(org.apache.geode.cache.query.Index) Query(org.apache.geode.cache.query.Query) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) HashMap(java.util.HashMap) Host(org.apache.geode.test.dunit.Host) VM(org.apache.geode.test.dunit.VM) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) PdxWriter(org.apache.geode.pdx.PdxWriter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LogWriterUtils(org.apache.geode.test.dunit.LogWriterUtils) QueryMessage(org.apache.geode.internal.cache.partitioned.QueryMessage) PdxSerializable(org.apache.geode.pdx.PdxSerializable) Region(org.apache.geode.cache.Region) NetworkUtils(org.apache.geode.test.dunit.NetworkUtils) DataOutput(java.io.DataOutput) Iterator(java.util.Iterator) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex) AvailablePortHelper(org.apache.geode.internal.AvailablePortHelper) RegionShortcut(org.apache.geode.cache.RegionShortcut) Test(org.junit.Test) IOException(java.io.IOException) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) QueryService(org.apache.geode.cache.query.QueryService) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) SelectResults(org.apache.geode.cache.query.SelectResults) JUnit4CacheTestCase(org.apache.geode.test.dunit.cache.internal.JUnit4CacheTestCase) PdxReader(org.apache.geode.pdx.PdxReader) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) Assert(org.junit.Assert) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 3 with SerializableRunnableIF

use of org.apache.geode.test.dunit.SerializableRunnableIF in project geode by apache.

the class RegionReliabilityTestCase method testCommitDistributionException.

@Test
public void testCommitDistributionException() throws Exception {
    if (getRegionScope().isGlobal())
        // skip test under Global
        return;
    if (getRegionScope().isDistributedNoAck())
        // skip test under DistributedNoAck
        return;
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {

        public void afterRemoteRegionDeparture(RegionEvent event) {
            synchronized (detectedDeparture_testCommitDistributionException) {
                detectedDeparture_testCommitDistributionException[0] = Boolean.TRUE;
                detectedDeparture_testCommitDistributionException.notify();
            }
        }
    };
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.addCacheListener(listener);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    // use vm1 to create role
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    // define the afterReleaseLocalLocks callback
    SerializableRunnableIF removeRequiredRole = new SerializableRunnableIF() {

        public void run() {
            Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {

                public void run() {
                    getRootRegion(name).close();
                }
            });
            try {
                synchronized (detectedDeparture_testCommitDistributionException) {
                    while (detectedDeparture_testCommitDistributionException[0] == Boolean.FALSE) {
                        detectedDeparture_testCommitDistributionException.wait();
                    }
                }
            } catch (InterruptedException e) {
                fail("interrupted");
            }
        }
    };
    // define the add and remove expected exceptions
    final String expectedExceptions = "org.apache.geode.internal.cache.CommitReplyException";
    SerializableRunnable addExpectedExceptions = new CacheSerializableRunnable("addExpectedExceptions") {

        public void run2() throws CacheException {
            getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
        }
    };
    SerializableRunnable removeExpectedExceptions = new CacheSerializableRunnable("removeExpectedExceptions") {

        public void run2() throws CacheException {
            getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
        }
    };
    // perform the actual test...
    CacheTransactionManager ctm = cache.getCacheTransactionManager();
    ctm.begin();
    TXStateInterface txStateProxy = ((TXManagerImpl) ctm).getTXState();
    ((TXStateProxyImpl) txStateProxy).forceLocalBootstrap();
    TXState txState = (TXState) ((TXStateProxyImpl) txStateProxy).getRealDeal(null, null);
    txState.setBeforeSend(() -> {
        try {
            removeRequiredRole.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    // now start a transaction and commit it
    region.put("KEY", "VAL");
    addExpectedExceptions.run();
    Host.getHost(0).getVM(1).invoke(addExpectedExceptions);
    try {
        ctm.commit();
        fail("Should have thrown CommitDistributionException");
    } catch (CommitDistributionException e) {
    // pass
    } finally {
        removeExpectedExceptions.run();
        Host.getHost(0).getVM(1).invoke(removeExpectedExceptions);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) TXStateProxyImpl(org.apache.geode.internal.cache.TXStateProxyImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TXState(org.apache.geode.internal.cache.TXState) AttributesFactory(org.apache.geode.cache.AttributesFactory) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) HashSet(java.util.HashSet) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) TXStateInterface(org.apache.geode.internal.cache.TXStateInterface) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) RegionAccessException(org.apache.geode.cache.RegionAccessException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionMembershipListenerAdapter(org.apache.geode.cache.util.RegionMembershipListenerAdapter) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 4 with SerializableRunnableIF

use of org.apache.geode.test.dunit.SerializableRunnableIF in project geode by apache.

the class MixedObjectIndexDUnitTest method luceneMustIndexFieldsWithMixedObjects.

@Test
@Parameters(method = "getPartitionRegionTypes")
public void luceneMustIndexFieldsWithMixedObjects(RegionTestableType regionTestableType) {
    SerializableRunnableIF createIndexOnTextAndDataField = getSerializableRunnableIFCreateIndexOnFieldData();
    dataStore1.invoke(() -> initDataStore(createIndexOnTextAndDataField, regionTestableType));
    dataStore2.invoke(() -> initDataStore(createIndexOnTextAndDataField, regionTestableType));
    accessor.invoke(() -> initDataStore(createIndexOnTextAndDataField, regionTestableType));
    accessor.invoke(() -> {
        Region region = getCache().getRegion(REGION_NAME);
        IntStream.range(0, NUM_BUCKETS).forEach(i -> region.put(i, new TestObject("hello world")));
        IntStream.range(NUM_BUCKETS, 2 * NUM_BUCKETS).forEach(i -> region.put(i, new TestObjectWithSameFieldName("hello world")));
        IntStream.range(2 * NUM_BUCKETS, 3 * NUM_BUCKETS).forEach(i -> region.put(i, new TestObjectWithNoCommonField("hello world")));
    });
    waitForFlushBeforeExecuteTextSearch(accessor, 60000);
    accessor.invoke(() -> {
        LuceneService luceneService = LuceneServiceProvider.get(getCache());
        LuceneQuery luceneQueryForTextField = luceneService.createLuceneQueryFactory().setLimit(100).create(INDEX_NAME, REGION_NAME, "world", "text");
        List luceneResults = luceneQueryForTextField.findResults();
        validateObjectResultCounts(luceneResults, TestObject.class, NUM_BUCKETS, TestObjectWithSameFieldName.class, NUM_BUCKETS, TestObjectWithNoCommonField.class, 0);
        luceneQueryForTextField = luceneService.createLuceneQueryFactory().setLimit(100).create(INDEX_NAME, REGION_NAME, "world", "data");
        luceneResults = luceneQueryForTextField.findResults();
        validateObjectResultCounts(luceneResults, TestObject.class, 0, TestObjectWithSameFieldName.class, 0, TestObjectWithNoCommonField.class, NUM_BUCKETS);
    });
}
Also used : Region(org.apache.geode.cache.Region) List(java.util.List) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) Parameters(junitparams.Parameters) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with SerializableRunnableIF

use of org.apache.geode.test.dunit.SerializableRunnableIF in project geode by apache.

the class MixedObjectIndexDUnitTest method luceneCanIndexFieldsWithSameNameButInDifferentObjects.

@Test
@Parameters(method = "getPartitionRegionTypes")
public void luceneCanIndexFieldsWithSameNameButInDifferentObjects(RegionTestableType regionTestType) {
    SerializableRunnableIF createIndex = getSerializableRunnableIFCreateIndexOnFieldText();
    dataStore1.invoke(() -> initDataStore(createIndex, regionTestType));
    dataStore2.invoke(() -> initDataStore(createIndex, regionTestType));
    accessor.invoke(() -> initDataStore(createIndex, regionTestType));
    accessor.invoke(() -> {
        Region region = getCache().getRegion(REGION_NAME);
        IntStream.range(0, NUM_BUCKETS).forEach(i -> region.put(i, new TestObject("hello world")));
        IntStream.range(NUM_BUCKETS, (2 * NUM_BUCKETS)).forEach(i -> region.put(i, new TestObjectWithSameFieldName("hello world 2")));
    });
    waitForFlushBeforeExecuteTextSearch(accessor, 60000);
    accessor.invoke(() -> {
        LuceneService luceneService = LuceneServiceProvider.get(getCache());
        LuceneQuery luceneQuery = luceneService.createLuceneQueryFactory().setLimit(100).create(INDEX_NAME, REGION_NAME, "world", "text");
        List resultList = luceneQuery.findResults();
        int objectType_1_count = 0;
        int objectType_2_count = 0;
        for (Object luceneResultStruct : resultList) {
            if (((LuceneResultStruct) luceneResultStruct).getValue() instanceof TestObject) {
                objectType_1_count++;
            } else if (((LuceneResultStruct) luceneResultStruct).getValue() instanceof TestObjectWithSameFieldName) {
                objectType_2_count++;
            }
        }
        assertEquals(NUM_BUCKETS, objectType_1_count);
        assertEquals(NUM_BUCKETS, objectType_2_count);
    });
}
Also used : Region(org.apache.geode.cache.Region) List(java.util.List) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) Parameters(junitparams.Parameters) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

SerializableRunnableIF (org.apache.geode.test.dunit.SerializableRunnableIF)49 Test (org.junit.Test)45 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)44 Parameters (junitparams.Parameters)39 Cache (org.apache.geode.cache.Cache)12 Region (org.apache.geode.cache.Region)12 List (java.util.List)7 ClientCache (org.apache.geode.cache.client.ClientCache)6 Host (org.apache.geode.test.dunit.Host)6 VM (org.apache.geode.test.dunit.VM)6 ArrayList (java.util.ArrayList)5 CacheException (org.apache.geode.cache.CacheException)5 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)5 AbstractIndex (org.apache.geode.cache.query.internal.index.AbstractIndex)5 IOException (java.io.IOException)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 CancellationException (java.util.concurrent.CancellationException)4 DataInput (java.io.DataInput)3 DataOutput (java.io.DataOutput)3