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

[ISSUE #8090] Optimize isSetEqual for DefaultLitePullConsumerImpl #8091

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Set<MessageQueue> fetchSubscribeMessageQueues(String topic) throws MQClie
e);
}

throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
throw new MQClientException("Unknown why, Can not find Message Queue for this topic, " + topic, null);
}

public long searchOffset(MessageQueue mq, long timestamp) throws MQClientException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,18 +1237,16 @@ private boolean isSetEqual(Set<MessageQueue> set1, Set<MessageQueue> set2) {
return true;
}

if (set1 == null || set2 == null || set1.size() != set2.size() || set1.size() == 0) {
if (set1 == null || set2 == null || set1.size() != set2.size()) {
return false;
}

Iterator<MessageQueue> iter = set2.iterator();
boolean isEqual = true;
while (iter.hasNext()) {
if (!set1.contains(iter.next())) {
isEqual = false;
for (MessageQueue messageQueue : set2) {
if (!set1.contains(messageQueue)) {
return false;
}
}
return isEqual;
return true;
}

public AssignedMessageQueue getAssignedMessageQueue() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.apache.rocketmq.client.impl.consumer;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.common.message.MessageQueue;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
* @author Willhow
* @since 2024/5/3
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Author information needs to be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

作者信息需要删除

already removed. Also revised code style and added license comments

public class DefaultLitePullConsumerImplTest {
private final DefaultLitePullConsumerImpl consumer = new DefaultLitePullConsumerImpl(new DefaultLitePullConsumer(), null);

private static Method isSetEqualMethod;

@BeforeClass
public static void initReflectionMethod() throws NoSuchMethodException {
Class<DefaultLitePullConsumerImpl> consumerClass = DefaultLitePullConsumerImpl.class;
Method testMethod = consumerClass.getDeclaredMethod("isSetEqual", Set.class, Set.class);
testMethod.setAccessible(true);
isSetEqualMethod = testMethod;
}


/**
* The two empty sets should be equal
*/
@Test
public void testIsSetEqual1() throws InvocationTargetException, IllegalAccessException {
Set<MessageQueue> set1 = new HashSet<>();
Set<MessageQueue> set2 = new HashSet<>();
boolean equalResult = (boolean) isSetEqualMethod.invoke(consumer, set1, set2);
Assert.assertTrue(equalResult);
}


/**
* When a set has elements and one does not, the two sets are not equal
*/
@Test
public void testIsSetEqual2() throws InvocationTargetException, IllegalAccessException {
Set<MessageQueue> set1 = new HashSet<>();
set1.add(new MessageQueue("testTopic","testBroker",111));
Set<MessageQueue> set2 = new HashSet<>();
boolean equalResult = (boolean) isSetEqualMethod.invoke(consumer, set1, set2);
Assert.assertFalse(equalResult);
}

/**
* The two null sets should be equal
*/
@Test
public void testIsSetEqual3() throws InvocationTargetException, IllegalAccessException{
Set<MessageQueue> set1 = null;
Set<MessageQueue> set2 = null;
boolean equalResult = (boolean) isSetEqualMethod.invoke(consumer, set1, set2);
Assert.assertTrue(equalResult);
}

@Test
public void testIsSetEqual4() throws InvocationTargetException, IllegalAccessException{
Set<MessageQueue> set1 = null;
Set<MessageQueue> set2 = new HashSet<>();
boolean equalResult = (boolean) isSetEqualMethod.invoke(consumer, set1, set2);
Assert.assertFalse(equalResult);
}

@Test
public void testIsSetEqual5() throws InvocationTargetException, IllegalAccessException{
Set<MessageQueue> set1 = new HashSet<>();
set1.add(new MessageQueue("testTopic","testBroker",111));
Set<MessageQueue> set2 = new HashSet<>();
set2.add(new MessageQueue("testTopic","testBroker",111));
boolean equalResult = (boolean) isSetEqualMethod.invoke(consumer, set1, set2);
Assert.assertTrue(equalResult);
}

}
Loading