Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve system test stability #1013

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;

import java.util.Set;
Expand All @@ -32,7 +31,6 @@
TestConditionalComponent.class,
TestOtherConditionalComponent.class
})
@DirtiesContext
class ComponentClassScannerIntegrationTest {
@MockBean
private AsyncApiDocketService asyncApiDocketService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.TestPropertySource;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;
Expand All @@ -41,12 +42,11 @@
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@Testcontainers
@DirtiesContext
@TestMethodOrder(OrderAnnotation.class)
@TestPropertySource(properties = {"spring.rabbitmq.host=localhost"})
@Slf4j
// @Ignore("Uncomment this line if you have issues running this test on your local machine.")
public class ProducerSystemTest {
public class AmqpProducerSystemTest {
private static final String AMQP_NAME = "amqp";

@Autowired
Expand All @@ -59,9 +59,16 @@ public class ProducerSystemTest {
public static DockerComposeContainer<?> environment = new DockerComposeContainer<>(new File("docker-compose.yml"))
.withCopyFilesInContainer(".env") // do not copy all files in the directory
.withServices(AMQP_NAME)
.withExposedService(AMQP_NAME, 5672)
.waitingFor(AMQP_NAME, Wait.forLogMessage(".*Server startup complete.*", 1))
.withLogConsumer(AMQP_NAME, l -> log.debug("amqp: {}", l.getUtf8StringWithoutLineEnding()));

@DynamicPropertySource
static void registerActiveMqBroker(DynamicPropertyRegistry registry) {
registry.add("spring.rabbitmq.host", () -> environment.getServiceHost(AMQP_NAME, 5672));
registry.add("spring.rabbitmq.port", () -> environment.getServicePort(AMQP_NAME, 5672));
}

@Test
@Order(1)
void verifyAmqpIsAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -20,10 +20,8 @@
@SpringBootTest(
classes = {SpringwolfCloudstreamExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EmbeddedKafka(
partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:9095", "port=9095"})
@DirtiesContext
@EmbeddedKafka
@TestPropertySource(properties = {"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}"})
public class ApiIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -17,13 +16,11 @@
public class SpringContextIntegrationTest {

@SpringBootTest(classes = SpringwolfCloudstreamExampleApplication.class)
@EmbeddedKafka(
partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:9095", "port=9095"})
@EmbeddedKafka
@Nested
@DirtiesContext
@TestPropertySource(
properties = {
"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
"springwolf.enabled=true",
"springwolf.docket.info.title=Info title was loaded from spring properties",
"springwolf.docket.info.version=1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(
classes = {SpringwolfCloudstreamExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EmbeddedKafka(
partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:9095", "port=9095"})
@DirtiesContext
@EmbeddedKafka
@TestPropertySource(properties = {"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}"})
class SpringwolfCloudstreamExampleApplicationIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
import io.github.springwolf.examples.jms.dtos.ExamplePayloadDto;
import io.github.springwolf.plugins.jms.producer.SpringwolfJmsProducer;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.org.awaitility.Awaitility;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.github.springwolf.examples.jms.dtos.ExamplePayloadDto.ExampleEnum.FOO1;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;

/**
Expand All @@ -33,11 +34,9 @@
classes = {SpringwolfJmsExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
@DirtiesContext
@TestMethodOrder(OrderAnnotation.class)
@Slf4j
// @Ignore("Uncomment this line if you have issues running this test on your local machine.")
public class ProducerSystemTest {
public class JmsProducerSystemTest {
private static final String APP_JMS = "activemq";

@Autowired
Expand All @@ -46,27 +45,42 @@ public class ProducerSystemTest {
@SpyBean
ExampleConsumer exampleConsumer;

@Value("${spring.activemq.broker-url}")
String brokerUrl;

@Container
public static DockerComposeContainer<?> environment = new DockerComposeContainer<>(new File("docker-compose.yml"))
.withCopyFilesInContainer(".env") // do not copy all files in the directory
.withServices(APP_JMS)
.withExposedService(APP_JMS, 61616)
.withLogConsumer(APP_JMS, l -> log.debug("jms: {}", l.getUtf8StringWithoutLineEnding()))
.waitingFor(APP_JMS, Wait.forLogMessage(".*Artemis Console available.*", 1));

@DynamicPropertySource
static void registerActiveMqBroker(DynamicPropertyRegistry registry) {
registry.add(
"spring.activemq.broker-url",
() -> String.format(
"tcp://%s:%s",
environment.getServiceHost(APP_JMS, 61616), environment.getServicePort(APP_JMS, 61616)));
}

@Test
@Order(2)
void producerCanUseSpringwolfConfigurationToSendMessage() {
// given
ExamplePayloadDto payload = new ExamplePayloadDto();
payload.setSomeString("foo");
payload.setSomeLong(5);
payload.setSomeEnum(FOO1);

// when
springwolfJmsProducer.send("example-queue", Map.of(), payload);
// Awaitility is used, because message sent before amqp is ready are lost
Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
// when
log.info("Waiting for message in {} on {}", exampleConsumer, brokerUrl);
springwolfJmsProducer.send("example-queue", Map.of(), payload);

// then
// Increased timeout once from 10s to 20s to fix flaky test in ci
verify(exampleConsumer, timeout(20000)).receiveExamplePayload(payload);
// then
verify(exampleConsumer, atLeastOnce()).receiveExamplePayload(payload);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -20,8 +18,6 @@
@SpringBootTest(
classes = {SpringwolfKafkaExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EmbeddedKafka(partitions = 1)
@DirtiesContext
class ApiIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -20,11 +19,11 @@
classes = {SpringwolfKafkaExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
"springwolf.endpoint.actuator.enabled=true",
"management.endpoints.web.exposure.include=springwolf"
})
@EmbeddedKafka(partitions = 1)
@DirtiesContext
@EmbeddedKafka
public class ApiIntegrationWithActuatorIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
@TestMethodOrder(OrderAnnotation.class)
@Slf4j
// @Ignore("Uncomment this line if you have issues running this test on your local machine.")
public class ProducerSystemTest {
public class KafkaProducerSystemTest {
private static final String KAFKA_NAME = "kafka";

private static final boolean USE_SCHEMA_REGISTRY = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -17,13 +16,11 @@
public class SpringContextIntegrationTest {

@SpringBootTest(classes = SpringwolfKafkaExampleApplication.class)
@EmbeddedKafka(
partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:9092", "port=9092"})
@EmbeddedKafka
@Nested
@DirtiesContext
@TestPropertySource(
properties = {
"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
"springwolf.enabled=true",
"springwolf.docket.info.title=Info title was loaded from spring properties",
"springwolf.docket.info.version=1.0.0",
Expand Down Expand Up @@ -66,13 +63,11 @@ void testAllChannelsAreFound() {
}

@SpringBootTest(classes = SpringwolfKafkaExampleApplication.class)
@EmbeddedKafka(
partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:9092", "port=9092"})
@EmbeddedKafka
@Nested
@DirtiesContext
@TestPropertySource(
properties = {
"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
"springwolf.scanner.async-listener.enabled=false",
"springwolf.scanner.async-publisher.enabled=false",
"springwolf.scanner.consumer-data.enabled=false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(
classes = {SpringwolfKafkaExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EmbeddedKafka(
partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:9092", "port=9092"})
@DirtiesContext
@EmbeddedKafka
@TestPropertySource(properties = {"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}"})
class SpringwolfKafkaExampleApplicationIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -35,10 +34,9 @@
classes = {SpringwolfSqsExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
@DirtiesContext
@Slf4j
// @Ignore("Uncomment this line if you have issues running this test on your local machine.")
public class ProducerSystemTest {
public class SqsProducerSystemTest {
private static final String LOCALSTACK_NAME = "localstack";

@Autowired
Expand Down
Loading