use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.
the class AsyncInterceptorChainInvocationTest method testComposeSync.
public void testComposeSync() {
AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return invokeNextAndHandle(ctx, command, (rCtx, rCommand, rv, t) -> "v1");
}
}, new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return "v2";
}
});
InvocationContext context = newInvocationContext();
Object returnValue = chain.invoke(context, testCommand);
assertEquals("v1", returnValue);
}
use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.
the class AsyncInterceptorChainInvocationTest method testAsyncInvocationManyHandlersSyncException.
public void testAsyncInvocationManyHandlersSyncException() throws Exception {
sideEffects.set("");
CompletableFuture<Object> f = CompletableFutures.completedExceptionFuture(new TestException(""));
AsyncInterceptorChain chain = makeChainWithManyHandlers(f);
CompletableFuture<Object> invokeFuture = chain.invokeAsync(newInvocationContext(), testCommand);
assertExceptionHandlers(invokeFuture);
}
use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.
the class AsyncInterceptorChainInvocationTest method testAsyncStageComposeAsyncStage.
public void testAsyncStageComposeAsyncStage() throws Exception {
CompletableFuture<Object> f1 = new CompletableFuture<>();
CompletableFuture<Object> f2 = new CompletableFuture<>();
CompletableFuture<Object> f3 = new CompletableFuture<>();
AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return invokeNextAndHandle(ctx, command, (rCtx, rCommand, rv, t) -> {
InvocationSuccessFunction function = (rCtx1, rCommand1, rv1) -> asyncValue(f3);
return asyncValue(f2).addCallback(rCtx, rCommand, function);
});
}
}, new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return asyncValue(f1);
}
});
InvocationContext context = newInvocationContext();
CompletableFuture<Object> invokeFuture = chain.invokeAsync(context, testCommand);
assertFalse(invokeFuture.isDone());
f1.complete("v1");
assertFalse(invokeFuture.isDone());
f2.complete("v2");
assertFalse(invokeFuture.isDone());
f3.complete("v3");
assertEquals("v3", invokeFuture.get(10, SECONDS));
}
use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.
the class VersionedTest method testCollectionUpdate.
@Test
public void testCollectionUpdate() throws Exception {
// the first insert puts VersionedEntry(null, null, timestamp), so we have to wait a while to cache the entry
TIME_SERVICE.advance(1);
withTxSession(s -> {
Item item = s.load(Item.class, itemId);
OtherItem otherItem = new OtherItem();
otherItem.setName("Other 1");
s.persist(otherItem);
item.addOtherItem(otherItem);
});
withTxSession(s -> {
Item item = s.load(Item.class, itemId);
Set<OtherItem> otherItems = item.getOtherItems();
assertFalse(otherItems.isEmpty());
otherItems.remove(otherItems.iterator().next());
});
AdvancedCache collectionCache = TEST_SESSION_ACCESS.getRegion(sessionFactory(), Item.class.getName() + ".otherItems").getCache();
CountDownLatch putFromLoadLatch = new CountDownLatch(1);
AtomicBoolean committing = new AtomicBoolean(false);
CollectionUpdateTestInterceptor collectionUpdateTestInterceptor = new CollectionUpdateTestInterceptor(putFromLoadLatch);
AnotherCollectionUpdateTestInterceptor anotherInterceptor = new AnotherCollectionUpdateTestInterceptor(putFromLoadLatch, committing);
AsyncInterceptorChain interceptorChain = collectionCache.getAsyncInterceptorChain();
interceptorChain.addInterceptorBefore(collectionUpdateTestInterceptor, CallInterceptor.class);
interceptorChain.addInterceptor(anotherInterceptor, 0);
TIME_SERVICE.advance(1);
Future<Boolean> addFuture = executor.submit(() -> withTxSessionApply(s -> {
awaitOrThrow(collectionUpdateTestInterceptor.updateLatch);
Item item = s.load(Item.class, itemId);
OtherItem otherItem = new OtherItem();
otherItem.setName("Other 2");
s.persist(otherItem);
item.addOtherItem(otherItem);
committing.set(true);
return true;
}));
Future<Boolean> readFuture = executor.submit(() -> withTxSessionApply(s -> {
Item item = s.load(Item.class, itemId);
assertTrue(item.getOtherItems().isEmpty());
return true;
}));
addFuture.get();
readFuture.get();
interceptorChain.removeInterceptor(CollectionUpdateTestInterceptor.class);
interceptorChain.removeInterceptor(AnotherCollectionUpdateTestInterceptor.class);
withTxSession(s -> assertFalse(s.load(Item.class, itemId).getOtherItems().isEmpty()));
}
use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.
the class BackupWriteCommand method invokeAsync.
@Override
public final CompletionStage<?> invokeAsync(ComponentRegistry componentRegistry) {
WriteCommand command = createWriteCommand();
if (command == null) {
// No-op command
return CompletableFutures.completedNull();
}
command.init(componentRegistry);
command.setFlagsBitSet(flags);
// Mark the command as a backup write and skip locking
command.addFlags(FlagBitSets.SKIP_LOCKING | FlagBitSets.BACKUP_WRITE);
command.setValueMatcher(MATCH_ALWAYS);
command.setTopologyId(topologyId);
InvocationContextFactory invocationContextFactory = componentRegistry.getInvocationContextFactory().running();
InvocationContext invocationContext = invocationContextFactory.createRemoteInvocationContextForCommand(command, getOrigin());
AsyncInterceptorChain interceptorChain = componentRegistry.getInterceptorChain().running();
return interceptorChain.invokeAsync(invocationContext, command);
}
Aggregations