Search in sources :

Example 1 with TimeService

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) {
        }
    });
}
Also used : Arrays(java.util.Arrays) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) LogFactory(org.infinispan.util.logging.LogFactory) EvictionManager(org.infinispan.eviction.EvictionManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Metadata(org.infinispan.metadata.Metadata) Test(org.testng.annotations.Test) EvictionType(org.infinispan.eviction.EvictionType) InternalExpirationManager(org.infinispan.expiration.impl.InternalExpirationManager) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Log(org.infinispan.util.logging.Log) PassivationManagerStub(org.infinispan.eviction.impl.PassivationManagerStub) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) TestingUtil(org.infinispan.test.TestingUtil) ActivationManagerStub(org.infinispan.eviction.impl.ActivationManagerStub) DataContainer(org.infinispan.container.DataContainer) EmbeddedTimeService(org.infinispan.util.EmbeddedTimeService) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) DefaultDataContainer(org.infinispan.container.impl.DefaultDataContainer) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CompletionStage(java.util.concurrent.CompletionStage) InternalEntryFactoryImpl(org.infinispan.container.impl.InternalEntryFactoryImpl) TimeService(org.infinispan.commons.time.TimeService) InternalEntryFactoryImpl(org.infinispan.container.impl.InternalEntryFactoryImpl) InternalExpirationManager(org.infinispan.expiration.impl.InternalExpirationManager) EmbeddedTimeService(org.infinispan.util.EmbeddedTimeService) TimeService(org.infinispan.commons.time.TimeService) PassivationManagerStub(org.infinispan.eviction.impl.PassivationManagerStub) CompletableFuture(java.util.concurrent.CompletableFuture) EmbeddedTimeService(org.infinispan.util.EmbeddedTimeService) ActivationManagerStub(org.infinispan.eviction.impl.ActivationManagerStub) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) CompletionStage(java.util.concurrent.CompletionStage)

Example 2 with TimeService

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);
}
Also used : TimeService(org.infinispan.commons.time.TimeService)

Example 3 with TimeService

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);
}
Also used : TimeService(org.infinispan.commons.time.TimeService)

Example 4 with TimeService

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));
        }
    });
}
Also used : JCacheManager(org.infinispan.jcache.embedded.JCacheManager) ControlledTimeService(org.infinispan.util.ControlledTimeService) TimeService(org.infinispan.commons.time.TimeService) Factory(javax.cache.configuration.Factory) TestCacheManagerFactory(org.infinispan.test.fwk.TestCacheManagerFactory) InMemoryJCacheLoader(org.infinispan.jcache.util.InMemoryJCacheLoader) Duration(javax.cache.expiry.Duration) MutableConfiguration(javax.cache.configuration.MutableConfiguration) DataContainer(org.infinispan.container.DataContainer) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) ControlledTimeService(org.infinispan.util.ControlledTimeService) AdvancedCache(org.infinispan.AdvancedCache) Cache(javax.cache.Cache)

Example 5 with TimeService

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));
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) ControlledTimeService(org.infinispan.util.ControlledTimeService) TimeService(org.infinispan.commons.time.TimeService)

Aggregations

TimeService (org.infinispan.commons.time.TimeService)18 ControlledTimeService (org.infinispan.util.ControlledTimeService)4 HashSet (java.util.HashSet)3 AdvancedCache (org.infinispan.AdvancedCache)3 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 EmbeddedTimeService (org.infinispan.util.EmbeddedTimeService)3 Log (org.infinispan.util.logging.Log)3 LogFactory (org.infinispan.util.logging.LogFactory)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 TimeUnit (java.util.concurrent.TimeUnit)2 Configuration (org.infinispan.configuration.cache.Configuration)2 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)2 DataContainer (org.infinispan.container.DataContainer)2 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)2 DataConversion (org.infinispan.encoding.DataConversion)2