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

Add test to verify that cleanup.policy is set to compact in the KafkaTopicClient #1071

Conversation

apurvam
Copy link
Contributor

@apurvam apurvam commented Mar 27, 2018

No description provided.

@apurvam apurvam requested a review from a team March 27, 2018 18:41
@apurvam
Copy link
Contributor Author

apurvam commented Mar 27, 2018

This is a follow up to #1042

Iterator<NewTopic> firstIterator = topicCollection1.iterator();
Iterator<NewTopic> secondIterator = topicCollection2.iterator();

while (firstIterator.hasNext()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The order of topics may not be the same in the two collections.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. But for now, we only are testing with lists of single size, since our API creates one topic at a time. So it isn't an issue IMO.

NewTopic topic2 = secondIterator.next();
if (!(topic1.name().equals(topic2.name())
&& topic1.replicationFactor() == topic2.replicationFactor()
&& topic1.numPartitions() == topic2.numPartitions()
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these be != instead of ==?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. The ! is over the whole condition. So everything has to be equal.

Copy link
Contributor

@big-andy-coates big-andy-coates left a comment

Choose a reason for hiding this comment

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

Hey @apurvam - I've suggested a cleaner way of testing this than a custom comparator. See what you think.

// Compares two collections of 'kafka.clients.admin.NewTopic' objects to verify that they are
// the same. The NewTopic.equals method isn't implemented so it just does reference equality,
// which is not what we want in these tests.
class NewTopicComparator implements Comparator<Collection<NewTopic>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

private static class, and could we move this to the bottom of the file please?

while (firstIterator.hasNext()) {
NewTopic topic1 = firstIterator.next();
NewTopic topic2 = secondIterator.next();
if (!(topic1.name().equals(topic2.name())
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a pretty hacky comparator impl! ;)

Comparators are all about sorting collections, which is not what you want to achieve here. Why not just use a custom argument matcher that does what you want? e.g.

private static Collection<NewTopic> singleNewTopic(final NewTopic expected) {
    class NewTopicsMatcher implements IArgumentMatcher {
      @SuppressWarnings("unchecked")
      @Override
      public boolean matches(final Object argument) {
        final Collection<NewTopic> newTopics = (Collection<NewTopic>) argument;
        if (newTopics.size() != 1) {
          return false;
        }

        final NewTopic actual = newTopics.iterator().next();
        return Objects.equals(actual.name(), expected.name())
               && Objects.equals(actual.replicationFactor(), expected.replicationFactor())
               && Objects.equals(actual.numPartitions(), expected.numPartitions())
               && Objects.equals(actual.configs(), expected.configs());
      }

      @Override
      public void appendTo(final StringBuffer buffer) {
        buffer.append("{NewTopic").append(expected).append("}");
      }
    }

    EasyMock.reportMatcher(new NewTopicsMatcher());
    return null;
  }

Then you can change the test to:

  @Test
  public void shouldSetTopicCleanupPolicyToCompact()  {
    AdminClient adminClient = mock(AdminClient.class);
    expect(adminClient.describeCluster()).andReturn(getDescribeClusterResult());
    expect(adminClient.listTopics()).andReturn(getEmptyListTopicResult());
    expect(adminClient.describeConfigs(anyObject())).andReturn(getDescribeConfigsResult());

    // Verify that the new topic configuration being passed to the admin client is what we expect.
    NewTopic newTopic = new NewTopic(topicName1, 1, (short) 1);
    newTopic.configs(Collections.singletonMap("cleanup.policy", "compact"));
  
    expect(adminClient.createTopics(singleNewTopic(newTopic))).andReturn(getCreateTopicsResult());
    replay(adminClient);
    
    KafkaTopicClient kafkaTopicClient = new KafkaTopicClientImpl(adminClient);
    kafkaTopicClient.createTopic(topicName1, 1, (short) 1, true);
    verify(adminClient);
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @big-andy-coates . One of the motivations for doing this was to learn about how to do this nicely with EasyMock. Thanks for the tip! I implemented your changes.

Copy link
Contributor

@hjafarpour hjafarpour left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@rodesai rodesai left a comment

Choose a reason for hiding this comment

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

LGTM!

@apurvam apurvam force-pushed the add-unit-test-case-to-check-that-compacted-topics-are-created branch from f56ca03 to 48bb73c Compare March 30, 2018 05:53
@apurvam apurvam merged commit e6df77d into confluentinc:master Mar 30, 2018
@apurvam apurvam deleted the add-unit-test-case-to-check-that-compacted-topics-are-created branch March 30, 2018 06:26
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