use of org.apache.geode.internal.util.StopWatch in project geode by apache.
the class StatSamplerTestCase method waitForFileToDelete.
protected static void waitForFileToDelete(final File file, final long millis, final long sleep) {
boolean done = false;
try {
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < millis; done = (!file.exists())) {
Thread.sleep(sleep);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
assertTrue("Waiting for file to be deleted: " + file, done);
}
use of org.apache.geode.internal.util.StopWatch in project geode by apache.
the class HostedLocatorsDUnitTest method assertEventuallyTrue.
protected static void assertEventuallyTrue(final String message, final Callable<Boolean> callable, final int timeout, final int interval) throws Exception {
boolean done = false;
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < timeout; done = (callable.call())) {
Thread.sleep(interval);
}
assertTrue(message, done);
}
use of org.apache.geode.internal.util.StopWatch in project geode by apache.
the class DistributedLockServiceDUnitTest method testLockQuery.
@Test
public void testLockQuery() throws Exception {
final String dlsName = getUniqueName();
final VM vmGrantor = Host.getHost(0).getVM(0);
final VM vm1 = Host.getHost(0).getVM(1);
final VM vm2 = Host.getHost(0).getVM(2);
final String key1 = "key1";
// vmGrantor creates grantor
vmGrantor.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vmGrantor creates grantor");
connectDistributedSystem();
DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
assertTrue(dls.lock(key1, -1, -1));
assertTrue(dls.isLockGrantor());
dls.unlock(key1);
dls.freeResources(key1);
}
});
AsyncInvocation whileVM1Locks = null;
try {
// vm1 locks key1
whileVM1Locks = vm1.invokeAsync(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 locks key1");
connectDistributedSystem();
DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
assertTrue(dls.lock(key1, -1, -1));
assertFalse(dls.isLockGrantor());
try {
synchronized (testLockQuery_whileVM1Locks) {
testLockQuery_whileVM1Locks.set(true);
testLockQuery_whileVM1Locks.notifyAll();
long maxWait = 10000;
StopWatch timer = new StopWatch(true);
while (testLockQuery_whileVM1Locks.get()) {
// while true
long timeLeft = maxWait - timer.elapsedTimeMillis();
if (timeLeft > 0) {
testLockQuery_whileVM1Locks.wait(timeLeft);
} else {
fail("Test attempted to wait too long");
}
}
}
} catch (InterruptedException e) {
org.apache.geode.test.dunit.Assert.fail(e.getMessage(), e);
}
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 unlocks key1");
dls.unlock(key1);
dls.freeResources(key1);
}
});
// wait for vm1 to set testLockQuery_whileVM1Locks
// get DistributedMember for vm1
final DistributedMember vm1Member = (DistributedMember) vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 waits for locking thread");
synchronized (testLockQuery_whileVM1Locks) {
long maxWait = 10000;
StopWatch timer = new StopWatch(true);
while (!testLockQuery_whileVM1Locks.get()) {
// while false
long timeLeft = maxWait - timer.elapsedTimeMillis();
if (timeLeft > 0) {
testLockQuery_whileVM1Locks.wait(timeLeft);
} else {
fail("Test attempted to wait too long");
}
}
}
return getSystem().getDistributedMember();
}
});
assertNotNull(vm1Member);
// vmGrantor tests positive local dlock query
vmGrantor.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vmGrantor tests local query");
DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertTrue(result.getLeaseId() != -1);
assertEquals(Long.MAX_VALUE, result.getLeaseExpireTime());
RemoteThread lesseeThread = result.getLesseeThread();
assertNotNull(lesseeThread);
assertEquals(vm1Member, lesseeThread.getDistributedMember());
assertEquals(vm1Member, result.getLessee());
// nothing to test for on threadId unless we serialize info from vm1
}
});
// vm2 tests positive remote dlock query
vm2.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm2 tests remote query");
connectDistributedSystem();
DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertTrue(result.getLeaseId() != -1);
assertEquals(Long.MAX_VALUE, result.getLeaseExpireTime());
RemoteThread lesseeThread = result.getLesseeThread();
assertNotNull(lesseeThread);
assertEquals(vm1Member, lesseeThread.getDistributedMember());
assertEquals(vm1Member, result.getLessee());
// nothing to test for on threadId unless we serialize info from vm1
}
});
} finally {
// guarantee that testLockQuery_whileVM1Locks is notfied!
// vm1 sets and notifies testLockQuery_whileVM1Locks to release lock
vm1.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 notifies/releases key1");
synchronized (testLockQuery_whileVM1Locks) {
testLockQuery_whileVM1Locks.set(false);
testLockQuery_whileVM1Locks.notifyAll();
}
}
});
ThreadUtils.join(whileVM1Locks, 10 * 1000);
if (whileVM1Locks.exceptionOccurred()) {
org.apache.geode.test.dunit.Assert.fail("Test failed", whileVM1Locks.getException());
}
}
// vmGrantor tests negative local dlock query
vmGrantor.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vmGrantor tests negative query");
DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertEquals(-1, result.getLeaseId());
assertEquals(0, result.getLeaseExpireTime());
assertNull(result.getLesseeThread());
assertNull(result.getLessee());
}
});
// vm2 tests negative remote dlock query
vm2.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm2 tests negative query");
DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertEquals(-1, result.getLeaseId());
assertEquals(0, result.getLeaseExpireTime());
assertNull(result.getLesseeThread());
assertNull(result.getLessee());
}
});
}
use of org.apache.geode.internal.util.StopWatch in project geode by apache.
the class AbstractLauncherIntegrationTestCase method assertEventuallyTrue.
protected static void assertEventuallyTrue(final String message, final Callable<Boolean> callable, final int timeout, final int interval) throws Exception {
boolean done = false;
for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < timeout; done = (callable.call())) {
Thread.sleep(interval);
}
assertTrue(message, done);
}
use of org.apache.geode.internal.util.StopWatch in project geode by apache.
the class LoggingPerformanceTestCase method performTimeBasedLoggingTest.
protected long performTimeBasedLoggingTest(final PerformanceLogger perfLogger) {
System.out.println("\nBeginning " + getUniqueName());
final StopWatch stopWatch = new StopWatch(true);
long count = 0;
while (stopWatch.elapsedTimeMillis() < TIME_TO_RUN) {
perfLogger.log(message);
count++;
}
stopWatch.stop();
final long millis = stopWatch.elapsedTimeMillis();
final long seconds = millis / 1000;
final long minutes = seconds / 60;
System.out.println(getUniqueName() + " performTimeBasedLoggingTest");
System.out.println("Number of log statements: " + count);
System.out.println("Total elapsed time in millis: " + millis);
System.out.println("Total elapsed time in seconds: " + seconds);
System.out.println("Total elapsed time in minutes: " + minutes);
return millis;
}
Aggregations