Search in sources :

Example 6 with StopWatch

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);
}
Also used : StopWatch(org.apache.geode.internal.util.StopWatch)

Example 7 with StopWatch

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);
}
Also used : StopWatch(org.apache.geode.internal.util.StopWatch)

Example 8 with StopWatch

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());
        }
    });
}
Also used : RemoteThread(org.apache.geode.distributed.internal.locks.RemoteThread) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) DLockService(org.apache.geode.distributed.internal.locks.DLockService) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) DLockRemoteToken(org.apache.geode.distributed.internal.locks.DLockRemoteToken) StopWatch(org.apache.geode.internal.util.StopWatch) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 9 with StopWatch

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);
}
Also used : StopWatch(org.apache.geode.internal.util.StopWatch)

Example 10 with StopWatch

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;
}
Also used : StopWatch(org.apache.geode.internal.util.StopWatch)

Aggregations

StopWatch (org.apache.geode.internal.util.StopWatch)37 Test (org.junit.Test)13 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)4 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)4 File (java.io.File)3 IOException (java.io.IOException)3 Properties (java.util.Properties)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Statistics (org.apache.geode.Statistics)2 Region (org.apache.geode.cache.Region)2 FailureTracker (org.apache.geode.cache.client.internal.ServerBlackList.FailureTracker)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 ServerLocation (org.apache.geode.distributed.internal.ServerLocation)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2 MemberMXBean (org.apache.geode.management.MemberMXBean)2 CliMetaData (org.apache.geode.management.cli.CliMetaData)2 MXBeanProvider.getMemberMXBean (org.apache.geode.management.internal.cli.shell.MXBeanProvider.getMemberMXBean)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2