Search in sources :

Example 1 with FaultToleranceConfig

use of fish.payara.microprofile.faulttolerance.FaultToleranceConfig in project Payara by payara.

the class ConfigOverrideTest method assertOverridden.

private <T, A extends Annotation> void assertOverridden(Class<A> annotationType, String propertyName, T annotated, T overridden, BiFunction<FaultToleranceConfig, A, T> property) {
    Method annotatedMethod = getAnnotatedMethod();
    FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
    A annotation = config.getAnnotation(annotationType);
    // check we get the expected annotated value
    T actual = property.apply(config, annotation);
    assertEqualValue(annotated, actual);
    // make the override
    overrides.override(annotatedMethod, annotationType, propertyName, overridden);
    // now check that we get the expected overridden value
    actual = property.apply(config, annotation);
    assertEqualValue(overridden, actual);
}
Also used : StaticAnalysisContext(fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext) FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) TestUtils.getAnnotatedMethod(fish.payara.microprofile.faulttolerance.test.TestUtils.getAnnotatedMethod) Method(java.lang.reflect.Method)

Example 2 with FaultToleranceConfig

use of fish.payara.microprofile.faulttolerance.FaultToleranceConfig in project Payara by payara.

the class FaultToleranceInterceptor method intercept.

public Object intercept(InvocationContext context) throws Exception {
    if (!shouldIntercept(context)) {
        return context.proceed();
    }
    context.getContextData().put(PAYARA_FAULT_TOLERANCE_INTERCEPTOR_EXECUTED, Boolean.TRUE);
    try {
        initialize();
        AtomicReference<FaultToleranceConfig> lazyConfig = new AtomicReference<>();
        Supplier<FaultToleranceConfig> configSupplier = () -> lazyConfig.updateAndGet(value -> value != null ? value : faultToleranceService.getConfig(context, this));
        FaultTolerancePolicy policy = FaultTolerancePolicy.get(context, configSupplier);
        if (policy.isPresent) {
            return policy.proceed(context, () -> faultToleranceService.getMethodContext(context, policy, getRequestContextController()));
        }
    } catch (FaultToleranceDefinitionException e) {
        logger.log(Level.SEVERE, "Effective FT policy contains illegal values, fault tolerance cannot be applied," + " falling back to plain method invocation.", e);
    // fall-through to normal proceed
    }
    return context.proceed();
}
Also used : FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) AtomicReference(java.util.concurrent.atomic.AtomicReference) FaultToleranceDefinitionException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException) FaultTolerancePolicy(fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy)

Example 3 with FaultToleranceConfig

use of fish.payara.microprofile.faulttolerance.FaultToleranceConfig in project Payara by payara.

the class ConfigOverridePriorityTest method enabledIsIndependentOfPresentAnnotations.

@Test
public void enabledIsIndependentOfPresentAnnotations() {
    Method annotatedMethod = TestUtils.getAnnotatedMethod();
    FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
    assertTrue("should be: present annotation is enabled by default", config.isEnabled(Asynchronous.class));
    assertTrue("should be: not present annotation is enabled by default", config.isEnabled(Retry.class));
    overrides.override(Asynchronous.class, "enabled", false);
    overrides.override(Retry.class, "enabled", false);
    assertFalse("should be: global override for present annotation has effect", config.isEnabled(Asynchronous.class));
    assertFalse("should be: global override for not present annotation has effect", config.isEnabled(Retry.class));
    overrides.override(getClass(), Asynchronous.class, "enabled", true);
    overrides.override(getClass(), Retry.class, "enabled", true);
    assertTrue("should be: class level override for present annotation has effect", config.isEnabled(Asynchronous.class));
    assertTrue("should be: class level override for not present annotation has effect", config.isEnabled(Retry.class));
    overrides.override(annotatedMethod, Asynchronous.class, "enabled", false);
    overrides.override(annotatedMethod, Retry.class, "enabled", false);
    assertFalse("should be: method level override for present annotation has effect", config.isEnabled(Asynchronous.class));
    assertFalse("should be: method level override for not present annotation has effect", config.isEnabled(Retry.class));
}
Also used : Asynchronous(org.eclipse.microprofile.faulttolerance.Asynchronous) StaticAnalysisContext(fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext) FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) Method(java.lang.reflect.Method) Retry(org.eclipse.microprofile.faulttolerance.Retry) Test(org.junit.Test)

Example 4 with FaultToleranceConfig

use of fish.payara.microprofile.faulttolerance.FaultToleranceConfig in project Payara by payara.

the class ConfigOverridePriorityTest method onlyClassLevelAnnotationPresent.

@Test
public void onlyClassLevelAnnotationPresent() {
    Method annotatedMethod = TestUtils.getAnnotatedMethod();
    FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
    Retry annotation = config.getAnnotation(Retry.class);
    assertEquals(ChronoUnit.MINUTES, config.delayUnit(annotation));
    overrides.override(Retry.class, "delayUnit", ChronoUnit.CENTURIES);
    assertEquals("should be: global override effective", ChronoUnit.CENTURIES, config.delayUnit(annotation));
    overrides.override(getClass(), Retry.class, "delayUnit", ChronoUnit.DAYS);
    assertEquals("should be: class level override effective because annotation present on class level", ChronoUnit.DAYS, config.delayUnit(annotation));
    overrides.override(annotatedMethod, Retry.class, "delayUnit", ChronoUnit.HOURS);
    assertEquals("should be: method level override ineffective because no method level annotation present", ChronoUnit.DAYS, config.delayUnit(annotation));
}
Also used : StaticAnalysisContext(fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext) FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) Method(java.lang.reflect.Method) Retry(org.eclipse.microprofile.faulttolerance.Retry) Test(org.junit.Test)

Example 5 with FaultToleranceConfig

use of fish.payara.microprofile.faulttolerance.FaultToleranceConfig in project Payara by payara.

the class ConfigScopeTest method isNonFallbackEnabledOnlyResolvedOnce.

@Test
public void isNonFallbackEnabledOnlyResolvedOnce() {
    FaultToleranceConfig config = new BindableFaultToleranceConfig(overrides, null);
    assertTrue("default should be true", config.isNonFallbackEnabled());
    overrides.override(BindableFaultToleranceConfig.NON_FALLBACK_ENABLED_PROPERTY, false);
    assertTrue("should still be true since it has been resolved already", config.isNonFallbackEnabled());
    assertFalse("should be false since property was set before it was resolved", new BindableFaultToleranceConfig(overrides, null).isNonFallbackEnabled());
}
Also used : FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) Test(org.junit.Test)

Aggregations

FaultToleranceConfig (fish.payara.microprofile.faulttolerance.FaultToleranceConfig)8 Test (org.junit.Test)6 StaticAnalysisContext (fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext)5 Method (java.lang.reflect.Method)5 Retry (org.eclipse.microprofile.faulttolerance.Retry)3 FaultTolerancePolicy (fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy)1 TestUtils.getAnnotatedMethod (fish.payara.microprofile.faulttolerance.test.TestUtils.getAnnotatedMethod)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Asynchronous (org.eclipse.microprofile.faulttolerance.Asynchronous)1 Timeout (org.eclipse.microprofile.faulttolerance.Timeout)1 FaultToleranceDefinitionException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)1