Search in sources :

Example 1 with FF4JCacheManager

use of org.ff4j.cache.FF4JCacheManager in project ff4j by ff4j.

the class CacheProxyTest method testCacheProxyManager.

@Test
public void testCacheProxyManager() {
    FF4jCacheProxy proxy = new FF4jCacheProxy();
    FF4JCacheManager cm = new InMemoryCacheManager();
    proxy.setCacheManager(cm);
    proxy.isCached();
    Assert.assertNotNull(proxy.getCacheProvider());
    proxy.setTargetPropertyStore(new InMemoryPropertyStore());
    Assert.assertEquals(0, proxy.readAllProperties().size());
    proxy.createProperty(new PropertyString("p1", "v1"));
    Assert.assertTrue(proxy.existProperty("p1"));
    Assert.assertFalse(proxy.existProperty("p2"));
    proxy.setTargetFeatureStore(new InMemoryFeatureStore());
    Set<Feature> setOfFeatures = new HashSet<Feature>();
    setOfFeatures.add(new Feature("f1"));
    setOfFeatures.add(new Feature("f2"));
    proxy.importFeatures(setOfFeatures);
}
Also used : FF4JCacheManager(org.ff4j.cache.FF4JCacheManager) PropertyString(org.ff4j.property.PropertyString) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) InMemoryCacheManager(org.ff4j.cache.InMemoryCacheManager) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) Feature(org.ff4j.core.Feature) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with FF4JCacheManager

use of org.ff4j.cache.FF4JCacheManager in project ff4j by ff4j.

the class CacheProxyWithPollingTest method testCacheProxyManagerProperty.

@Test
public void testCacheProxyManagerProperty() throws InterruptedException {
    // When
    FeatureStore fs = new InMemoryFeatureStore("ff4j.xml");
    PropertyStore ps = new InMemoryPropertyStore("ff4j.xml");
    FF4JCacheManager cm = new InMemoryCacheManager();
    FF4jCacheProxy proxy = new FF4jCacheProxy(fs, ps, cm);
    // Start polling on 100ms basis
    proxy.startPolling(100);
    proxy.createSchema();
    Thread.sleep(200);
    // When (Remove something)
    fs.delete("AwesomeFeature");
    // Then (Proxy is not yet refresh)
    Assert.assertTrue(proxy.exist("AwesomeFeature"));
    // When (wait for cache refresh)
    Thread.sleep(200);
    // Then (also delete in cache si Cache is refreshed)
    Assert.assertFalse(proxy.exist("AwesomeFeature"));
    Store2CachePollingScheduler scheduler = proxy.getStore2CachePoller();
    scheduler.setInitialDelay(scheduler.getInitialDelay());
    scheduler.setPollingDelay(scheduler.getPollingDelay());
    proxy.stopPolling();
    proxy.setStore2CachePoller(new Store2CachePollingScheduler(fs, ps, cm));
}
Also used : FF4JCacheManager(org.ff4j.cache.FF4JCacheManager) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) InMemoryCacheManager(org.ff4j.cache.InMemoryCacheManager) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FeatureStore(org.ff4j.core.FeatureStore) Store2CachePollingScheduler(org.ff4j.cache.Store2CachePollingScheduler) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) PropertyStore(org.ff4j.property.store.PropertyStore) Test(org.junit.Test)

Example 3 with FF4JCacheManager

use of org.ff4j.cache.FF4JCacheManager in project ff4j by ff4j.

the class CacheProxyWithPollingTest method testCacheProxyManagerPropertyDuringRefresh.

@Test
public void testCacheProxyManagerPropertyDuringRefresh() throws InterruptedException {
    FeatureStore fs = new InMemoryFeatureStore("ff4j.xml");
    PropertyStore ps = new InMemoryPropertyStore("ff4j.xml");
    FF4JCacheManager cm = new InMemoryCacheManager();
    final FF4jCacheProxy proxy = new FF4jCacheProxy(fs, ps, cm);
    // scheduler refreshing cache through worker thread
    Store2CachePollingScheduler store2CachePollingScheduler = new Store2CachePollingScheduler(proxy);
    // setting polling delay 10ms
    store2CachePollingScheduler.start(10);
    // 20 threads trying to fetch property from cacheManager
    ExecutorService fetchPropertyService = Executors.newFixedThreadPool(20);
    Callable<Property<?>> callable = new Callable<Property<?>>() {

        @Override
        public Property<?> call() throws Exception {
            try {
                return proxy.getCacheManager().getProperty("a");
            } catch (Exception e) {
                throw e;
            }
        }
    };
    List<Callable<Property<?>>> multiplePropertyFetchCalls = new ArrayList<Callable<Property<?>>>(1000);
    // generating 100000 requests
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 10; j++) {
            // clear to avoid accumulation of callables over multiple iterations
            multiplePropertyFetchCalls.clear();
        }
        for (int k = 0; k < 100; k++) {
            multiplePropertyFetchCalls.add(callable);
        }
        // execute 100 property fetch calls on 20 threads
        List<Future<Property<?>>> fetchPropertyCalls = fetchPropertyService.invokeAll(multiplePropertyFetchCalls);
        // property should never be null
        for (Future<Property<?>> property : fetchPropertyCalls) {
            Assert.assertNotNull(property);
        }
    }
}
Also used : FF4JCacheManager(org.ff4j.cache.FF4JCacheManager) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) InMemoryCacheManager(org.ff4j.cache.InMemoryCacheManager) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) Property(org.ff4j.property.Property) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FeatureStore(org.ff4j.core.FeatureStore) Store2CachePollingScheduler(org.ff4j.cache.Store2CachePollingScheduler) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) PropertyStore(org.ff4j.property.store.PropertyStore) Test(org.junit.Test)

Aggregations

FF4JCacheManager (org.ff4j.cache.FF4JCacheManager)3 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)3 InMemoryCacheManager (org.ff4j.cache.InMemoryCacheManager)3 InMemoryPropertyStore (org.ff4j.property.store.InMemoryPropertyStore)3 InMemoryFeatureStore (org.ff4j.store.InMemoryFeatureStore)3 Test (org.junit.Test)3 Store2CachePollingScheduler (org.ff4j.cache.Store2CachePollingScheduler)2 FeatureStore (org.ff4j.core.FeatureStore)2 PropertyStore (org.ff4j.property.store.PropertyStore)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Feature (org.ff4j.core.Feature)1 Property (org.ff4j.property.Property)1 PropertyString (org.ff4j.property.PropertyString)1