Skip to content

Commit

Permalink
Merge pull request #10 from GoodforGod/dev
Browse files Browse the repository at this point in the history
[0.6.2]
  • Loading branch information
GoodforGod authored Aug 31, 2023
2 parents e4eaf0b + 0a86ae9 commit 0d15c2e
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 84 deletions.
19 changes: 11 additions & 8 deletions cassandra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.1"
testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.2"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-cassandra</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -63,6 +63,7 @@ testImplementation "com.datastax.oss:java-driver-core:4.17.0"
`@TestcontainersCassandra` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:

- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.
Expand Down Expand Up @@ -96,9 +97,10 @@ class ExampleTests {
```

Image syntax:
1) Image can have static value: `cassandra:4.1`
2) Image can be provided via environment variable using syntax: `${MY_IMAGE_ENV}`
3) Image environment variable can have default value if empty using syntax: `${MY_IMAGE_ENV|cassandra:4.1}`

- Image can have static value: `cassandra:4.1`
- Image can be provided via environment variable using syntax: `${MY_IMAGE_ENV}`
- Image environment variable can have default value if empty using syntax: `${MY_IMAGE_ENV|cassandra:4.1}`

### Manual Container

Expand Down Expand Up @@ -155,9 +157,10 @@ class ExampleTests {
```

Image syntax:
1) Image can have static value: `my-alias`
2) Image can be provided via environment variable using syntax: `${MY_ALIAS_ENV}`
3) Image environment variable can have default value if empty using syntax: `${MY_ALIAS_ENV|my-alias-default}`

- Image can have static value: `my-alias`
- Image can be provided via environment variable using syntax: `${MY_ALIAS_ENV}`
- Image environment variable can have default value if empty using syntax: `${MY_ALIAS_ENV|my-alias-default}`

## Container Old Driver

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ protected CassandraContainer<?> getContainerDefault(CassandraMetadata metadata)
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CassandraContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAliasOrDefault()))
.withNetworkAliases(metadata.networkAliasOrDefault())
.waitingFor(Wait.forListeningPort())
.withStartupTimeout(Duration.ofMinutes(5));

container.setNetworkAliases(new ArrayList<>(List.of(metadata.networkAliasOrDefault())));
if (metadata.networkShared()) {
container.withNetwork(Network.SHARED);
}
Expand Down
19 changes: 11 additions & 8 deletions cockroachdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.1"
testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.2"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-cockroachdb</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -62,6 +62,7 @@ testRuntimeOnly "org.postgresql:postgresql:42.6.0"
`@TestcontainersCockroachdb` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:

- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.
Expand Down Expand Up @@ -95,9 +96,10 @@ class ExampleTests {
```

Image syntax:
1) Image can have static value: `cockroachdb/cockroach:latest-v23.1`
2) Image can be provided via environment variable using syntax: `${MY_IMAGE_ENV}`
3) Image environment variable can have default value if empty using syntax: `${MY_IMAGE_ENV|cockroachdb/cockroach:latest-v23.1}`

- Image can have static value: `cockroachdb/cockroach:latest-v23.1`
- Image can be provided via environment variable using syntax: `${MY_IMAGE_ENV}`
- Image environment variable can have default value if empty using syntax: `${MY_IMAGE_ENV|cockroachdb/cockroach:latest-v23.1}`

### Manual Container

Expand Down Expand Up @@ -151,9 +153,10 @@ class ExampleTests {
```

Image syntax:
1) Image can have static value: `my-alias`
2) Image can be provided via environment variable using syntax: `${MY_ALIAS_ENV}`
3) Image environment variable can have default value if empty using syntax: `${MY_ALIAS_ENV|my-alias-default}`

- Image can have static value: `my-alias`
- Image can be provided via environment variable using syntax: `${MY_ALIAS_ENV}`
- Image environment variable can have default value if empty using syntax: `${MY_ALIAS_ENV|my-alias-default}`

## Connection

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.lang.annotation.Annotation;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down Expand Up @@ -51,9 +53,9 @@ protected CockroachContainer getContainerDefault(CockroachMetadata metadata) {
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CockroachContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAliasOrDefault()))
.withNetworkAliases(metadata.networkAliasOrDefault())
.withStartupTimeout(Duration.ofMinutes(5));

container.setNetworkAliases(new ArrayList<>(List.of(metadata.networkAliasOrDefault())));
if (metadata.networkShared()) {
container.withNetwork(Network.SHARED);
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
groupId=io.goodforgod
artifactRootId=testcontainers-extensions
artifactVersion=0.6.1-SNAPSHOT
artifactVersion=0.6.2-SNAPSHOT


##### GRADLE #####
Expand Down
19 changes: 11 additions & 8 deletions kafka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.1"
testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.2"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-kafka</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -65,6 +65,7 @@ testRuntimeOnly "org.apache.kafka:kafka-clients:3.5.1"
`@TestcontainersKafka` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:

- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.
Expand Down Expand Up @@ -98,9 +99,10 @@ class ExampleTests {
```

Image syntax:
1) Image can have static value: `confluentinc/cp-kafka:7.4.1`
2) Image can be provided via environment variable using syntax: `${MY_IMAGE_ENV}`
3) Image environment variable can have default value if empty using syntax: `${MY_IMAGE_ENV|confluentinc/cp-kafka:7.4.1}`

- Image can have static value: `confluentinc/cp-kafka:7.4.1`
- Image can be provided via environment variable using syntax: `${MY_IMAGE_ENV}`
- Image environment variable can have default value if empty using syntax: `${MY_IMAGE_ENV|confluentinc/cp-kafka:7.4.1}`

### Manual Container

Expand Down Expand Up @@ -157,9 +159,10 @@ class ExampleTests {
```

Image syntax:
1) Image can have static value: `my-alias`
2) Image can be provided via environment variable using syntax: `${MY_ALIAS_ENV}`
3) Image environment variable can have default value if empty using syntax: `${MY_ALIAS_ENV|my-alias-default}`

