Search in sources :

Example 1 with DSClock

use of org.apache.geode.distributed.internal.DSClock in project geode by apache.

the class Fakes method cache.

/**
   * A fake cache, which contains a fake distributed system, distribution manager, etc.
   */
public static GemFireCacheImpl cache() {
    GemFireCacheImpl cache = mock(GemFireCacheImpl.class);
    InternalDistributedSystem system = mock(InternalDistributedSystem.class);
    DistributionConfig config = mock(DistributionConfig.class);
    DistributionManager distributionManager = mock(DistributionManager.class);
    CancelCriterion systemCancelCriterion = mock(CancelCriterion.class);
    DSClock clock = mock(DSClock.class);
    LogWriter logger = mock(LogWriter.class);
    Statistics stats = mock(Statistics.class);
    InternalDistributedMember member;
    member = new InternalDistributedMember("localhost", 5555);
    when(config.getCacheXmlFile()).thenReturn(new File(""));
    when(config.getDeployWorkingDir()).thenReturn(new File("."));
    when(cache.getDistributedSystem()).thenReturn(system);
    when(cache.getInternalDistributedSystem()).thenReturn(system);
    when(cache.getSystem()).thenReturn(system);
    when(cache.getMyId()).thenReturn(member);
    when(cache.getDistributionManager()).thenReturn(distributionManager);
    when(cache.getCancelCriterion()).thenReturn(systemCancelCriterion);
    when(cache.getCachePerfStats()).thenReturn(mock(CachePerfStats.class));
    when(system.getDistributedMember()).thenReturn(member);
    when(system.getConfig()).thenReturn(config);
    when(system.getDistributionManager()).thenReturn(distributionManager);
    when(system.getCancelCriterion()).thenReturn(systemCancelCriterion);
    when(system.getClock()).thenReturn(clock);
    when(system.getLogWriter()).thenReturn(logger);
    when(system.createAtomicStatistics(any(), any(), anyLong())).thenReturn(stats);
    when(system.createAtomicStatistics(any(), any())).thenReturn(stats);
    when(distributionManager.getId()).thenReturn(member);
    when(distributionManager.getDistributionManagerId()).thenReturn(member);
    when(distributionManager.getConfig()).thenReturn(config);
    when(distributionManager.getSystem()).thenReturn(system);
    when(distributionManager.getCancelCriterion()).thenReturn(systemCancelCriterion);
    return cache;
}
Also used : DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) DSClock(org.apache.geode.distributed.internal.DSClock) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) LogWriter(org.apache.geode.LogWriter) CancelCriterion(org.apache.geode.CancelCriterion) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Statistics(org.apache.geode.Statistics) File(java.io.File)

Example 2 with DSClock

use of org.apache.geode.distributed.internal.DSClock in project geode by apache.

the class ClientServerTimeSyncDUnitTest method testClientTimeAdvances.

@Ignore("Bug 52327")
@Test
public void testClientTimeAdvances() {
    Host host = Host.getHost(0);
    // Server
    VM vm0 = host.getVM(0);
    // Client
    VM vm1 = host.getVM(1);
    final String regionName = "testRegion";
    final long TEST_OFFSET = 10000;
    ClientCache cache = null;
    try {
        final int serverPort = (Integer) vm0.invoke(new SerializableCallable("Start server with a region") {

            @Override
            public Object call() {
                Cache cache = getCache();
                cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
                LogWriterUtils.getLogWriter().info("Done creating region, now creating CacheServer");
                CacheServer server = null;
                try {
                    server = cache.addCacheServer();
                    server.setPort(AvailablePortHelper.getRandomAvailableTCPPort());
                    server.start();
                } catch (IOException e) {
                    Assert.fail("Starting cache server failed.", e);
                }
                // now set an artificial time offset for the test
                basicGetSystem().getClock().setCacheTimeOffset(null, TEST_OFFSET, true);
                LogWriterUtils.getLogWriter().info("Done creating and starting CacheServer on port " + server.getPort());
                return server.getPort();
            }
        });
        final String hostName = NetworkUtils.getServerHostName(vm0.getHost());
        // Start client with proxy region and register interest
        disconnectFromDS();
        Properties props = new Properties();
        props.setProperty(LOCATORS, "");
        props = getSystem(props).getProperties();
        cache = new ClientCacheFactory(props).setPoolSubscriptionEnabled(true).addPoolServer(hostName, serverPort).setPoolPingInterval(5000).create();
        Region proxyRegion = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
        proxyRegion.registerInterestRegex(".*");
        proxyRegion.put("testkey", "testValue1");
        final DSClock clock = ((GemFireCacheImpl) cache).getSystem().getClock();
        WaitCriterion wc = new WaitCriterion() {

            public boolean done() {
                long clientTimeOffset = clock.getCacheTimeOffset();
                LogWriterUtils.getLogWriter().info("Client node's new time offset is: " + clientTimeOffset);
                return clientTimeOffset >= TEST_OFFSET;
            }

            public String description() {
                return "Waiting for cacheTimeOffset to be non-zero.  PingOp should have set it to something";
            }
        };
        Wait.waitForCriterion(wc, 60000, 1000, true);
    } finally {
        cache.close();
        vm1.invoke(() -> CacheTestCase.disconnectFromDS());
    }
}
Also used : ClientCache(org.apache.geode.cache.client.ClientCache) IOException(java.io.IOException) Properties(java.util.Properties) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) DSClock(org.apache.geode.distributed.internal.DSClock) CacheServer(org.apache.geode.cache.server.CacheServer) ClientCache(org.apache.geode.cache.client.ClientCache) Ignore(org.junit.Ignore) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with DSClock

