Skip to content

Commit

Permalink
[ISSUE #8090] Optimize isSetEqual for DefaultLitePullConsumerImpl (#8091
Browse files Browse the repository at this point in the history
)

* [ISSUE #8090]1.optimize isSetEqual method and add Unit tests; 2.fix a typo in MQAdminImpl

* remove author message

* Modify code style
  • Loading branch information
Willhow-Gao authored May 8, 2024
1 parent 52c5d89 commit f0c243d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
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,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;


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);
}

}

0 comments on commit f0c243d

Please sign in to comment.