Skip to content

Commit

Permalink
GEODE-4734: cleanup tests for use as examples in Geode wiki (apache#1518
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kirklund authored Mar 1, 2018
1 parent 25a0d6e commit ce0251d
Show file tree
Hide file tree
Showing 23 changed files with 863 additions and 872 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.cache;

import static org.apache.geode.cache.RegionShortcut.REPLICATE;
import static org.apache.geode.cache.RegionShortcut.REPLICATE_PROXY;
import static org.apache.geode.internal.i18n.LocalizedStrings.TXStateStub_LOCAL_DESTROY_NOT_ALLOWED_IN_TRANSACTION;
import static org.apache.geode.internal.i18n.LocalizedStrings.TXStateStub_LOCAL_INVALIDATE_NOT_ALLOWED_IN_TRANSACTION;
import static org.apache.geode.test.dunit.Host.getHost;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.Serializable;
import java.util.function.Consumer;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import junitparams.naming.TestCaseName;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;

import org.apache.geode.cache.Region.Entry;
import org.apache.geode.i18n.StringId;
import org.apache.geode.test.dunit.VM;
import org.apache.geode.test.dunit.rules.CacheRule;
import org.apache.geode.test.dunit.rules.DistributedTestRule;
import org.apache.geode.test.junit.categories.DistributedTest;
import org.apache.geode.test.junit.rules.serializable.SerializableErrorCollector;

/**
* Create and LocalDestroy/LocalInvalidate should create event with NewValue
*
* <p>
* TRAC #34387: TX in Proxy Regions with create followed by localDestroy on same key results in
* remote VMs receiving create events with null getNewValue().
*
* @since GemFire 5.0
*/
@Category(DistributedTest.class)
@RunWith(JUnitParamsRunner.class)
@SuppressWarnings("serial")
public class CreateAndLocalDestroyInTXRegressionTest implements Serializable {

private static final String REGION_NAME = "r1";
private static final String KEY = "createKey";
private static final String VALUE = "createValue";

private transient CacheListener<String, String> spyCacheListener;

private VM otherVM;

@ClassRule
public static DistributedTestRule distributedTestRule = new DistributedTestRule();

@Rule
public CacheRule cacheRule = new CacheRule();

@Rule
public SerializableErrorCollector errorCollector = new SerializableErrorCollector();

@Before
public void setUp() throws Exception {
otherVM = getHost(0).getVM(0);
spyCacheListener = mock(CacheListener.class);

otherVM.invoke(() -> {
RegionFactory<String, String> regionFactory =
cacheRule.getOrCreateCache().createRegionFactory(REPLICATE_PROXY);
regionFactory.create(REGION_NAME);
});

RegionFactory<String, String> regionFactory =
cacheRule.getOrCreateCache().createRegionFactory(REPLICATE);
regionFactory.addCacheListener(spyCacheListener);
regionFactory.create(REGION_NAME);
}

@Test
@Parameters({"LOCAL_DESTROY", "LOCAL_INVALIDATE"})
@TestCaseName("{method}({params})")
public void createAndLocalOpShouldCreateEventWithNewValue(final LocalOperation operation) {
otherVM.invoke(() -> {
CacheTransactionManager transactionManager =
cacheRule.getCache().getCacheTransactionManager();
Region<String, String> region = cacheRule.getCache().getRegion(REGION_NAME);

transactionManager.begin();
try {
region.create(KEY, VALUE);

assertThatThrownBy(() -> operation.invoke(region))
.isInstanceOf(UnsupportedOperationInTransactionException.class)
.hasMessage(operation.getMessage());
} finally {
transactionManager.commit();
}
});

Region<String, String> region = cacheRule.getCache().getRegion(REGION_NAME);
Entry<String, String> entry = region.getEntry(KEY);

assertThat(entry.getValue()).isEqualTo(VALUE);

ArgumentCaptor<EntryEvent<String, String>> argument = ArgumentCaptor.forClass(EntryEvent.class);
verify(spyCacheListener, times(1)).afterCreate(argument.capture());

EntryEvent<String, String> event = argument.getValue();
assertThat(event.getKey()).isEqualTo(KEY);
assertThat(event.getNewValue()).isEqualTo(VALUE);
}

private enum LocalOperation {
LOCAL_DESTROY((region) -> region.localDestroy(KEY),
TXStateStub_LOCAL_DESTROY_NOT_ALLOWED_IN_TRANSACTION),
LOCAL_INVALIDATE((region) -> region.localInvalidate(KEY),
TXStateStub_LOCAL_INVALIDATE_NOT_ALLOWED_IN_TRANSACTION);

private final Consumer<Region<String, String>> strategy;
private final StringId stringId;

LocalOperation(final Consumer<Region<String, String>> strategy, final StringId stringId) {
this.strategy = strategy;
this.stringId = stringId;
}

void invoke(final Region<String, String> region) {
strategy.accept(region);
}

String getMessage() {
return stringId.toLocalizedString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.internal.cache;
package org.apache.geode.cache;

import java.util.Arrays;

Expand All @@ -21,12 +21,8 @@
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;

import org.apache.geode.cache.CacheListener;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.PartitionAttributesFactory;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.test.junit.categories.DistributedTest;
import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory;

Expand All @@ -40,9 +36,9 @@
*/
@Category(DistributedTest.class)
@RunWith(Parameterized.class)
@Parameterized.UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
@UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
@SuppressWarnings("serial")
public class PRCacheListenerInvocationTest extends ReplicateCacheListenerInvocationTest {
public class PRCacheListenerDistributedTest extends ReplicateCacheListenerDistributedTest {

@Parameters(name = "{index}: redundancy={0}")
public static Iterable<Integer> data() {
Expand All @@ -55,13 +51,13 @@ public static Iterable<Integer> data() {
@Override
protected Region<String, Integer> createRegion(final String name,
final CacheListener<String, Integer> listener) {
PartitionAttributesFactory partitionFactory = new PartitionAttributesFactory();
partitionFactory.setRedundantCopies(redundancy);
PartitionAttributesFactory<String, Integer> paf = new PartitionAttributesFactory<>();
paf.setRedundantCopies(redundancy);

RegionFactory<String, Integer> regionFactory = cacheRule.getCache().createRegionFactory();
regionFactory.addCacheListener(listener);
regionFactory.setDataPolicy(DataPolicy.PARTITION);
regionFactory.setPartitionAttributes(partitionFactory.create());
regionFactory.setPartitionAttributes(paf.create());

return regionFactory.create(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.internal.cache;
package org.apache.geode.cache;

import java.util.Arrays;

Expand All @@ -21,19 +21,13 @@
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;

import org.apache.geode.cache.CacheListener;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.InterestPolicy;
import org.apache.geode.cache.PartitionAttributesFactory;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.cache.SubscriptionAttributes;
import org.apache.geode.test.junit.categories.DistributedTest;
import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory;

/**
* This class tests event triggering and handling in partitioned regions.
* Verifies behavior of CacheListener with InterestPolicy.ALL for Partitioned region.
*
* <p>
* Converted from JUnit 3.
Expand All @@ -42,10 +36,10 @@
*/
@Category(DistributedTest.class)
@RunWith(Parameterized.class)
@Parameterized.UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
@UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
@SuppressWarnings("serial")
public class PRCacheListenerWithInterestPolicyAllInvocationTest
extends ReplicateCacheListenerInvocationTest {
public class PRCacheListenerWithInterestPolicyAllDistributedTest
extends ReplicateCacheListenerDistributedTest {

@Parameters(name = "{index}: redundancy={0}")
public static Iterable<Integer> data() {
Expand All @@ -58,13 +52,13 @@ public static Iterable<Integer> data() {
@Override
protected Region<String, Integer> createRegion(final String name,
final CacheListener<String, Integer> listener) {
PartitionAttributesFactory partitionFactory = new PartitionAttributesFactory();
partitionFactory.setRedundantCopies(redundancy);
PartitionAttributesFactory<String, Integer> paf = new PartitionAttributesFactory<>();
paf.setRedundantCopies(redundancy);

RegionFactory<String, Integer> regionFactory = cacheRule.getCache().createRegionFactory();
regionFactory.addCacheListener(listener);
regionFactory.setDataPolicy(DataPolicy.PARTITION);
regionFactory.setPartitionAttributes(partitionFactory.create());
regionFactory.setPartitionAttributes(paf.create());
regionFactory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));

return regionFactory.create(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.internal.cache;
package org.apache.geode.cache;

import static org.apache.geode.cache.PartitionAttributesFactory.GLOBAL_MAX_BUCKETS_DEFAULT;
import static org.apache.geode.cache.PartitionAttributesFactory.GLOBAL_MAX_BUCKETS_PROPERTY;
Expand All @@ -31,16 +31,15 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import org.apache.geode.cache.Cache;
import org.apache.geode.cache.PartitionAttributesFactory;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.internal.cache.PartitionedRegion;
import org.apache.geode.test.dunit.IgnoredException;
import org.apache.geode.test.dunit.VM;
import org.apache.geode.test.dunit.rules.CacheRule;
import org.apache.geode.test.dunit.rules.DistributedTestRule;
import org.apache.geode.test.junit.categories.DistributedTest;

@Category(DistributedTest.class)
@SuppressWarnings("serial")
public class PRCreationTotalNumBucketsDistributedTest implements Serializable {

private VM vm0;
Expand All @@ -64,24 +63,24 @@ public void setUp() throws Exception {
@Test
public void testSetTotalNumBuckets() throws Exception {
vm0.invoke(() -> {
Cache cache = cacheRule.createCache();
Cache cache = cacheRule.getOrCreateCache();

RegionFactory regionFactory = cache.createRegionFactory(PARTITION);
RegionFactory<String, String> regionFactory = cache.createRegionFactory(PARTITION);
regionFactory.create("PR1");

PartitionAttributesFactory partitionAttributesFactory = new PartitionAttributesFactory();
partitionAttributesFactory.setTotalNumBuckets(totalNumBuckets);
regionFactory.setPartitionAttributes(partitionAttributesFactory.create());
PartitionAttributesFactory<String, String> paf = new PartitionAttributesFactory<>();
paf.setTotalNumBuckets(totalNumBuckets);
regionFactory.setPartitionAttributes(paf.create());
regionFactory.create("PR2");
});

vm1.invoke(() -> {
Cache cache = cacheRule.createCache();
Cache cache = cacheRule.getOrCreateCache();

PartitionAttributesFactory partitionAttributesFactory = new PartitionAttributesFactory();
partitionAttributesFactory.setLocalMaxMemory(0);
RegionFactory regionFactory = cache.createRegionFactory(PARTITION);
regionFactory.setPartitionAttributes(partitionAttributesFactory.create());
PartitionAttributesFactory<String, String> paf = new PartitionAttributesFactory<>();
paf.setLocalMaxMemory(0);
RegionFactory<String, String> regionFactory = cache.createRegionFactory(PARTITION);
regionFactory.setPartitionAttributes(paf.create());
PartitionedRegion accessor = (PartitionedRegion) regionFactory.create("PR1");

assertThat(accessor.getTotalNumberOfBuckets()).isEqualTo(GLOBAL_MAX_BUCKETS_DEFAULT);
Expand All @@ -91,32 +90,35 @@ public void testSetTotalNumBuckets() throws Exception {
.isInstanceOf(IllegalStateException.class);
}

partitionAttributesFactory.setTotalNumBuckets(totalNumBuckets);
regionFactory.setPartitionAttributes(partitionAttributesFactory.create());
paf.setTotalNumBuckets(totalNumBuckets);
regionFactory.setPartitionAttributes(paf.create());
accessor = (PartitionedRegion) regionFactory.create("PR2");

assertThat(accessor.getTotalNumberOfBuckets()).isEqualTo(totalNumBuckets);
});
}

/**
* Tests {@link PartitionAttributesFactory#setGlobalProperties(Properties)} which is deprecated.
*/
@Test
public void testSetGlobalProperties() throws Exception {
vm0.invoke(() -> {
Cache cache = cacheRule.createCache();
Cache cache = cacheRule.getOrCreateCache();

RegionFactory regionFactory = cache.createRegionFactory(PARTITION);
PartitionAttributesFactory partitionAttributesFactory = new PartitionAttributesFactory();
partitionAttributesFactory.setTotalNumBuckets(totalNumBuckets);
regionFactory.setPartitionAttributes(partitionAttributesFactory.create());
RegionFactory<String, String> regionFactory = cache.createRegionFactory(PARTITION);
PartitionAttributesFactory<String, String> paf = new PartitionAttributesFactory<>();
paf.setTotalNumBuckets(totalNumBuckets);
regionFactory.setPartitionAttributes(paf.create());
regionFactory.create("PR1");
});

vm1.invoke(() -> {
Cache cache = cacheRule.createCache();
Cache cache = cacheRule.getOrCreateCache();

RegionFactory regionFactory = cache.createRegionFactory(PARTITION);
PartitionAttributesFactory partitionAttributesFactory = new PartitionAttributesFactory();
regionFactory.setPartitionAttributes(partitionAttributesFactory.create());
RegionFactory<String, String> regionFactory = cache.createRegionFactory(PARTITION);
PartitionAttributesFactory<String, String> paf = new PartitionAttributesFactory<>();
regionFactory.setPartitionAttributes(paf.create());

try (IgnoredException ie = addIgnoredException(IllegalStateException.class)) {
assertThatThrownBy(() -> regionFactory.create("PR1"))
Expand All @@ -125,8 +127,8 @@ public void testSetGlobalProperties() throws Exception {

Properties globalProperties = new Properties();
globalProperties.setProperty(GLOBAL_MAX_BUCKETS_PROPERTY, "" + totalNumBuckets);
partitionAttributesFactory.setGlobalProperties(globalProperties);
regionFactory.setPartitionAttributes(partitionAttributesFactory.create());
paf.setGlobalProperties(globalProperties);
regionFactory.setPartitionAttributes(paf.create());
PartitionedRegion accessor = (PartitionedRegion) regionFactory.create("PR1");

assertThat(accessor.getTotalNumberOfBuckets()).isEqualTo(totalNumBuckets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class RegionExpirationIntegrationTest {

@Before
public void setUp() {
regionName = getClass().getSimpleName() + "_" + testName.getMethodName();
regionName = testName.getMethodName() + "_Region";
spyCacheListener = mock(CacheListener.class);

cache = new CacheFactory().set(LOCATORS, "").set(MCAST_PORT, "0").create();
Expand Down
Loading

0 comments on commit ce0251d

Please sign in to comment.