use of org.infinispan.interceptors.BaseAsyncInterceptor 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.BaseAsyncInterceptor 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.BaseAsyncInterceptor in project infinispan by infinispan.
the class PutForExternalReadTest method testKeyOnlyWrittenOnceOnOriginator.
// This test executes PFER on cache1, and expects that it will be relayed to cache2 == primary
// and then sent to cache1 again for backup. In scattered cache there's only one RPC.
@InCacheMode({ CacheMode.DIST_SYNC, CacheMode.REPL_SYNC })
public void testKeyOnlyWrittenOnceOnOriginator() throws Exception {
final Cache<MagicKey, String> cache1 = cache(0, CACHE_NAME);
final Cache<MagicKey, String> cache2 = cache(1, CACHE_NAME);
final CyclicBarrier barrier = new CyclicBarrier(2);
cache1.getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
if (command instanceof PutKeyValueCommand) {
if (!ctx.isOriginLocal()) {
// wait first before the check
TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
// and once more after the check
TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
}
}
return invokeNext(ctx, command);
}
}, 0);
final MagicKey myKey = new MagicKey(cache2);
cache1.putForExternalRead(myKey, value);
// Verify that the key was not written on the origin by the time it was looped back
barrier.await(10, TimeUnit.SECONDS);
assertNull(cache1.get(myKey));
// Verify that the key is written on the origin afterwards
barrier.await(10, TimeUnit.SECONDS);
eventually(() -> value.equals(cache1.get(myKey)) && value.equals(cache2.get(myKey)));
}
use of org.infinispan.interceptors.BaseAsyncInterceptor in project infinispan by infinispan.
the class AsyncInterceptorChainInvocationTest method testCompletedStage.
public void testCompletedStage() {
AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return "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.BaseAsyncInterceptor in project infinispan by infinispan.
the class AsyncInterceptorChainInvocationTest method testInvokeNextAsync.
public void testInvokeNextAsync() throws Exception {
CompletableFuture<Object> f = new CompletableFuture<>();
AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return asyncInvokeNext(ctx, command, f);
}
}, new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
return "v1";
}
});
InvocationContext context = newInvocationContext();
CompletableFuture<Object> invokeFuture = chain.invokeAsync(context, testCommand);
assertFalse(invokeFuture.isDone());
f.complete("v");
assertEquals("v1", invokeFuture.get(10, SECONDS));
}
Aggregations