Skip to content

Commit

Permalink
Update README (#27)
Browse files Browse the repository at this point in the history
* Tweak top level readme

* Tweak top level readme

* Add readme with examples to Kafka-JUnit-Core

* Add readme with examples to Kafka-JUnit-Core

* Add readme with examples to Kafka-JUnit-Core
  • Loading branch information
Crim committed Sep 26, 2018
1 parent 8205ff8 commit a91f945
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 7 deletions.
18 changes: 11 additions & 7 deletions README.md
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/salesforce/kafka-junit.svg?branch=master)](https://travis-ci.org/salesforce/kafka-junit)

This library wraps Apache Kafka's [KafkaServerStartable](https://github.com/apache/kafka/blob/1.1/core/src/main/scala/kafka/server/KafkaServerStartable.scala#L32) class and allows you to easily create and run tests against
one or more "real" kafka brokers running within your tests. No longer do you need to setup and coordinate with an external kafka cluster for your tests!
one or more "real" kafka brokers. No longer do you need to setup and coordinate with an external kafka cluster for your tests! The library transparently supports running a single or multi-broker cluster. Running a multi-broker cluster allows you to validate how your software reacts under various error scenarios, such as when one or more brokers become unavailable.

Currently the library supports Kafka versions 2.0.x, 1.1.x, 1.0.x, and 0.11.0.x.

Expand All @@ -15,12 +15,10 @@ Please review [Kafka-JUnit4 Readme](kafka-junit4/) for usage instructions with J

Please review [Kafka-JUnit5 Readme](kafka-junit5/) for usage instructions with JUnit5.

## Changelog
## Using Kafka-JUnit-Core.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
For use cases where you want to embed Kafka broker/cluster within your existing software, you can make use of the core package directly. Please review [Kafka-JUnit-Core Readme](kafka-junit-core/) for usage instructions.

[View Changelog](CHANGELOG.md)

# Contributing

Expand All @@ -45,7 +43,6 @@ We love contributions, but it's important that your pull request adhere to some
- All tests must be passing!
- All code changes require tests!
- All code changes must be consistent with our checkstyle rules.
- New configuration options should have proper annotations and README updates generated.
- Great inline comments.

# Other Notes
Expand All @@ -64,7 +61,14 @@ Steps for proper release:
- Deploy to Maven Central: `mvn clean deploy -P release-kafka-junit`
- Create release on Github project.

## Changelog

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

[View Changelog](CHANGELOG.md)

## License

[View License](LICENSE.txt)
BSD 3-Clause [View License](LICENSE.txt).

146 changes: 146 additions & 0 deletions kafka-junit-core/README.md
@@ -0,0 +1,146 @@
# Kafka-JUnit-Core

This library wraps Apache Kafka's [KafkaServerStartable](https://github.com/apache/kafka/blob/1.1/core/src/main/scala/kafka/server/KafkaServerStartable.scala#L32) class and allows you to embed a single or multi-broker Kafka cluster within your application. It's original
purpose was to support [Kafka-JUnit4](../kafka-junit4) and [Kafka-JUnit5](../kafka-junit5), but it could be useful for other situations.

The most obvious use case would be for starting an embedded Kafka cluster within your application. In development environments
for applications that depend on a multi-broker Kafka cluster, you could potentially avoid the need for a developer to manage and configure the service. It should be noted
that other tools exist that may solve this problem better, such as Docker.

## Example 1: Starting a 3-node test cluster as a command line application.

```java
import com.salesforce.kafka.test.KafkaTestCluster;
import com.salesforce.kafka.test.KafkaTestUtils;

/**
* Simplified example.
*/
public class TestClusterApplication {
/**
* Entry point for example.
* @param args command line arguments.
*/
public static void main(String[] args) throws Exception {
// Right now we accept one parameter, the number of nodes in the cluster.
final int clusterSize;
if (args.length > 0) {
// Pull in cluster size.
clusterSize = Integer.parseInt(args[0]);
} else {
// If no argument, default to cluster size of 1.
clusterSize = 1;
}

System.out.println("Starting up kafka cluster with " + clusterSize + " brokers");

// Create a test cluster
final KafkaTestCluster kafkaTestCluster = new KafkaTestCluster(clusterSize);

// Start the cluster.
kafkaTestCluster.start();

// Create a topic
final String topicName = "TestTopicA";
final KafkaTestUtils utils = new KafkaTestUtils(kafkaTestCluster);
utils.createTopic(topicName, clusterSize, (short) clusterSize);

// Publish some data into that topic
for (int partition = 0; partition < clusterSize; partition++) {
utils.produceRecords(1000, topicName, partition);
}

kafkaTestCluster
.getKafkaBrokers()
.stream()
.forEach((broker) -> System.out.println(
"Started broker with Id " + broker.getBrokerId() + " at " + broker.getConnectString()
));

System.out.println("Cluster started at: " + kafkaTestCluster.getKafkaConnectString());

try {
// Wait forever.
Thread
.currentThread()
.join();
} finally {
// On interrupt, shutdown the cluster.
System.out.println("Shutting down cluster...");
kafkaTestCluster.close();
}
}
}
```

### Example 2: Starting a 3-node test cluster when SpringBoot application starts.

```java
import com.salesforce.kafka.test.KafkaTestCluster;
import com.salesforce.kafka.test.KafkaTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
* Example for starting an embedded Kafka Cluster when SpringBoot application starts.
*
* Typically you'd only fire this in development or test environments.
*/
@Component
public final class KafkaTestClusterRunner implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(KafkaTestClusterRunner.class);
private KafkaTestCluster kafkaTestCluster = null;

/**
* Run on startup.
*/
@Override
public void run(final ApplicationArguments args) throws Exception {
startKafkaService(3);
}

/**
* Creates default admin user if none exists.
*/
private void startKafkaService(final int clusterSize) {
kafkaTestCluster = new KafkaTestCluster(clusterSize);

// Start the cluster.
kafkaTestCluster.start();

// Create default data
createDefaultTopic("TestTopicA", clusterSize, (short) clusterSize);
createDefaultTopic("TestTopicB", clusterSize, (short) clusterSize);

// Log details about the cluster
logger.info("Cluster started at: {}", kafkaTestCluster.getKafkaConnectString());
}

/**
* Create topics with test data.
*/
private void createDefaultTopic(final String topicName, final int partitions, final short replicaFactor) {
// Create a topic
final KafkaTestUtils utils = new KafkaTestUtils(kafkaTestCluster);
utils.createTopic(
topicName,
kafkaTestCluster,
(short) clusterSize
);

// Publish some data into that topic
for (int partition = 0; partition < clusterSize; partition++) {
utils.produceRecords(1000, topicName, partition);
}
}

/**
* @return Kafka Test Cluster.
*/
public KafkaTestCluster getKafkaTestCluster() {
return this.kafkaTestCluster;
}
}
```

0 comments on commit a91f945

Please sign in to comment.