- Image can have static value: `my-alias`
- Image can be provided via environment variable using syntax: `${MY_ALIAS_ENV}`
- Image environment variable can have default value if empty using syntax: `${MY_ALIAS_ENV|my-alias-default}`

### Topics

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,14 @@ static void createTopicsIfNeeded(@NotNull KafkaConnection connection, @NotNull S
}
}

void clear() {
void clearProducer() {
if (producer != null) {
producer.close(Duration.ofMinutes(5));
producer = null;
}
}

void clearConsumer() {
for (var consumer : consumers) {
try {
consumer.close();
Expand All @@ -514,12 +516,8 @@ void clear() {
void close() {
if (!isClosed) {
isClosed = true;
if (producer != null) {
producer.close(Duration.ofMinutes(5));
producer = null;
}

clear();
clearProducer();
clearConsumer();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ private void add(KafkaConnectionImpl connection) {
connections.add(connection);
}

private void clear() {
private void clearProducer() {
for (KafkaConnectionImpl connection : connections) {
try {
connection.clear();
connection.clearProducer();
} catch (Exception e) {
// do nothing
}
}
}

private void clearConsumer() {
for (KafkaConnectionImpl connection : connections) {
try {
connection.clearConsumer();
} catch (Exception e) {
// do nothing
}
Expand Down Expand Up @@ -94,10 +104,11 @@ protected KafkaContainer getContainerDefault(KafkaMetadata metadata) {
.withEnv("KAFKA_CONFLUENT_SUPPORT_METRICS_ENABLE", "false")
.withEnv("AUTO_CREATE_TOPICS", "true")
.withEmbeddedZookeeper()
.withNetworkAliases(metadata.networkAliasOrDefault())
.withExposedPorts(9092, 9093)
.waitingFor(Wait.forListeningPort())
.withStartupTimeout(Duration.ofMinutes(5));

container.setNetworkAliases(new ArrayList<>(List.of(metadata.networkAliasOrDefault())));
if (metadata.networkShared()) {
container.withNetwork(Network.SHARED);
}
Expand Down Expand Up @@ -244,18 +255,30 @@ public void beforeEach(ExtensionContext context) {

@Override
public void afterEach(ExtensionContext context) {
var metadata = getMetadata(context);
var storage = getStorage(context);
var pool = storage.get(KafkaConnectionPool.class, KafkaConnectionPool.class);
pool.clear();
if (metadata.runMode() == ContainerMode.PER_METHOD) {
pool.clearProducer();
pool.clearConsumer();
} else {
pool.clearConsumer();
}

super.afterEach(context);
}

@Override
public void afterAll(ExtensionContext context) {
var metadata = getMetadata(context);
var storage = getStorage(context);
var pool = storage.get(KafkaConnectionPool.class, KafkaConnectionPool.class);
pool.close();
if (metadata.runMode() == ContainerMode.PER_RUN) {
pool.clearProducer();
pool.clearConsumer();
} else {
pool.close();
}

super.afterAll(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.goodforgod.testcontainers.extensions.kafka;

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

import io.goodforgod.testcontainers.extensions.ContainerMode;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestcontainersKafka(mode = ContainerMode.PER_RUN)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class KafkaContainerPerRunFirstTests {

static volatile KafkaConnection perRunConnection;

@ContainerKafkaConnection
private KafkaConnection sameConnection;

@Order(1)
@Test
void firstConnection(@ContainerKafkaConnection KafkaConnection connection) {
assertNotNull(connection);
assertNotNull(connection.params().bootstrapServers());
assertNotNull(sameConnection);
assertEquals(sameConnection, connection);

if (perRunConnection == null) {
perRunConnection = connection;
}

if (KafkaContainerPerRunSecondTests.perRunConnection != null) {
assertEquals(perRunConnection, KafkaContainerPerRunSecondTests.perRunConnection);
}

connection.send("my-topic", Event.ofValue("my-value"));
sameConnection.send("my-topic", Event.ofValue("my-value"));
}

@Order(2)
@Test
void secondConnection(@ContainerKafkaConnection KafkaConnection connection) {
assertNotNull(connection);
assertNotNull(connection.params().bootstrapServers());
assertNotNull(sameConnection);
assertEquals(sameConnection, connection);
assertEquals(perRunConnection, connection);

connection.send("my-topic", Event.ofValue("my-value"));
sameConnection.send("my-topic", Event.ofValue("my-value"));
}
}
Loading

0 comments on commit 0d15c2e

Please sign in to comment.