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;
}
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());
}
}
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());
}
}
Aggregations