Skip to content

Commit

Permalink
GEODE-4456: Remove singleton calls from all tests in org.apache.geode…
Browse files Browse the repository at this point in the history
….internal.cache

* removed InternalDistributedSystem.getConnectedInstance call from TombstoneCreationJUnitTest
* Also the cache is now created in a @before method and uses non-deprecated methods to create it.
* removed InternalDistributedSystem.getConnectedInstance call from ClientServerTransactionDUnitTest
* removed getAnyInstance calls in GridAdvisorDUnitTest
  • Loading branch information
dschneider-pivotal authored and PurelyApplied committed Apr 4, 2018
1 parent 93a484d commit da1ce1c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,9 @@ public void testTwoPoolsNotAllowed() {
exceptionThrown = true;
}

SerializableCallable disconnect = new SerializableCallable("disconnect") {
public Object call() throws Exception {
InternalDistributedSystem.getConnectedInstance().disconnect();
return null;
}
};

cCache.close();
datastore1.invoke(disconnect);
datastore2.invoke(disconnect);
datastore1.invoke(() -> closeCache());
datastore2.invoke(() -> closeCache());

if (!exceptionThrown) {
fail("expected TransactionException to be thrown since two pools were used");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.server.CacheServer;
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.Locator;
import org.apache.geode.distributed.internal.DistributionAdvisee;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.distributed.internal.InternalLocator;
import org.apache.geode.internal.AvailablePort.Keeper;
import org.apache.geode.internal.AvailablePortHelper;
Expand All @@ -53,6 +51,19 @@
@Category(DistributedTest.class)
public class GridAdvisorDUnitTest extends JUnit4DistributedTestCase {

private static InternalCache cache;

private void createCache(String locators, String groups) {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, locators);
if (groups != null) {
props.setProperty(GROUPS, groups);
}
props.setProperty(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
cache = (InternalCache) new CacheFactory(props).create();
}

/**
* Tests 2 controllers and 2 bridge servers
*/
Expand Down Expand Up @@ -118,19 +129,15 @@ public void run() {

SerializableRunnable connect = new SerializableRunnable("Connect to " + locators) {
public void run() {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, locators);
dsProps.setProperty(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
CacheFactory.create(DistributedSystem.connect(props));
createCache(locators, null);
}
};
vm1.invoke(connect);
vm2.invoke(connect);
SerializableRunnable startBS1 = new SerializableRunnable("start bridgeServer on " + bsPort1) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort1);
bs.setGroups(new String[] {"bs1Group1", "bs1Group2"});
Expand All @@ -145,7 +152,7 @@ public void run() {
SerializableRunnable startBS3 = new SerializableRunnable("start bridgeServer on " + bsPort3) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort3);
bs.setGroups(new String[] {"bs3Group1", "bs3Group2"});
Expand All @@ -166,7 +173,7 @@ public void run() {
vm2.invoke(new SerializableRunnable("start bridgeServer on " + bsPort2) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort2);
bs.setGroups(new String[] {"bs2Group1", "bs2Group2"});
Expand All @@ -182,7 +189,7 @@ public void run() {
vm2.invoke(new SerializableRunnable("start bridgeServer on " + bsPort4) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort4);
bs.setGroups(new String[] {"bs4Group1", "bs4Group2"});
Expand Down Expand Up @@ -273,7 +280,7 @@ public void run() {
vm1.invoke(
new SerializableRunnable("Verify bridge server view on " + bsPort1 + " and on " + bsPort3) {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand Down Expand Up @@ -303,7 +310,7 @@ public void run() {
vm2.invoke(
new SerializableRunnable("Verify bridge server view on " + bsPort2 + " and on " + bsPort4) {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand Down Expand Up @@ -333,7 +340,7 @@ public void run() {

SerializableRunnable stopBS = new SerializableRunnable("stop bridge server") {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
CacheServer bs = (CacheServer) bslist.get(0);
Expand Down Expand Up @@ -414,10 +421,9 @@ public void run() {

SerializableRunnable disconnect = new SerializableRunnable("Disconnect from " + locators) {
public void run() {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, locators);
DistributedSystem.connect(props).disconnect();
if (cache != null) {
cache.close();
}
}
};
SerializableRunnable stopLocator = new SerializableRunnable("Stop locator") {
Expand All @@ -443,7 +449,7 @@ public void run() {
});
vm2.invoke(new SerializableRunnable("Verify bridge server saw locator stop") {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand All @@ -466,7 +472,7 @@ public void run() {
});
vm1.invoke(new SerializableRunnable("Verify bridge server saw locator stop") {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand Down Expand Up @@ -495,7 +501,7 @@ public void run() {
SerializableRunnable restartBS = new SerializableRunnable("restart bridge server") {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
CacheServer bs = (CacheServer) bslist.get(0);
Expand Down Expand Up @@ -623,29 +629,19 @@ public void run() {

vm1.invoke(new SerializableRunnable("Connect to " + locators) {
public void run() {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, locators);
props.setProperty(GROUPS, "bs1Group1, bs1Group2");
props.setProperty(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
CacheFactory.create(DistributedSystem.connect(props));
createCache(locators, "bs1Group1, bs1Group2");
}
});
vm2.invoke(new SerializableRunnable("Connect to " + locators) {
public void run() {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, locators);
props.setProperty(GROUPS, "bs2Group1, bs2Group2");
props.setProperty(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
CacheFactory.create(DistributedSystem.connect(props));
createCache(locators, "bs2Group1, bs2Group2");
}
});

SerializableRunnable startBS1 = new SerializableRunnable("start bridgeServer on " + bsPort1) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort1);
bs.start();
Expand All @@ -659,7 +655,7 @@ public void run() {
SerializableRunnable startBS3 = new SerializableRunnable("start bridgeServer on " + bsPort3) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort3);
bs.start();
Expand All @@ -679,7 +675,7 @@ public void run() {
vm2.invoke(new SerializableRunnable("start bridgeServer on " + bsPort2) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort2);
bs.start();
Expand All @@ -694,7 +690,7 @@ public void run() {
vm2.invoke(new SerializableRunnable("start bridgeServer on " + bsPort4) {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
CacheServer bs = c.addCacheServer();
bs.setPort(bsPort4);
bs.start();
Expand Down Expand Up @@ -784,7 +780,7 @@ public void run() {
vm1.invoke(
new SerializableRunnable("Verify bridge server view on " + bsPort1 + " and on " + bsPort3) {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand Down Expand Up @@ -814,7 +810,7 @@ public void run() {
vm2.invoke(
new SerializableRunnable("Verify bridge server view on " + bsPort2 + " and on " + bsPort4) {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand Down Expand Up @@ -844,7 +840,7 @@ public void run() {

SerializableRunnable stopBS = new SerializableRunnable("stop bridge server") {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
CacheServer bs = (CacheServer) bslist.get(0);
Expand Down Expand Up @@ -925,7 +921,9 @@ public void run() {

SerializableRunnable disconnect = new SerializableRunnable("Disconnect from " + locators) {
public void run() {
InternalDistributedSystem.getAnyInstance().disconnect();
if (cache != null) {
cache.close();
}
}
};
SerializableRunnable stopLocator = new SerializableRunnable("Stop locator") {
Expand All @@ -951,7 +949,7 @@ public void run() {
});
vm2.invoke(new SerializableRunnable("Verify bridge server saw locator stop") {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand All @@ -974,7 +972,7 @@ public void run() {
});
vm1.invoke(new SerializableRunnable("Verify bridge server saw locator stop") {
public void run() {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
for (int i = 0; i < bslist.size(); i++) {
Expand Down Expand Up @@ -1003,7 +1001,7 @@ public void run() {
SerializableRunnable restartBS = new SerializableRunnable("restart bridge server") {
public void run() {
try {
Cache c = CacheFactory.getAnyInstance();
Cache c = cache;
List bslist = c.getCacheServers();
assertEquals(2, bslist.size());
CacheServer bs = (CacheServer) bslist.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
*/
package org.apache.geode.internal.cache;

import static org.apache.geode.distributed.ConfigurationProperties.*;
import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
import static org.apache.geode.distributed.ConfigurationProperties.LOG_LEVEL;
import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;

import java.net.InetAddress;
import java.util.Properties;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestName;

import org.apache.geode.cache.*;
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.EntryNotFoundException;
import org.apache.geode.cache.Operation;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.internal.Assert;
import org.apache.geode.internal.cache.entries.VersionedThinRegionEntryHeap;
Expand All @@ -40,23 +44,22 @@ public class TombstoneCreationJUnitTest {
@Rule
public TestName nameRule = new TestName();

private GemFireCacheImpl cache;

@Before
public void setUp() {
this.cache = (GemFireCacheImpl) new CacheFactory().set(LOCATORS, "").set(MCAST_PORT, "0")
.set(LOG_LEVEL, "config").create();
}

@After
public void tearDown() {
InternalDistributedSystem system = InternalDistributedSystem.getConnectedInstance();
if (system != null) {
system.disconnect();
}
this.cache.close();
}

@Test
public void testDestroyCreatesTombstone() throws Exception {
String name = nameRule.getMethodName();
Properties props = new Properties();
props.put(LOCATORS, "");
props.put(MCAST_PORT, "0");
props.put(LOG_LEVEL, "config");
GemFireCacheImpl cache =
(GemFireCacheImpl) CacheFactory.create(DistributedSystem.connect(props));
RegionFactory f = cache.createRegionFactory(RegionShortcut.REPLICATE);
DistributedRegion region = (DistributedRegion) f.create(name);

Expand Down Expand Up @@ -89,12 +92,6 @@ public void testDestroyCreatesTombstone() throws Exception {
@Test
public void testConcurrentCreateAndDestroy() throws Exception {
String name = nameRule.getMethodName();
Properties props = new Properties();
props.put(LOCATORS, "");
props.put(MCAST_PORT, "0");
props.put(LOG_LEVEL, "config");
final GemFireCacheImpl cache =
(GemFireCacheImpl) CacheFactory.create(DistributedSystem.connect(props));
RegionFactory f = cache.createRegionFactory(RegionShortcut.REPLICATE);
final DistributedRegion region = (DistributedRegion) f.create(name);

Expand Down Expand Up @@ -165,12 +162,6 @@ public void testConcurrentCreateAndDestroy() throws Exception {
@Test
public void testOlderEventIgnoredEvenIfTombstoneHasExpired() throws Exception {
String name = nameRule.getMethodName();
Properties props = new Properties();
props.put(LOCATORS, "");
props.put(MCAST_PORT, "0");
props.put(LOG_LEVEL, "config");
final GemFireCacheImpl cache =
(GemFireCacheImpl) CacheFactory.create(DistributedSystem.connect(props));
RegionFactory f = cache.createRegionFactory(RegionShortcut.REPLICATE);
final DistributedRegion region = (DistributedRegion) f.create(name);

Expand Down

0 comments on commit da1ce1c

Please sign in to comment.