Search in sources :

Example 1 with ExtendedTransactionListener

use of com.newrelic.agent.ExtendedTransactionListener in project newrelic-java-agent by newrelic.

the class SpringBootTest method testDuplicateTransactions.

@Test
public void testDuplicateTransactions() throws Exception {
    final AtomicInteger txCounter = new AtomicInteger(0);
    final AtomicInteger finishedTxCount = new AtomicInteger(0);
    final AtomicReference<String> finishedTxString = new AtomicReference<>();
    try (ConfigurableApplicationContext context = SpringApplication.run(ArticleResource.class)) {
        ServiceFactory.getTransactionService().addTransactionListener(new ExtendedTransactionListener() {

            @Override
            public void dispatcherTransactionStarted(Transaction transaction) {
                txCounter.incrementAndGet();
            }

            @Override
            public void dispatcherTransactionCancelled(Transaction transaction) {
            // no-op
            }

            @Override
            public void dispatcherTransactionFinished(TransactionData transactionData, TransactionStats transactionStats) {
                txCounter.decrementAndGet();
                if (transactionData.getBlameMetricName().startsWith("OtherTransaction")) {
                    return;
                }
                finishedTxCount.incrementAndGet();
                finishedTxString.set(transactionData.getBlameMetricName());
            }
        });
        int port = (int) context.getBean("port");
        HttpURLConnection connection = (HttpURLConnection) new URL("http", "localhost", port, "/").openConnection();
        int responseCode = connection.getResponseCode();
        assertEquals(200, responseCode);
        // 20 * 250ms = 5 seconds
        int timeout = 10;
        while (timeout > 0 && txCounter.get() > 0) {
            Thread.sleep(250);
            timeout--;
        }
        assertEquals(1, finishedTxCount.get());
        assertEquals("WebTransaction/SpringController/ (GET)", finishedTxString.get());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URL(java.net.URL) ExtendedTransactionListener(com.newrelic.agent.ExtendedTransactionListener) TransactionStats(com.newrelic.agent.stats.TransactionStats) HttpURLConnection(java.net.HttpURLConnection) Transaction(com.newrelic.agent.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Aggregations

ExtendedTransactionListener (com.newrelic.agent.ExtendedTransactionListener)1 Transaction (com.newrelic.agent.Transaction)1 TransactionData (com.newrelic.agent.TransactionData)1 TransactionStats (com.newrelic.agent.stats.TransactionStats)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1