From 3e2ec51b101081fa832dde8891c9a9b8a9e3e76e Mon Sep 17 00:00:00 2001 From: Nugusbayev Kanagat <48118512+videnkz@users.noreply.github.com> Date: Thu, 16 Nov 2023 20:36:19 +0600 Subject: [PATCH] fixed flaky test SPringAmqpBatchIT - solved problem with bean creation order (#3388) * fixed flaky test SPringAmqpBatchIT - solved problem with bean creation order * add missing file header * remove useless import --------- Co-authored-by: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> --- .../apm/agent/rabbitmq/SpringAmqpBatchIT.java | 10 ++-- .../batch/BatchListenerComponent.java | 53 +++++++++++++++++++ .../rabbitmq/config/BaseConfiguration.java | 1 + .../rabbitmq/config/BatchConfiguration.java | 17 ------ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/components/batch/BatchListenerComponent.java diff --git a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/SpringAmqpBatchIT.java b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/SpringAmqpBatchIT.java index 776a7f786e..d6b00fcfe0 100644 --- a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/SpringAmqpBatchIT.java +++ b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/SpringAmqpBatchIT.java @@ -19,12 +19,12 @@ package co.elastic.apm.agent.rabbitmq; -import co.elastic.apm.agent.tracer.configuration.MessagingConfiguration; import co.elastic.apm.agent.impl.transaction.Span; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; +import co.elastic.apm.agent.rabbitmq.components.batch.BatchListenerComponent; import co.elastic.apm.agent.rabbitmq.config.BatchConfiguration; -import org.junit.Ignore; +import co.elastic.apm.agent.tracer.configuration.MessagingConfiguration; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.core.BatchingRabbitTemplate; @@ -42,10 +42,9 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.Mockito.doReturn; -@Ignore @RunWith(SpringRunner.class) @SpringBootTest -@ContextConfiguration(classes = {BatchConfiguration.class}, initializers = {RabbitMqTestBase.Initializer.class}) +@ContextConfiguration(classes = {BatchConfiguration.class, BatchListenerComponent.class}, initializers = {RabbitMqTestBase.Initializer.class}) public class SpringAmqpBatchIT extends RabbitMqTestBase { @Autowired @@ -158,7 +157,7 @@ public void testTransactionPerBatch() { .filter(span -> Objects.equals(span.getNameAsString(), "RabbitMQ SEND to ")) .collect(Collectors.toList()); assertThat(sendSpans.size()).isEqualTo(2); - sendSpans.forEach(span -> { + sendSpans.forEach(span -> { assertThat(span.getType()).isEqualTo("messaging"); assertThat(span.getTraceContext().getParentId()).isEqualTo(rootTraceTransaction.getTraceContext().getId()); }); @@ -193,3 +192,4 @@ public void testTransactionPerBatch() { rootTraceTransaction.deactivate().end(); } } + diff --git a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/components/batch/BatchListenerComponent.java b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/components/batch/BatchListenerComponent.java new file mode 100644 index 0000000000..90e160aae1 --- /dev/null +++ b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/components/batch/BatchListenerComponent.java @@ -0,0 +1,53 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.rabbitmq.components.batch; + +import co.elastic.apm.agent.rabbitmq.TestConstants; +import co.elastic.apm.agent.sdk.logging.Logger; +import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.api.CaptureSpan; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +import java.util.List; + +import static co.elastic.apm.agent.rabbitmq.TestConstants.QUEUE_NAME; + +@Component +public class BatchListenerComponent { + + public static final Logger logger = LoggerFactory.getLogger(BatchListenerComponent.class); + + @RabbitListener( + queues = TestConstants.QUEUE_NAME, + containerFactory = "simpleRabbitListenerContainerFactory" + ) + public void receiveWorkingBatch(List batchMessages) { + logger.info("Received batch of size {} from '{}'", batchMessages.size(), QUEUE_NAME); + batchMessages.forEach(message -> { + logger.info("Message in 'spring-boot' batch: {}", message.getBody()); + testSpan(); + }); + } + + @CaptureSpan(value = "testSpan", type = "custom", subtype = "anything", action = "test") + public void testSpan() { + } +} diff --git a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BaseConfiguration.java b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BaseConfiguration.java index 9c9f8e9efe..863ec3f7cc 100644 --- a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BaseConfiguration.java +++ b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BaseConfiguration.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; import org.springframework.core.env.Environment; import org.springframework.web.client.RestTemplate; diff --git a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BatchConfiguration.java b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BatchConfiguration.java index 8d8db7c3a4..eef1d4dcaa 100644 --- a/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BatchConfiguration.java +++ b/apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-spring/src/test/java/co/elastic/apm/agent/rabbitmq/config/BatchConfiguration.java @@ -19,13 +19,10 @@ package co.elastic.apm.agent.rabbitmq.config; -import co.elastic.apm.agent.rabbitmq.TestConstants; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; -import org.springframework.amqp.core.Message; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.annotation.EnableRabbit; -import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.batch.SimpleBatchingStrategy; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; @@ -37,8 +34,6 @@ import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import java.util.List; - import static co.elastic.apm.agent.rabbitmq.TestConstants.QUEUE_NAME; @EnableRabbit @@ -83,16 +78,4 @@ public Jackson2JsonMessageConverter converter() { public Queue queue() { return new Queue(QUEUE_NAME, false); } - - @RabbitListener( - queues = TestConstants.QUEUE_NAME, - containerFactory = "simpleRabbitListenerContainerFactory" - ) - public void receiveWorkingBatch(List batchMessages) { - logger.info("Received batch of size {} from '{}'", batchMessages.size(), QUEUE_NAME); - batchMessages.forEach(message -> { - logger.info("Message in 'spring-boot' batch: {}", message.getBody()); - testSpan(); - }); - } }