Search in sources :

Example 6 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InstrumentationTest method testEquals_separateInstanceWithSameArgs.

@Test
void testEquals_separateInstanceWithSameArgs() {
    TestImplementation delegate = new TestImplementation();
    InvocationEventHandler<InvocationContext> handler = TracingInvocationEventHandler.create("test");
    TestInterface proxy0 = Instrumentation.builder(TestInterface.class, delegate).withHandler(handler).build();
    TestInterface proxy1 = Instrumentation.builder(TestInterface.class, delegate).withHandler(handler).build();
    assertThat(proxy0).isNotEqualTo(proxy1);
}
Also used : TestImplementation(com.palantir.tritium.test.TestImplementation) TestInterface(com.palantir.tritium.test.TestInterface) InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Test(org.junit.jupiter.api.Test)

Example 7 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InstrumentationTest method testThrowingHandler_success_single.

@Test
@SuppressWarnings("unchecked")
void testThrowingHandler_success_single() {
    InvocationEventHandler<InvocationContext> handler = Mockito.mock(InvocationEventHandler.class);
    when(handler.isEnabled()).thenReturn(true);
    when(handler.preInvocation(any(), any(), any())).thenThrow(new RuntimeException());
    Runnable wrapped = Instrumentation.builder(Runnable.class, Runnables.doNothing()).withHandler(handler).build();
    assertThatCode(wrapped::run).doesNotThrowAnyException();
    verify(handler).isEnabled();
    verify(handler).preInvocation(any(), any(), any());
    verify(handler).onSuccess(isNull(), any());
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Test(org.junit.jupiter.api.Test)

Example 8 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InstrumentationTest method testThrowingHandler_failure_composite.

@Test
@SuppressWarnings("unchecked")
void testThrowingHandler_failure_composite() {
    InvocationEventHandler<InvocationContext> handler = Mockito.mock(InvocationEventHandler.class);
    when(handler.isEnabled()).thenReturn(true);
    when(handler.preInvocation(any(), any(), any())).thenThrow(new RuntimeException());
    Runnable wrapped = Instrumentation.builder(Runnable.class, () -> {
        throw new SafeRuntimeException("expected");
    }).withHandler(handler).withTaggedMetrics(new DefaultTaggedMetricRegistry()).build();
    assertThatCode(wrapped::run).isExactlyInstanceOf(SafeRuntimeException.class).hasMessage("expected");
    verify(handler).isEnabled();
    verify(handler).preInvocation(any(), any(), any());
    verify(handler).onFailure(isNull(), any());
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Test(org.junit.jupiter.api.Test)

Example 9 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InvocationEventProxyTest method testInstrumentOnSuccessThrows.

@Test
@SuppressWarnings("checkstyle:illegalthrows")
public void testInstrumentOnSuccessThrows() throws Throwable {
    InvocationEventHandler<InvocationContext> testHandler = new SimpleHandler() {

        @Override
        public void onSuccess(@Nullable InvocationContext _context, @Nullable Object _result) {
            throw new IllegalStateException("expected");
        }
    };
    InvocationEventProxy proxy = createTestProxy(testHandler);
    Object result = proxy.invoke(this, getStringLengthMethod(), EMPTY_ARGS);
    assertThat(result).isEqualTo("test".length());
    InvocationContext context = DefaultInvocationContext.of(this, getStringLengthMethod(), null);
    proxy.handleOnSuccess(context, result);
}
Also used : InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Nullable(javax.annotation.Nullable) Test(org.junit.jupiter.api.Test)

Example 10 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class MetricsInvocationEventHandlerTest method testFailure.

@Test
// SortedMap is part of Metrics API
@SuppressWarnings("JdkObsolete")
void testFailure() throws Exception {
    MetricRegistry metricRegistry = new MetricRegistry();
    MetricsInvocationEventHandler handler = new MetricsInvocationEventHandler(metricRegistry, "test");
    InvocationContext context = mock(InvocationContext.class);
    when(context.getMethod()).thenReturn(String.class.getDeclaredMethod("length"));
    assertThat(metricRegistry.getMeters()).doesNotContainKey("failures");
    handler.onFailure(context, new RuntimeException("unexpected"));
    assertThat(metricRegistry.getMeters()).containsKey("failures");
    assertThat(metricRegistry.getMeters().get("failures").getCount()).isOne();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) InvocationContext(com.palantir.tritium.event.InvocationContext) Test(org.junit.jupiter.api.Test)

Aggregations

InvocationContext (com.palantir.tritium.event.InvocationContext)20 Test (org.junit.jupiter.api.Test)16 DefaultInvocationContext (com.palantir.tritium.event.DefaultInvocationContext)14 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)5 Nullable (javax.annotation.Nullable)4 DefaultTaggedMetricRegistry (com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Span (com.palantir.tracing.api.Span)2 TestImplementation (com.palantir.tritium.test.TestImplementation)2 TestInterface (com.palantir.tritium.test.TestInterface)2 Set (java.util.Set)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Throwables (com.google.common.base.Throwables)1 Lists (com.google.common.collect.Lists)1 Goethe (com.palantir.goethe.Goethe)1 GoetheException (com.palantir.goethe.GoetheException)1 Preconditions (com.palantir.logsafe.Preconditions)1 Instrument (com.palantir.tritium.annotations.Instrument)1 InstrumentationBuilder (com.palantir.tritium.annotations.internal.InstrumentationBuilder)1 InstrumentationFilter (com.palantir.tritium.api.event.InstrumentationFilter)1