use of org.apache.geode.distributed.internal.DSClock in project geode by apache.

the class ClientServerTimeSyncDUnitTest method testClientTimeSlowsDown.

@Ignore("not yet implemented")
@Test
public void testClientTimeSlowsDown() {
    Host host = Host.getHost(0);
    // Server
    VM vm0 = host.getVM(0);
    // Client
    VM vm1 = host.getVM(1);
    final String regionName = "testRegion";
    final long TEST_OFFSET = 10000;
    ClientCache cache = null;
    try {
        final int serverPort = (Integer) vm0.invoke(new SerializableCallable("Start server with a region") {

            @Override
            public Object call() {
                Cache cache = getCache();
                cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
                LogWriterUtils.getLogWriter().info("Done creating region, now creating CacheServer");
                CacheServer server = null;
                try {
                    server = cache.addCacheServer();
                    server.setPort(AvailablePortHelper.getRandomAvailableTCPPort());
                    server.start();
                } catch (IOException e) {
                    Assert.fail("Starting cache server failed.", e);
                }
                // now set an artificial time offset for the test
                basicGetSystem().getClock().setCacheTimeOffset(null, -TEST_OFFSET, true);
                LogWriterUtils.getLogWriter().info("Done creating and starting CacheServer on port " + server.getPort());
                return server.getPort();
            }
        });
        // let cacheTimeMillis consume the time offset
        Wait.pause((int) TEST_OFFSET);
        final String hostName = NetworkUtils.getServerHostName(vm0.getHost());
        // Start client with proxy region and register interest
        disconnectFromDS();
        Properties props = new Properties();
        props.setProperty(LOCATORS, "");
        props = getSystem(props).getProperties();
        cache = new ClientCacheFactory(props).setPoolSubscriptionEnabled(true).addPoolServer(hostName, serverPort).setPoolPingInterval(5000).create();
        Region proxyRegion = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
        proxyRegion.registerInterestRegex(".*");
        proxyRegion.put("testkey", "testValue1");
        final DSClock clock = ((GemFireCacheImpl) cache).getSystem().getClock();
        WaitCriterion wc = new WaitCriterion() {

            public boolean done() {
                long clientTimeOffset = clock.getCacheTimeOffset();
                LogWriterUtils.getLogWriter().info("Client node's new time offset is: " + clientTimeOffset);
                if (clientTimeOffset >= 0) {
                    return false;
                }
                long cacheTime = clock.cacheTimeMillis();
                return Math.abs(System.currentTimeMillis() - (cacheTime - clientTimeOffset)) < 5;
            }

            public String description() {
                return "Waiting for cacheTimeOffset to be negative and cacheTimeMillis to stabilize";
            }
        };
        Wait.waitForCriterion(wc, 60000, 1000, true);
    } finally {
        cache.close();
        vm1.invoke(() -> CacheTestCase.disconnectFromDS());
    }
}
Also used : ClientCache(org.apache.geode.cache.client.ClientCache) IOException(java.io.IOException) Properties(java.util.Properties) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) DSClock(org.apache.geode.distributed.internal.DSClock) CacheServer(org.apache.geode.cache.server.CacheServer) ClientCache(org.apache.geode.cache.client.ClientCache) Ignore(org.junit.Ignore) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

DSClock (org.apache.geode.distributed.internal.DSClock)3 IOException (java.io.IOException)2 Properties (java.util.Properties)2 ClientCache (org.apache.geode.cache.client.ClientCache)2 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)2 CacheServer (org.apache.geode.cache.server.CacheServer)2 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 File (java.io.File)1 CancelCriterion (org.apache.geode.CancelCriterion)1 LogWriter (org.apache.geode.LogWriter)1 Statistics (org.apache.geode.Statistics)1 DistributionConfig (org.apache.geode.distributed.internal.DistributionConfig)1 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)1 CachePerfStats (org.apache.geode.internal.cache.CachePerfStats)1 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)1