use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.
the class DataContainerStressTest method initializeDefaultDataContainer.
private void initializeDefaultDataContainer(DefaultDataContainer dc) {
InternalEntryFactoryImpl entryFactory = new InternalEntryFactoryImpl();
TimeService timeService = new EmbeddedTimeService();
TestingUtil.inject(entryFactory, timeService);
// Mockito cannot be used as it will run out of memory from keeping all the invocations, thus we use blank impls
TestingUtil.inject(dc, (EvictionManager) (evicted, cmd) -> CompletableFutures.completedNull(), new PassivationManagerStub(), entryFactory, new ActivationManagerStub(), null, timeService, null, new InternalExpirationManager() {
@Override
public void processExpiration() {
}
@Override
public boolean isEnabled() {
return false;
}
@Override
public CompletableFuture<Boolean> entryExpiredInMemory(InternalCacheEntry entry, long currentTime, boolean writeOperation) {
return null;
}
@Override
public CompletionStage<Void> handleInStoreExpirationInternal(Object key) {
return null;
}
@Override
public CompletionStage<Void> handleInStoreExpirationInternal(MarshallableEntry marshalledEntry) {
return null;
}
@Override
public CompletionStage<Boolean> handlePossibleExpiration(InternalCacheEntry entry, int segment, boolean isWrite) {
return null;
}
@Override
public void addInternalListener(ExpirationConsumer consumer) {
}
@Override
public void removeInternalListener(Object listener) {
}
});
}
use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.
the class EmbeddedTimeServiceTest method testExpectedTime.
public void testExpectedTime() {
TimeService timeService = new EmbeddedTimeService() {
@Override
public long time() {
return 10;
}
@Override
public long wallClockTime() {
return 10;
}
};
assertEquals(timeService.expectedEndTime(-1, TimeUnit.NANOSECONDS), 10);
assertEquals(timeService.expectedEndTime(0, TimeUnit.NANOSECONDS), 10);
assertEquals(timeService.expectedEndTime(1, TimeUnit.NANOSECONDS), 11);
assertEquals(timeService.expectedEndTime(9, TimeUnit.NANOSECONDS), 19);
assertEquals(timeService.expectedEndTime(10, TimeUnit.NANOSECONDS), 20);
assertEquals(timeService.expectedEndTime(11, TimeUnit.NANOSECONDS), 21);
assertEquals(timeService.expectedEndTime(11, TimeUnit.MICROSECONDS), 11010);
assertEquals(timeService.expectedEndTime(11, TimeUnit.MILLISECONDS), 11000010);
}
use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.
the class EmbeddedTimeServiceTest method testDuration.
public void testDuration() {
TimeService timeService = new EmbeddedTimeService() {
@Override
public long time() {
return 10;
}
@Override
public long wallClockTime() {
return 10;
}
};
assertEquals(timeService.timeDuration(0, TimeUnit.NANOSECONDS), 10);
assertEquals(timeService.timeDuration(-1, TimeUnit.NANOSECONDS), 11);
assertEquals(timeService.timeDuration(10, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(11, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(9, TimeUnit.NANOSECONDS), 1);
assertEquals(timeService.timeDuration(9, TimeUnit.MICROSECONDS), 0);
assertEquals(timeService.timeDuration(9, TimeUnit.MILLISECONDS), 0);
assertEquals(timeService.timeDuration(0, 1, TimeUnit.NANOSECONDS), 1);
assertEquals(timeService.timeDuration(0, -1, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(1, 0, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(1, -1, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(-1, -1, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(0, 0, TimeUnit.NANOSECONDS), 0);
assertEquals(timeService.timeDuration(0, 1000, TimeUnit.MICROSECONDS), 1);
assertEquals(timeService.timeDuration(0, 1000000, TimeUnit.MILLISECONDS), 1);
}
use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.
the class JCacheLoaderTest method testLoadEntryWithExpiration.
public void testLoadEntryWithExpiration(Method m) {
final String cacheName = m.getName();
withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {
@Override
public void call() {
ControlledTimeService timeService = new ControlledTimeService();
TestingUtil.replaceComponent(cm, TimeService.class, timeService, true);
JCacheManager jCacheManager = createJCacheManager(cm, this);
InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
cacheLoader.store(1, "v1").store(2, "v2");
MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
final long lifespan = 3000;
cfg.setReadThrough(true);
cfg.setExpiryPolicyFactory((Factory<ExpiryPolicy>) () -> new ExpiryPolicy() {
@Override
public Duration getExpiryForCreation() {
return new Duration(TimeUnit.MILLISECONDS, lifespan);
}
@Override
public Duration getExpiryForAccess() {
return null;
}
@Override
public Duration getExpiryForUpdate() {
return Duration.ZERO;
}
});
cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
assertEquals("v2", cache.get(2));
assertEquals("v1", cache.get(1));
timeService.advance(lifespan + 100);
DataContainer<Integer, String> dc = cache.unwrap(AdvancedCache.class).getDataContainer();
assertEquals(null, dc.peek(2));
assertEquals(null, dc.peek(1));
}
});
}
use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.
the class BiasLeaseTest method createCacheManagers.
@Override
protected void createCacheManagers() throws Throwable {
ConfigurationBuilder builder = getDefaultClusteredCacheConfig(CacheMode.SCATTERED_SYNC, false);
builder.clustering().biasAcquisition(BiasAcquisition.ON_WRITE);
// Scan biases frequently
builder.expiration().wakeUpInterval(100);
createCluster(TestDataSCI.INSTANCE, builder, 3);
waitForClusterToForm();
Arrays.stream(managers()).forEach(cm -> TestingUtil.replaceComponent(cm, TimeService.class, timeService, true));
rpcManager0 = ControlledRpcManager.replaceRpcManager(cache(0));
rpcManager1 = CountingRpcManager.replaceRpcManager(cache(1));
TestingUtil.wrapInboundInvocationHandler(cache(0), handler -> handler0 = new RenewWaitingInvocationHandler(handler));
}
Aggregations