-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add test to verify that cleanup.policy is set to compact in the KafkaTopicClient #1071
Conversation
This is a follow up to #1042 |
Iterator<NewTopic> firstIterator = topicCollection1.iterator(); | ||
Iterator<NewTopic> secondIterator = topicCollection2.iterator(); | ||
|
||
while (firstIterator.hasNext()) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 ==
?
There was a problem hiding this comment.
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.
There was a problem hiding this 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>> { |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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! ;)
Comparator
s 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);
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
…up.policy to compact correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
f56ca03
to
48bb73c
Compare
No description provided.