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