use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class DistributedSystemLogFileJUnitTest method testDistributedSystemWithSecurityInfoLevelAndLogAtFineLevelButNoSecurityLog.
/**
* tests scenario where security log has not been set but a level has been set to a less granular
* level than that of the regular log. Verifies that the correct logs for security show up in the
* regular log as expected
*
* @throws Exception
*/
@Test
public void testDistributedSystemWithSecurityInfoLevelAndLogAtFineLevelButNoSecurityLog() throws Exception {
// final int port = AvailablePort.getRandomAvailablePort(AvailablePort.JGROUPS);
final String logFileName = name.getMethodName() + "-system-" + System.currentTimeMillis() + ".log";
final Properties properties = new Properties();
properties.put(LOG_FILE, logFileName);
properties.put(LOG_LEVEL, "fine");
properties.put(SECURITY_LOG_LEVEL, "info");
properties.put(MCAST_PORT, "0");
properties.put(LOCATORS, "");
properties.put(ENABLE_NETWORK_PARTITION_DETECTION, "false");
properties.put(DISABLE_AUTO_RECONNECT, "true");
properties.put(MEMBER_TIMEOUT, "2000");
properties.put(ENABLE_CLUSTER_CONFIGURATION, "false");
final File logFile = new File(logFileName);
if (logFile.exists()) {
logFile.delete();
}
assertFalse(logFile.exists());
this.system = DistributedSystem.connect(properties);
assertNotNull(this.system);
DistributionConfig config = ((InternalDistributedSystem) this.system).getConfig();
assertEquals("Expected " + LogWriterImpl.levelToString(InternalLogWriter.INFO_LEVEL) + " but was " + LogWriterImpl.levelToString(config.getLogLevel()), InternalLogWriter.INFO_LEVEL, config.getSecurityLogLevel());
assertEquals("Expected " + LogWriterImpl.levelToString(InternalLogWriter.FINE_LEVEL) + " but was " + LogWriterImpl.levelToString(config.getLogLevel()), InternalLogWriter.FINE_LEVEL, config.getLogLevel());
InternalLogWriter securityLogWriter = (InternalLogWriter) system.getSecurityLogWriter();
InternalLogWriter logWriter = (InternalLogWriter) system.getLogWriter();
assertNotNull(securityLogWriter);
assertNotNull(logWriter);
assertTrue(securityLogWriter instanceof LogWriterLogger);
assertTrue(logWriter instanceof LogWriterLogger);
assertEquals("Expected " + LogWriterImpl.levelToString(InternalLogWriter.INFO_LEVEL) + " but was " + LogWriterImpl.levelToString(securityLogWriter.getLogWriterLevel()), InternalLogWriter.INFO_LEVEL, securityLogWriter.getLogWriterLevel());
assertEquals("Expected " + LogWriterImpl.levelToString(InternalLogWriter.FINE_LEVEL) + " but was " + LogWriterImpl.levelToString(securityLogWriter.getLogWriterLevel()), InternalLogWriter.FINE_LEVEL, logWriter.getLogWriterLevel());
assertFalse(securityLogWriter.fineEnabled());
assertTrue(logWriter.fineEnabled());
assertFalse(((LogWriterLogger) securityLogWriter).isDebugEnabled());
assertTrue(((LogWriterLogger) logWriter).isDebugEnabled());
assertTrue(securityLogWriter instanceof FastLogger);
assertTrue(logWriter instanceof FastLogger);
assertTrue(((FastLogger) securityLogWriter).isDelegating());
assertTrue(((FastLogger) logWriter).isDelegating());
Wait.waitForCriterion(new WaitCriterion() {
@Override
public boolean done() {
return logFile.exists();
}
@Override
public String description() {
return "waiting for log files to exist: " + logFile;
}
}, TIMEOUT_MILLISECONDS, INTERVAL_MILLISECONDS, true);
assertTrue(logFile.exists());
final Logger logger = LogService.getLogger();
int i = 0;
{
i++;
final String FINEST_STRING = "testLogLevels Message logged at FINEST level [" + i + "]";
securityLogWriter.finest(FINEST_STRING);
assertFalse(fileContainsString(logFile, FINEST_STRING));
i++;
final String FINER_STRING = "testLogLevels Message logged at FINER level [" + i + "]";
securityLogWriter.finer(FINER_STRING);
assertFalse(fileContainsString(logFile, FINER_STRING));
i++;
final String FINE_STRING = "testLogLevels Message logged at FINE level [" + i + "]";
securityLogWriter.fine(FINE_STRING);
assertFalse(fileContainsString(logFile, FINE_STRING));
i++;
final String CONFIG_STRING = "testLogLevels Message logged at CONFIG level [" + i + "]";
securityLogWriter.config(CONFIG_STRING);
assertTrue(fileContainsString(logFile, CONFIG_STRING));
i++;
final String INFO_STRING = "testLogLevels Message logged at INFO level [" + i + "]";
securityLogWriter.info(INFO_STRING);
assertTrue(fileContainsString(logFile, INFO_STRING));
i++;
final String WARNING_STRING = "ExpectedStrings: testLogLevels Message logged at WARNING level [" + i + "]";
securityLogWriter.warning(WARNING_STRING);
assertTrue(fileContainsString(logFile, WARNING_STRING));
i++;
final String ERROR_STRING = "ExpectedStrings: testLogLevels Message logged at ERROR level [" + i + "]";
securityLogWriter.error(ERROR_STRING);
assertTrue(fileContainsString(logFile, ERROR_STRING));
i++;
final String SEVERE_STRING = "ExpectedStrings: testLogLevels Message logged at SEVERE level [" + i + "]";
securityLogWriter.severe(SEVERE_STRING);
assertTrue(fileContainsString(logFile, SEVERE_STRING));
i++;
final String TRACE_STRING = "testLogLevels Message logged at TRACE level [" + i + "]";
logger.trace(TRACE_STRING);
assertFalse(fileContainsString(logFile, TRACE_STRING));
i++;
final String DEBUG_STRING = "testLogLevels Message logged at DEBUG level [" + i + "]";
logger.debug(DEBUG_STRING);
assertTrue(fileContainsString(logFile, DEBUG_STRING));
i++;
final String INFO_STRING_J = "testLogLevels Message logged at INFO level [" + i + "]";
logger.info(INFO_STRING_J);
assertTrue(fileContainsString(logFile, INFO_STRING_J));
i++;
final String WARN_STRING = "ExpectedStrings: testLogLevels Message logged at WARN level [" + i + "]";
logger.warn(WARN_STRING);
assertTrue(fileContainsString(logFile, WARN_STRING));
i++;
final String ERROR_STRING_J = "ExpectedStrings: testLogLevels Message logged at ERROR level [" + i + "]";
logger.error(ERROR_STRING_J);
assertTrue(fileContainsString(logFile, ERROR_STRING_J));
i++;
final String FATAL_STRING = "ExpectedStrings: testLogLevels Message logged at FATAL level [" + i + "]";
logger.fatal(FATAL_STRING);
assertTrue(fileContainsString(logFile, FATAL_STRING));
}
this.system.disconnect();
this.system = null;
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class DiskStoreCommandsDUnitTest method testMissingDiskStoreCommandWithColocation.
@Test
public void testMissingDiskStoreCommandWithColocation() {
final String regionName = "testShowPersistentRecoveryFailuresRegion";
final String childName = "childRegion";
setUpJmxManagerOnVm0ThenConnect(null);
final VM vm0 = Host.getHost(0).getVM(0);
final VM vm1 = Host.getHost(0).getVM(1);
final String vm1Name = "VM" + vm1.getPid();
final String diskStoreName = "DiskStoreCommandsDUnitTest";
// Default setup creates a cache in the Manager, now create a cache in VM1
vm1.invoke(new SerializableRunnable() {
public void run() {
Properties localProps = new Properties();
localProps.setProperty(NAME, vm1Name);
getSystem(localProps);
Cache cache = getCache();
}
});
// Create a disk store and region in the Manager (VM0) and VM1 VMs
for (final VM vm : (new VM[] { vm0, vm1 })) {
final String vmName = "VM" + vm.getPid();
vm.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
File diskStoreDirFile = new File(diskStoreName + vm.getPid());
diskStoreDirFile.mkdirs();
DiskStoreFactory diskStoreFactory = cache.createDiskStoreFactory();
diskStoreFactory.setDiskDirs(new File[] { diskStoreDirFile });
diskStoreFactory.setMaxOplogSize(1);
diskStoreFactory.setAllowForceCompaction(true);
diskStoreFactory.setAutoCompact(false);
diskStoreFactory.create(regionName);
diskStoreFactory.create(childName);
RegionFactory regionFactory = cache.createRegionFactory();
regionFactory.setDiskStoreName(regionName);
regionFactory.setDiskSynchronous(true);
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
regionFactory.create(regionName);
PartitionAttributes pa = new PartitionAttributesFactory().setColocatedWith(regionName).create();
RegionFactory childRegionFactory = cache.createRegionFactory();
childRegionFactory.setPartitionAttributes(pa);
childRegionFactory.setDiskStoreName(childName);
childRegionFactory.setDiskSynchronous(true);
childRegionFactory.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
childRegionFactory.create(childName);
}
});
}
// Add data to the region
vm0.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion(regionName);
region.put("A", "a");
region.put("B", "b");
}
});
// Make sure that everything thus far is okay and there are no missing disk stores
CommandResult cmdResult = executeCommand(CliStrings.SHOW_MISSING_DISK_STORE);
System.out.println("command result=\n" + commandResultToString(cmdResult));
assertEquals(Result.Status.OK, cmdResult.getStatus());
assertTrue(cmdResult.toString(), commandResultToString(cmdResult).contains("No missing disk store found"));
// Close the regions in the Manager (VM0) VM
vm0.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion(childName);
region.close();
region = cache.getRegion(regionName);
region.close();
}
});
// Add data to VM1 and then close the region
vm1.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region childRegion = cache.getRegion(childName);
PartitionedRegion parentRegion = (PartitionedRegion) (cache.getRegion(regionName));
try {
parentRegion.put("A", "C");
} catch (Exception e) {
// Ignore any exception on the put
}
childRegion.close();
parentRegion.close();
}
});
SerializableRunnable restartParentRegion = new SerializableRunnable("Restart parent region on") {
public void run() {
Cache cache = getCache();
RegionFactory regionFactory = cache.createRegionFactory();
regionFactory.setDiskStoreName(regionName);
regionFactory.setDiskSynchronous(true);
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
try {
regionFactory.create(regionName);
} catch (Exception e) {
// okay to ignore
}
}
};
SerializableRunnable restartChildRegion = new SerializableRunnable("Restart child region") {
public void run() {
Cache cache = getCache();
PartitionAttributes pa = new PartitionAttributesFactory().setColocatedWith(regionName).create();
RegionFactory regionFactory = cache.createRegionFactory();
regionFactory.setPartitionAttributes(pa);
regionFactory.setDiskStoreName(childName);
regionFactory.setDiskSynchronous(true);
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
try {
regionFactory.create(childName);
} catch (Exception e) {
// okay to ignore
e.printStackTrace();
}
}
};
// Add the region back to the Manager (VM0) VM
AsyncInvocation async0 = vm0.invokeAsync(restartParentRegion);
AsyncInvocation async1 = vm1.invokeAsync(restartParentRegion);
// Wait for the region in the Manager (VM0) to come online
vm0.invoke(new SerializableRunnable("WaitForRegionInVm0") {
public void run() {
WaitCriterion waitCriterion = new WaitCriterion() {
public boolean done() {
Cache cache = getCache();
PersistentMemberManager memberManager = ((GemFireCacheImpl) cache).getPersistentMemberManager();
return !memberManager.getWaitingRegions().isEmpty();
}
public String description() {
return "Waiting for another persistent member to come online";
}
};
try {
waitForCriterion(waitCriterion, 5000, 100, true);
} catch (AssertionError ae) {
// Ignore. waitForCriterion is expected to timeout in this test
}
}
});
// Validate that there is a missing disk store on VM1
try {
cmdResult = executeCommand(CliStrings.SHOW_MISSING_DISK_STORE);
assertNotNull("Expect command result != null", cmdResult);
assertEquals(Result.Status.OK, cmdResult.getStatus());
String stringResult = commandResultToString(cmdResult);
System.out.println("command result=\n" + stringResult);
// Expect 2 result sections with header lines and 4 information lines in the first section
assertEquals(6, countLinesInString(stringResult, false));
assertTrue(stringContainsLine(stringResult, "Host.*Distributed Member.*Parent Region.*Missing Colocated Region"));
assertTrue(stringContainsLine(stringResult, ".*" + regionName + ".*" + childName));
AsyncInvocation async0b = vm0.invokeAsync(restartChildRegion);
try {
async0b.get(5000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
// Expected timeout - Region recovery is still waiting on vm1 child region and disk-store to
// come online
}
cmdResult = executeCommand(CliStrings.SHOW_MISSING_DISK_STORE);
assertNotNull("Expect command result != null", cmdResult);
assertEquals(Result.Status.OK, cmdResult.getStatus());
stringResult = commandResultToString(cmdResult);
System.out.println("command result=\n" + stringResult);
// Extract the id from the returned missing disk store
String line = getLineFromString(stringResult, 4);
assertFalse(line.contains("---------"));
StringTokenizer resultTokenizer = new StringTokenizer(line);
String id = resultTokenizer.nextToken();
AsyncInvocation async1b = vm1.invokeAsync(restartChildRegion);
try {
async1b.get(5000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
cmdResult = executeCommand(CliStrings.SHOW_MISSING_DISK_STORE);
assertNotNull("Expect command result != null", cmdResult);
assertEquals(Result.Status.OK, cmdResult.getStatus());
stringResult = commandResultToString(cmdResult);
System.out.println("command result=\n" + stringResult);
} finally {
// Verify that the invokeAsync thread terminated
try {
async0.get(10000, TimeUnit.MILLISECONDS);
async1.get(10000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
fail("Unexpected timeout waitiong for invokeAsync threads to terminate: " + e.getMessage());
}
}
// Do our own cleanup so that the disk store directories can be removed
super.destroyDefaultSetup();
for (final VM vm : (new VM[] { vm0, vm1 })) {
final String vmName = "VM" + vm.getPid();
vm.invoke(new SerializableRunnable() {
public void run() {
try {
FileUtils.deleteDirectory((new File(diskStoreName + vm.getPid())));
} catch (IOException iex) {
// There's nothing else we can do
}
}
});
}
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class DiskStoreCommandsDUnitTest method testMissingDiskStore.
// GEODE-2102
@Category(FlakyTest.class)
@Test
public void testMissingDiskStore() {
final String regionName = "testShowMissingDiskStoreRegion";
setUpJmxManagerOnVm0ThenConnect(null);
final VM vm0 = Host.getHost(0).getVM(0);
final VM vm1 = Host.getHost(0).getVM(1);
final String vm1Name = "VM" + vm1.getPid();
final String diskStoreName = "DiskStoreCommandsDUnitTest";
// Default setup creates a cache in the Manager, now create a cache in VM1
vm1.invoke(new SerializableRunnable() {
public void run() {
Properties localProps = new Properties();
localProps.setProperty(NAME, vm1Name);
getSystem(localProps);
Cache cache = getCache();
}
});
// Create a disk store and region in the Manager (VM0) and VM1 VMs
for (final VM vm : (new VM[] { vm0, vm1 })) {
final String vmName = "VM" + vm.getPid();
vm.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
File diskStoreDirFile = new File(diskStoreName + vm.getPid());
diskStoreDirFile.mkdirs();
DiskStoreFactory diskStoreFactory = cache.createDiskStoreFactory();
diskStoreFactory.setDiskDirs(new File[] { diskStoreDirFile });
diskStoreFactory.setMaxOplogSize(1);
diskStoreFactory.setAllowForceCompaction(true);
diskStoreFactory.setAutoCompact(false);
diskStoreFactory.create(regionName);
RegionFactory regionFactory = cache.createRegionFactory();
regionFactory.setDiskStoreName(regionName);
regionFactory.setDiskSynchronous(true);
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
regionFactory.setScope(Scope.DISTRIBUTED_ACK);
regionFactory.create(regionName);
}
});
}
// Add data to the region
vm0.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion(regionName);
region.put("A", "B");
}
});
// Make sure that everything thus far is okay and there are no missing disk stores
CommandResult cmdResult = executeCommand(CliStrings.SHOW_MISSING_DISK_STORE);
assertEquals(Result.Status.OK, cmdResult.getStatus());
assertTrue(commandResultToString(cmdResult).contains("No missing disk store found"));
// Close the region in the Manager (VM0) VM
vm0.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion(regionName);
region.close();
}
});
// Add data to VM1 and then close the region
vm1.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion(regionName);
region.put("A", "C");
region.close();
}
});
// Add the region back to the Manager (VM0) VM
vm0.invokeAsync(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
RegionFactory regionFactory = cache.createRegionFactory();
regionFactory.setDiskStoreName(regionName);
regionFactory.setDiskSynchronous(true);
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
regionFactory.setScope(Scope.DISTRIBUTED_ACK);
try {
regionFactory.create(regionName);
} catch (DistributedSystemDisconnectedException ignore) {
// okay to ignore
}
}
});
// Wait for the region in the Manager (VM0) to come online
vm0.invoke(new SerializableRunnable() {
public void run() {
WaitCriterion waitCriterion = new WaitCriterion() {
public boolean done() {
Cache cache = getCache();
PersistentMemberManager memberManager = ((InternalCache) cache).getPersistentMemberManager();
return !memberManager.getWaitingRegions().isEmpty();
}
public String description() {
return "Waiting for another persistent member to come online";
}
};
waitForCriterion(waitCriterion, 70000, 100, true);
}
});
// Validate that there is a missing disk store on VM1
cmdResult = executeCommand(CliStrings.SHOW_MISSING_DISK_STORE);
assertEquals(Result.Status.OK, cmdResult.getStatus());
String stringResult = commandResultToString(cmdResult);
System.out.println("command result=" + stringResult);
assertEquals(5, countLinesInString(stringResult, false));
assertTrue(stringContainsLine(stringResult, "Disk Store ID.*Host.*Directory"));
assertTrue(stringContainsLine(stringResult, ".*" + diskStoreName + vm1.getPid()));
// Extract the id from the returned missing disk store
String line = getLineFromString(stringResult, 4);
assertFalse(line.contains("---------"));
StringTokenizer resultTokenizer = new StringTokenizer(line);
String id = resultTokenizer.nextToken();
// Remove the missing disk store and validate the result
cmdResult = executeCommand("revoke missing-disk-store --id=" + id);
assertNotNull(cmdResult);
assertEquals(Result.Status.OK, cmdResult.getStatus());
assertTrue(commandResultToString(cmdResult).contains("Missing disk store successfully revoked"));
// Do our own cleanup so that the disk store directories can be removed
super.destroyDefaultSetup();
for (final VM vm : (new VM[] { vm0, vm1 })) {
final String vmName = "VM" + vm.getPid();
vm.invoke(new SerializableRunnable() {
public void run() {
try {
FileUtils.deleteDirectory((new File(diskStoreName + vm.getPid())));
} catch (IOException iex) {
// There's nothing else we can do
}
}
});
}
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class RegionReliabilityTestCase method waitForEntryDestroy.
public static void waitForEntryDestroy(final Region region, final Object key) {
WaitCriterion wc = new WaitCriterion() {
public boolean done() {
return region.get(key) == null;
}
public String description() {
return "expected entry " + key + " to not exist but it has the value " + region.get(key);
}
};
Wait.waitForCriterion(wc, 30 * 1000, 10, true);
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class DistributedLockServiceDUnitTest method testSuspendLockingBlocksUntilNoLocks.
/**
* Test that exlusive locking prohibits locking activity
*/
@Test
public void testSuspendLockingBlocksUntilNoLocks() throws InterruptedException {
final String name = getUniqueName();
distributedCreateService(2, name);
final DistributedLockService service = DistributedLockService.getServiceNamed(name);
// Get lock from other VM. Since same thread needs to lock and unlock,
// invoke asynchronously, get lock, wait to be notified, then unlock.
VM vm1 = Host.getHost(0).getVM(1);
vm1.invokeAsync(new SerializableRunnable("Lock & unlock in vm1") {
public void run() {
DistributedLockService service2 = DistributedLockService.getServiceNamed(name);
assertTrue(service2.lock("lock", -1, -1));
synchronized (monitor) {
try {
monitor.wait();
} catch (InterruptedException ex) {
System.out.println("Unexpected InterruptedException");
fail("interrupted");
}
}
service2.unlock("lock");
}
});
// Let vm1's thread get the lock and go into wait()
Thread.sleep(100);
Thread thread = new Thread(new Runnable() {
public void run() {
setGot(service.suspendLocking(-1));
setDone(true);
service.resumeLocking();
}
});
setGot(false);
setDone(false);
thread.start();
// Let thread start, make sure it's blocked in suspendLocking
Thread.sleep(100);
assertFalse("Before release, got: " + getGot() + ", done: " + getDone(), getGot() || getDone());
vm1.invoke(new SerializableRunnable("notify vm1 to unlock") {
public void run() {
synchronized (monitor) {
monitor.notify();
}
}
});
// Let thread finish, make sure it successfully suspended and is done
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return getDone();
}
public String description() {
return null;
}
};
Wait.waitForCriterion(ev, 30 * 1000, 200, true);
if (!getGot() || !getDone()) {
ThreadUtils.dumpAllStacks();
}
assertTrue("After release, got: " + getGot() + ", done: " + getDone(), getGot() && getDone());
}
Aggregations