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

Improve MockProducerFactory usability by wrapping MockProducer with … #2961

Closed
wants to merge 6 commits into from

Conversation

pszymczyk
Copy link
Contributor

@pszymczyk pszymczyk commented Dec 18, 2023

…CloseSafeMockProducer

Hi

In the current project, I've opted to use the MockProducer class and Spring support in the form of MockProducerFactory. Unfortunately, it turns out that after the first message is sent, KafkaTemplate by default closes the provided MockProducer instance, making it impossible to send another message. I suggest a solution in which the factory wraps the MockProducer in a class that ignores the producer's closing.

Best regards,
Pawel

@sobychacko
Copy link
Contributor

@sobychacko
Copy link
Contributor

Build is still failing.

@pszymczyk
Copy link
Contributor Author

pszymczyk commented Dec 18, 2023

I've executed ./gradlew idea and then executed reformat from Intellij Idea and it's still failing, to be honest I don't know how to fix it.

@sobychacko
Copy link
Contributor

@pszymczyk
Copy link
Contributor Author

I see:

 Error: [Task :spring-kafka:checkstyleMain] [ImportOrder] Wrong order for 'java.time.Duration' import.
Error: [Task :spring-kafka:checkstyleMain] [RegexpSinglelineJava] Line has leading space characters; indentation should be performed with tabs only.

@pszymczyk
Copy link
Contributor Author

Sorry but I have no idea what is wrong with following imports order:

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.function.BiFunction;
import java.util.function.Supplier;

@pszymczyk pszymczyk closed this Dec 19, 2023
@sobychacko
Copy link
Contributor

sobychacko commented Dec 20, 2023

@pszymczyk As a solution to this problem, in your test, you can extend the MockProducer and override the close method. Then provide an instance of this implementation to the MockProducerFactory supplier. Did you try that option?

Discussed this with @artembilan. KafkaTemplate is not the right component to make these decisions about whether the producer needs to be closed or not. It should be the producer's responsibility to do that, and the solution we suggested above does that. Therefore, we suggest using this approach in your testing scenario.

We will update the testing chapter in the reference docs with these details.

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.

3 participants