Search in sources :

Example 1 with ModuleLifecycle

use of org.infinispan.lifecycle.ModuleLifecycle in project infinispan by infinispan.

the class StartCacheFromListenerTest method testStartSameCache.

public void testStartSameCache() {
    final EmbeddedCacheManager cacheManager = manager(0);
    GlobalComponentRegistry registry = TestingUtil.extractGlobalComponentRegistry(cacheManager);
    List<ModuleLifecycle> lifecycles = new LinkedList<>();
    TestingUtil.replaceField(lifecycles, "moduleLifecycles", registry, GlobalComponentRegistry.class);
    lifecycles.add(new ModuleLifecycle() {

        @Override
        public void cacheStarted(ComponentRegistry cr, String cacheName) {
            Cache cache = cacheManager.getCache("single");
            cache.put("k1", "v1");
        }
    });
    Cache<Object, Object> some = cacheManager.getCache("single");
    some.put("k2", "v2");
    assertEquals("v1", cacheManager.getCache("single").get("k1"));
    assertEquals("v2", cacheManager.getCache("single").get("k2"));
}
Also used : ModuleLifecycle(org.infinispan.lifecycle.ModuleLifecycle) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) LinkedList(java.util.LinkedList) Cache(org.infinispan.Cache)

Example 2 with ModuleLifecycle

use of org.infinispan.lifecycle.ModuleLifecycle in project infinispan by infinispan.

the class TestingUtil method addCacheStartingHook.

/**
 * Add a hook to cache startup sequence that will allow to replace existing component with a mock.
 */
public static void addCacheStartingHook(EmbeddedCacheManager cacheContainer, BiConsumer<String, ComponentRegistry> consumer) {
    GlobalComponentRegistry gcr = extractGlobalComponentRegistry(cacheContainer);
    extractField(gcr, "moduleLifecycles");
    TestingUtil.<Collection<ModuleLifecycle>>replaceField(gcr, "moduleLifecycles", moduleLifecycles -> {
        Collection<ModuleLifecycle> copy = new ArrayList<>(moduleLifecycles);
        copy.add(new ModuleLifecycle() {

            @Override
            public void cacheStarting(ComponentRegistry cr, Configuration configuration, String cacheName) {
                consumer.accept(cacheName, cr);
            }
        });
        return copy;
    });
}
Also used : ModuleLifecycle(org.infinispan.lifecycle.ModuleLifecycle) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) Configuration(org.infinispan.configuration.cache.Configuration) BasicComponentRegistry(org.infinispan.factories.impl.BasicComponentRegistry) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) ArrayList(java.util.ArrayList) Collection(java.util.Collection) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry)

Example 3 with ModuleLifecycle

use of org.infinispan.lifecycle.ModuleLifecycle in project infinispan by infinispan.

the class StartCacheFromListenerTest method testSingleInvocation.

public void testSingleInvocation() {
    final EmbeddedCacheManager cacheManager = manager(0);
    GlobalComponentRegistry registry = TestingUtil.extractGlobalComponentRegistry(cacheManager);
    List<ModuleLifecycle> lifecycles = new LinkedList<>();
    TestingUtil.replaceField(lifecycles, "moduleLifecycles", registry, GlobalComponentRegistry.class);
    lifecycles.add(new ModuleLifecycle() {

        @Override
        public void cacheStarting(ComponentRegistry cr, Configuration configuration, String cacheName) {
            log.debug("StartCacheFromListenerTest.cacheStarting");
            if (!cacheStartingInvoked.get()) {
                cacheStartingInvoked.set(true);
                Future<Cache> fork = fork(() -> {
                    try {
                        return cacheManager.getCache("cacheStarting");
                    } catch (Exception e) {
                        log.error("Got", e);
                        throw e;
                    }
                });
                try {
                    log.debug("About to wait in get");
                    Cache cache = fork.get();
                    cache.put("k", "v");
                    log.debug("returned from get!");
                } catch (InterruptedException e) {
                    log.error("Interrupted while waiting for the cache to start");
                } catch (ExecutionException e) {
                    log.error("Failed to start cache", e);
                }
            }
        }
    });
    log.debug("StartCacheFromListenerTest.testSingleInvocation1");
    Cache<Object, Object> some = cacheManager.getCache("some");
    log.debug("StartCacheFromListenerTest.testSingleInvocation2");
    some.put("k", "v");
    assertEquals("v", cacheManager.getCache("cacheStarting").get("k"));
}
Also used : Configuration(org.infinispan.configuration.cache.Configuration) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) ModuleLifecycle(org.infinispan.lifecycle.ModuleLifecycle) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Cache(org.infinispan.Cache)

Aggregations

ComponentRegistry (org.infinispan.factories.ComponentRegistry)3 GlobalComponentRegistry (org.infinispan.factories.GlobalComponentRegistry)3 ModuleLifecycle (org.infinispan.lifecycle.ModuleLifecycle)3 LinkedList (java.util.LinkedList)2 Cache (org.infinispan.Cache)2 Configuration (org.infinispan.configuration.cache.Configuration)2 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)1 BasicComponentRegistry (org.infinispan.factories.impl.BasicComponentRegistry)1