Search in sources :

Example 11 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TokenTimeoutTest method testTxnAttrTokenTimeout.

@Test
public void testTxnAttrTokenTimeout() throws InterruptedException {
    Transaction.clearTransaction();
    Transaction tx = Transaction.getTransaction();
    Tracer rootTracer = TransactionAsyncUtility.createDispatcherTracer(this, "hi");
    tx.getTransactionActivity().tracerStarted(rootTracer);
    // Name the transaction so we can identify it in the listener below and create an event
    tx.setTransactionName(TransactionNamePriority.CUSTOM_HIGH, true, "TokenTimeout", "timeout");
    final List<TransactionEvent> events = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);
    ServiceFactory.getServiceManager().getTransactionService().addTransactionListener(new TransactionListener() {

        @Override
        public void dispatcherTransactionFinished(TransactionData transactionData, TransactionStats transactionStats) {
            if (transactionData.getPriorityTransactionName().getName().equals("WebTransaction/TokenTimeout/timeout")) {
                events.add(ServiceFactory.getTransactionEventsService().createEvent(transactionData, transactionStats, transactionData.getBlameOrRootMetricName()));
                latch.countDown();
            }
        }
    });
    tx.getTransactionActivity().tracerStarted(rootTracer);
    // Let this timeout the transaction
    Token token = tx.getToken();
    rootTracer.finish(Opcodes.RETURN, 0);
    assertFalse(tx.isFinished());
    // Don't start the thread. The timeout is configured to 0 seconds.
    // Allow it to expire and then run the code that implements it.
    busyWait(1000);
    latch.await();
    assertTrue(tx.isFinished());
    assertFalse(events.isEmpty());
    assertEquals("WebTransaction/TokenTimeout/timeout", events.get(0).getName());
    assertEquals(TimeoutCause.TOKEN, events.get(0).getTimeoutCause());
}
Also used : TransactionListener(com.newrelic.agent.TransactionListener) ExtendedTransactionListener(com.newrelic.agent.ExtendedTransactionListener) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) TransactionStats(com.newrelic.agent.stats.TransactionStats) Transaction(com.newrelic.agent.Transaction) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Token(com.newrelic.agent.bridge.Token) TransactionData(com.newrelic.agent.TransactionData) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 12 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TransactionPropagationTest method testEmptyMonoOnSuccess.

@Test
public void testEmptyMonoOnSuccess() {
    AtomicBoolean hadTransaction = new AtomicBoolean();
    inTransaction(() -> {
        Token token = createToken();
        Mono.empty().subscribeOn(Schedulers.elastic()).doOnSuccess(v -> checkTransaction(hadTransaction)).subscriberContext(with(token)).block();
        token.expire();
    });
    assertCapturedData(hadTransaction);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Token(com.newrelic.agent.bridge.Token) Test(org.junit.Test)

Example 13 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TransactionPropagationTest method testMonoOnSuccess.

@Test
public void testMonoOnSuccess() {
    AtomicBoolean hadTransaction = new AtomicBoolean();
    inTransaction(() -> {
        Token token = createToken();
        Mono.just(A_VALUE).subscribeOn(Schedulers.elastic()).doOnSuccess(v -> checkTransaction(hadTransaction)).subscriberContext(with(token)).block();
        token.expire();
    });
    assertCapturedData(hadTransaction);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Token(com.newrelic.agent.bridge.Token) Test(org.junit.Test)

Example 14 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TransactionPropagationTest method testLambdaMonoSubscriberOnError.

@Test(timeout = 10000L)
public void testLambdaMonoSubscriberOnError() {
    AtomicBoolean hadTransaction = new AtomicBoolean();
    CountDownLatch done = new CountDownLatch(1);
    inTransaction(() -> {
        Token token = createToken();
        Mono.error(new RuntimeException()).subscribeOn(Schedulers.elastic()).doOnError(v -> checkTransaction(hadTransaction)).subscribe(nil(), v -> done.countDown());
        await(done);
        token.expire();
    });
    assertCapturedData(hadTransaction);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TokenLinkingSubscriber(com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber) Trace(com.newrelic.api.agent.Trace) BeforeClass(org.junit.BeforeClass) Token(com.newrelic.agent.bridge.Token) Context(reactor.util.context.Context) RunWith(org.junit.runner.RunWith) Introspector(com.newrelic.agent.introspec.Introspector) Hooks(reactor.core.publisher.Hooks) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) TokenLinkingSubscriber.tokenLift(com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber.tokenLift) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) AgentBridge(com.newrelic.agent.bridge.AgentBridge) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) InstrumentationTestConfig(com.newrelic.agent.introspec.InstrumentationTestConfig) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) InstrumentationTestRunner(com.newrelic.agent.introspec.InstrumentationTestRunner) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Token(com.newrelic.agent.bridge.Token) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TransactionPropagationTest method testLambdaSubscriberOnComplete.

@Test(timeout = 10000L)
public void testLambdaSubscriberOnComplete() {
    AtomicBoolean hadTransaction = new AtomicBoolean();
    CountDownLatch done = new CountDownLatch(1);
    inTransaction(() -> {
        Token token = createToken();
        Flux.empty().subscribeOn(Schedulers.elastic()).doOnComplete(() -> checkTransaction(hadTransaction)).subscribe(nil(), nil(), done::countDown);
        await(done);
        token.expire();
    });
    assertCapturedData(hadTransaction);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Token(com.newrelic.agent.bridge.Token) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Token (com.newrelic.agent.bridge.Token)20 Test (org.junit.Test)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 AgentBridge (com.newrelic.agent.bridge.AgentBridge)8 Trace (com.newrelic.api.agent.Trace)7 InstrumentationTestConfig (com.newrelic.agent.introspec.InstrumentationTestConfig)6 InstrumentationTestRunner (com.newrelic.agent.introspec.InstrumentationTestRunner)6 Introspector (com.newrelic.agent.introspec.Introspector)6 TokenLinkingSubscriber (com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber)6 TokenLinkingSubscriber.tokenLift (com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber.tokenLift)6 Duration (java.time.Duration)6 Consumer (java.util.function.Consumer)6 CoreMatchers.is (org.hamcrest.CoreMatchers.is)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 Matchers (org.hamcrest.Matchers)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 BeforeClass (org.junit.BeforeClass)6 RunWith (org.junit.runner.RunWith)6 Flux (reactor.core.publisher.Flux)6