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

Enhance consumer test util #601

Conversation

KartikShrivastava
Copy link
Contributor

@KartikShrivastava KartikShrivastava commented Mar 10, 2024

Resolves #599

Copy link
Contributor

@jonas-grgt jonas-grgt left a comment

Choose a reason for hiding this comment

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

Wonderful to see someone pick up and improve spring-pulsar based on the work I did a couple of weeks back! Way to go @KartikShrivastava 🎉

You did a really nice job and it's definitely a worthwhile contribution. I did raise a couple of questions.

There are also some formatting violations that are easy to fix simply by running ./gradlew format

@@ -85,6 +122,12 @@ public ConditionsSpec<T> awaitAtMost(Duration timeout) {

@Override
public ConditionsSpec<T> until(ConsumedMessagesCondition<T> condition) {
if (calledUntil) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need to introduce a new variable? Is there a reason why we can't rely on this.condition being not being null to verify if until has been called before?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe we do since we do not have to specify a condition (e.g. PCTUT.whenConditionIsNotSpecifiedMessagesAreConsumedUntilAwaitDuration).

I would like to suggest to rename the var to untilMethodAlreadyCalled as the word until in general is a confusing one that makes my brain pause when reading the code "until something happens". It seems nit picky but including the "method" in there helps readability for me anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

untilMethodAlreadyCalled is better than calledUntil imo

Copy link
Collaborator

@onobc onobc left a comment

Choose a reason for hiding this comment

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

@KartikShrivastava, I could not agree w/ @jonas-grgt more on this being a great contribution. Thank you for this.

I also like that you submitted the WIP before investing too much into testing etc..that way we can align on current direction. Good call.

I have left a few comments/suggestions.

Great work.

@@ -85,6 +122,12 @@ public ConditionsSpec<T> awaitAtMost(Duration timeout) {

@Override
public ConditionsSpec<T> until(ConsumedMessagesCondition<T> condition) {
if (calledUntil) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe we do since we do not have to specify a condition (e.g. PCTUT.whenConditionIsNotSpecifiedMessagesAreConsumedUntilAwaitDuration).

I would like to suggest to rename the var to untilMethodAlreadyCalled as the word until in general is a confusing one that makes my brain pause when reading the code "until something happens". It seems nit picky but including the "method" in there helps readability for me anyways.

@KartikShrivastava
Copy link
Contributor Author

Thank you @jonas-grgt and @onobc, for your kind words and detailed suggestion for improvements. Checking them out now

Copy link
Collaborator

@onobc onobc left a comment

Choose a reason for hiding this comment

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

@KartikShrivastava, thanks for the updates. LGTM.

The only remaining piece is to add a few tests to cover the newly added methods.

@KartikShrivastava KartikShrivastava marked this pull request as ready for review March 17, 2024 17:21
void messagesAreConsumedWhenContainerIsStoppedAndConsumeMessagesIsCalledWithoutArguments() {
PulsarTestContainerSupport.stopContainer();
PulsarConsumerTestUtil.<String>consumeMessages();
// TODO: Complete this test
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to request some assistance writing this test

  • Basically I want to test if consumeMessages works properly even if PULSAR_CONTAINER is not running
  • When I try to get() the consumeMessages I get connection refused error for the localhost pulsar client
  • Also I'm not sure at what point should I send messages to the topic, if I do it before stopping the container will it still work? since the pulsarClient instance would be different after container is stopped

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, this one is tricky to test. We actually can't stop the container as the other tests will fail. The "magic" of the PTCS is that it has the @BeforeAll that starts it for the test class. If we stopped it we would have to restart it and then it gets into a bit of flaky-test-land at that point.

I think instead we can static mock the "isContainerStarted" that it uses to decide what to do.

We are releasing 1.1.0-M2 tomorrow so I am going to take what you have in this current state and add a polish commit on top so that we can get this in. I will include this test adjustment in that commit.

Great work @KartikShrivastava !

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, if you are wondering why we did not have (or need) the stop container method annotated w/ a @AfterAll to match the start w/ / @BeforeAll it is because Testcontainers does

At the end of the test suite the Ryuk container that is started by Testcontainers core will take care of stopping the singleton container.

However, I think I will add it in the polish commit to avoid future confusion

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note

Adding the @AfterAll to the new stopContainer method had unintended side-effects when used in a Jupiter @Nested test. I removed the annotation but left the method (9bc6bda)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, thanks for taking this up @onobc, really appreciate that.
I've one minor question seeing the improvements you've made: Is there a recommended way or methodology we follow to name test methods in this project? If so, then I would like to learn/read more about it :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

You are welcome @KartikShrivastava .
In general we follow the "when-THIS-then-OUTCOME" but more than anything the principle I follow is consistency w/in a test suite and enough information in the name to understand what it is testing. I will also typically group tests together using @Nested which groups them and allows us to not have ultra long test names. I was tempted to do some grouping on this test but ended up leaving as is.

I also noticed that the negative test cases I named a bit different than the other ones (they are shorter in nature).

So there is no doc that describes this but it would be a good idea for us to add one. I will note this.

Thanks

@KartikShrivastava
Copy link
Contributor Author

KartikShrivastava commented Mar 17, 2024

Hey folks, I've added the tests for the changes made in PulsarConsumerTestUtil and left few questions I got while testing the new methods
PS: Seems like build failed due to a known flaky test

@onobc
Copy link
Collaborator

onobc commented Mar 17, 2024

Closing via 2c7e3b2

@onobc onobc closed this Mar 17, 2024
@onobc
Copy link
Collaborator

onobc commented Mar 17, 2024

@KartikShrivastava thanks for this great contribution. Take a peek at the follow on commit I did d99101f. You can see how I uses static mocks to test our difficult case. I re-ordered the test methods a bit as well. Great work. Thank you.

@KartikShrivastava KartikShrivastava deleted the enhance-consumer-test-util branch March 18, 2024 16:58
@KartikShrivastava
Copy link
Contributor Author

Thanks @onobc!

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.

Enhance ConsumerTestUtil
3 participants