-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support running integration tests against real kafka (#1779)
* feat: support running integration tests against real kafka * docs: add reference to support of kafka * chore: add ci configs and remove old config files
- Loading branch information
Showing
12 changed files
with
202 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
version: '2.0' | ||
|
||
services: | ||
kafka: | ||
image: confluentinc/cp-kafka:7.2.6 | ||
depends_on: | ||
- zookeeper | ||
ports: | ||
- 9092:9092 # used when running clients from containerized Kalix proxy | ||
- 9093:9093 # used when running non-docker clients from host | ||
environment: | ||
KAFKA_BROKER_ID: 1 | ||
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 | ||
# advertised listener running on port 9092 must be accessible to kalix-proxy container running for integration tests | ||
# therefore we need to use host.testcontainers.internal (testcontainers's bridge address) to access it through the host machine | ||
# advertised listener running on port 9093 is to be used by non-docker clients running on host machine | ||
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:29092,EXTERNAL_DOCKER://host.testcontainers.internal:9092,HOST://localhost:9093 | ||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL_DOCKER:PLAINTEXT,HOST:PLAINTEXT | ||
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL | ||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
zookeeper: | ||
image: zookeeper:3.9 | ||
ports: | ||
- "2181:2181" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
samples/java-protobuf-customer-registry-kafka-quickstart/my-dev.kafka.properties
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
samples/java-protobuf-customer-registry-kafka-quickstart/my-local.kafka.properties
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...try-kafka-quickstart/src/it/java/customer/api/CustomerActionWithKafkaIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package customer.api; | ||
|
||
import com.google.protobuf.Empty; | ||
import customer.Main; | ||
import kalix.javasdk.testkit.KalixTestKit; | ||
import kalix.javasdk.testkit.junit.KalixTestKitResource; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.serialization.ByteArrayDeserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.*; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.testcontainers.shaded.org.awaitility.Awaitility.await; | ||
|
||
// This class was initially generated based on the .proto definition by Kalix tooling. | ||
// | ||
// As long as this file exists it will not be overwritten: you can maintain it yourself, | ||
// or delete it so it is regenerated as needed. | ||
|
||
// Example of an integration test calling our service via the Kalix proxy | ||
// IMPORTANT: this tests depends on an external kafka instance. Make sure to have it running | ||
// Run all test classes ending with "IntegrationTest" using `mvn verify -Pit` | ||
public class CustomerActionWithKafkaIntegrationTest { | ||
|
||
/** | ||
* The test kit starts both the service container and the Kalix proxy. | ||
*/ | ||
@ClassRule | ||
public static final KalixTestKitResource testKit = | ||
new KalixTestKitResource(Main.createKalix(), | ||
KalixTestKit.Settings.DEFAULT.withEventingSupport(KalixTestKit.Settings.EventingSupport.KAFKA)); | ||
|
||
/** | ||
* Use the generated gRPC client to call the service through the Kalix proxy. | ||
*/ | ||
private final CustomerService client; | ||
|
||
|
||
public CustomerActionWithKafkaIntegrationTest() { | ||
client = testKit.getGrpcClient(CustomerService.class); | ||
} | ||
|
||
@Test | ||
public void createAndPublish() throws Exception { | ||
var id = UUID.randomUUID().toString(); | ||
var customer = buildCustomer(id, "Johanna", "[email protected]", "Porto", "Long Road"); | ||
createCustomer(customer); | ||
|
||
//change customer name | ||
var completeName = "Johanna Doe"; | ||
client.changeName(CustomerApi.ChangeNameRequest.newBuilder() | ||
.setCustomerId(id) | ||
.setNewName(completeName) | ||
.build()) | ||
.toCompletableFuture() | ||
.get(5, SECONDS); | ||
|
||
|
||
// Example Kafka consumer configuration | ||
Properties props = new Properties(); | ||
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "0.0.0.0:9093"); | ||
props.put(ConsumerConfig.GROUP_ID_CONFIG, "it-test-" + System.currentTimeMillis()); // using new consumer group every run | ||
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); | ||
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class.getName()); | ||
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
|
||
// Create Kafka consumer and subscribe to topic | ||
KafkaConsumer<String, byte[]> consumer = new KafkaConsumer<>(props); | ||
consumer.subscribe(Collections.singletonList("customer_changes")); | ||
|
||
await() | ||
.ignoreExceptions() | ||
.atMost(20, TimeUnit.of(ChronoUnit.SECONDS)) | ||
.untilAsserted(() -> { | ||
ConsumerRecords<String, byte[]> records = consumer.poll(Duration.ofMillis(200)); | ||
var foundRecord = false; | ||
for (ConsumerRecord<String, byte[]> r : records) { | ||
var customerState = CustomerApi.Customer.parseFrom(r.value()); | ||
if (completeName.equals(customerState.getName())) { | ||
foundRecord = true; | ||
break; | ||
} | ||
} | ||
|
||
assertTrue(foundRecord); | ||
}); | ||
} | ||
|
||
private CustomerApi.Customer buildCustomer(String id, String name, String email, String city, String street) { | ||
return CustomerApi.Customer.newBuilder() | ||
.setCustomerId(id) | ||
.setName(name) | ||
.setEmail(email) | ||
.setAddress(CustomerApi.Address.newBuilder() | ||
.setCity(city) | ||
.setStreet(street) | ||
.build()) | ||
.build(); | ||
} | ||
private Empty createCustomer(CustomerApi.Customer toCreate) throws ExecutionException, InterruptedException, TimeoutException { | ||
return client.create(toCreate) | ||
.toCompletableFuture() | ||
.get(5, SECONDS); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters