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

Example to test actual implementation with TopologyTestDriver #219

Merged

Conversation

jukkakarvanen
Copy link
Contributor

Readme states: "These examples are also a good starting point to learn how to implement your own end-to-end integration tests.". I disagree. See testing instructions:
https://docs.confluent.io/current/streams/developer-guide/test-streams.html

Now the Integration Tests are mainly copy/paste of actual implementation and not testing actual implementation class at all. The most of the test could also be done without EmbeddedKafka with TopologyTestDriver.

In this pull request there is a new WordCountLambdaExampleTest added utilizing TopologyTestDriver.
To be able to test WordCountLambdaExample implementation of WordCountLambdaExample main method needed to be refactored, so actual configuration and Stream building are own methods.

WordCountLambdaIntegrationTest class is also modified to utilize Stream logic of actual application and that way avoid code duplication.

The same logic should be applied also to other tests.

@ghost
Copy link

ghost commented Apr 2, 2019

It looks like @jukkakarvanen hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement here.

Once you've signed reply with [clabot:check] to prove it.

Appreciation of efforts,

clabot

@jukkakarvanen
Copy link
Contributor Author

[clabot:check]

@ghost
Copy link

ghost commented Apr 2, 2019

@confluentinc It looks like @jukkakarvanen just signed our Contributor License Agreement. 👍

Always at your service,

clabot

@jukkakarvanen jukkakarvanen changed the title Example for actual implementaion test with TopologyTestDriver Example to test actual implementation with TopologyTestDriver Apr 3, 2019
Copy link
Contributor

@bbejeck bbejeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @jukkakarvanen! Over this looks good I just have a few minor comments.

README.md Outdated
that demonstrate end-to-end data pipelines. Here, we use a testing framework to automatically spawn embedded Kafka
* **Examples under [src/test/](src/test/)**: These examples should test applications under [src/main/](src/main/).
Unit Tests with TopologyTestDriver test the stream logic without external system dependencies.
The integration tests use a testing framework to automatically spawn embedded Kafka
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: integration tests use a testing framework to automatically spawn -> integration tests use an embedded Kafka

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

private ConsumerRecordFactory<String, String> recordFactory = new ConsumerRecordFactory<>(new StringSerializer(), new StringSerializer());

@Before
public void setup() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove the throws clause and all exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/**
* Stream processing unit test of {@link WordCountLambdaExample}, using TopologyTestDriver.
*
* @author Jukka Karvanen / jukinimi.com
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we usually don't have any author names in the javadoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

}


/** Read one Record from output topic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move the text one line below the start of the javadoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return testDriver.readOutput(WordCountLambdaExample.outputTopic, stringDeserializer, longDeserializer);
}

/** Read counts from output to map.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here and elsewhere below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/
package io.confluent.examples.streams;

import org.apache.kafka.clients.producer.ProducerRecord;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove wildcard imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, Changed IDEA settings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -111,8 +111,48 @@
*/
public class WordCountLambdaExample {

static final String inputTopic = "inputTopic";
static final String outputTopic = "outputTopic";

public static void main(final String[] args) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can rid of throws Exception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed (Old problem,)

@@ -111,8 +111,48 @@
*/
public class WordCountLambdaExample {

static final String inputTopic = "inputTopic";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These topic names need to match the names of the directions in the javadoc
inputTopic= streams-plaintext-input
outputTopic = streams-wordcount-output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem was inherited from WordCountLambdaIntegrationTest.java

Copy link
Member

@vvcephei vvcephei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jukkakarvanen ,

You have an excellent point. I'm wondering if we should even keep the integration test at all. The application logic of the WordCountExample depends purely on Streams; it has no logical interactions with the broker. Streams already guarantees that it works with the broker, so it seems like the only necessary test is to verify the application logic, which is better done with the TopologyTestDriver.

What do you think about removing the integration test entirely?

(the code looks good to me, by the way)

@jukkakarvanen
Copy link
Contributor Author

No idea why previous test failed due to WordCountScalaIntegrationTest failure and now it is successful again.

@vvcephei
Copy link
Member

These integration tests are quite flaky, which is part of my motivation to just switch to TopologyTestDriver.

@jukkakarvanen
Copy link
Contributor Author

You have an excellent point. I'm wondering if we should even keep the integration test at all. The application logic of the WordCountExample depends purely on Streams; it has no logical interactions with the broker. Streams already guarantees that it works with the broker, so it seems like the only necessary test is to verify the application logic, which is better done with the TopologyTestDriver.

What do you think about removing the integration test entirely?

@vvcephei, Yes, I don't see the point of the this kind of integration test with EmbeddedKafka at all, if you want to test Kafka Stream application logic. I understand the use case of EmbeddedSingleNodeKafkaCluster if the application contains also consumers or producers, but not in pure Kafka Streams.

Maybe somebody knowing the internals of the TopologyTestDriver could comment is there possibility of difference, but I have found TopologyTestDriver a fast and reliable way of the testing the Kafka Stream application.

@mjsax
Copy link
Member

mjsax commented Apr 14, 2019

It's historical reasons. When the examples where created, there was no TopologyTestDriver yet. And we never invested time to rewrite the tests later.

@jukkakarvanen
Copy link
Contributor Author

FYI: There are PR apache/kafka#6569 to some point of time get rid of try / catch in tearDown, I needed to add because I am working with Windows.

I also working to propose some extra Helper class to make simplify the use of TopologyTestDriver to be added kafka-streams-test-utils (and possible independent package for old Kafka versions)

I added an example of how those class could be used in this WordCountLambdaExampleTest in separate brach of my repo: jukkakarvanen/kafka-streams-examples@TopologyTestDriver_tests...jukkakarvanen:InputOutputTopic

@vvcephei
Copy link
Member

Thanks @jukkakarvanen ,

It sounds like we can actually go ahead and just get rid of the WordCountScalaIntegrationTest and just use your new test instead.

Thanks also for those other PRs. I'd say, we should just consider them all independently. If we merge the other ones first, we can update this PR, but if we merge this PR first, we can always use a new PR to improve the test further.

@bbejeck
Copy link
Contributor

bbejeck commented Apr 16, 2019

@jukkakarvanen thanks for the contribution!
To follow up on @vvcephei's comment If we can get rid of the WordCountScalaIntegrationTest in this PR then we can just the new test instead

@jukkakarvanen
Copy link
Contributor Author

@bbejeck WordCountScalaIntegrationTest class and references to it removed

@bbejeck
Copy link
Contributor

bbejeck commented Apr 17, 2019

I pulled down the PR and verified the WordCountLambdaExample

@bbejeck bbejeck merged commit 5f331ca into confluentinc:5.2.0-post Apr 17, 2019
@bbejeck
Copy link
Contributor

bbejeck commented Apr 17, 2019

Thanks @jukkakarvanen !

@bbejeck
Copy link
Contributor

bbejeck commented Apr 17, 2019

merged with pint

@mjsax
Copy link
Member

mjsax commented Apr 18, 2019

Sorry that I am late to the game. TopologyTestDriver was introduced in AK 1.1, thus I am wondering if this should have gone to 4.1.0-post? Maybe we can still backport it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants