提交 edde487e 编写于 作者: Y yukon

[ROCKETMQ-52] Add unit tests for Validators and ThreadLocalIndex

上级 4587775c
......@@ -38,5 +38,9 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
......@@ -118,23 +118,23 @@ public class Validators {
*/
public static void checkTopic(String topic) throws MQClientException {
if (UtilAll.isBlank(topic)) {
throw new MQClientException("the specified topic is blank", null);
throw new MQClientException("The specified topic is blank", null);
}
if (!regularExpressionMatcher(topic, PATTERN)) {
throw new MQClientException(String.format(
"the specified topic[%s] contains illegal characters, allowing only %s", topic,
"The specified topic[%s] contains illegal characters, allowing only %s", topic,
VALID_PATTERN_STR), null);
}
if (topic.length() > CHARACTER_MAX_LENGTH) {
throw new MQClientException("the specified topic is longer than topic max length 255.", null);
throw new MQClientException("The specified topic is longer than topic max length 255.", null);
}
//whether the same with system reserved keyword
if (topic.equals(MixAll.DEFAULT_TOPIC)) {
throw new MQClientException(
String.format("the topic[%s] is conflict with default topic.", topic), null);
String.format("The topic[%s] is conflict with default topic.", topic), null);
}
}
}
......@@ -23,10 +23,6 @@ public class ThreadLocalIndex {
private final ThreadLocal<Integer> threadLocalIndex = new ThreadLocal<Integer>();
private final Random random = new Random();
public ThreadLocalIndex(int value) {
}
public int getAndIncrement() {
Integer index = this.threadLocalIndex.get();
if (null == index) {
......
......@@ -27,7 +27,7 @@ public class TopicPublishInfo {
private boolean orderTopic = false;
private boolean haveTopicRouterInfo = false;
private List<MessageQueue> messageQueueList = new ArrayList<MessageQueue>();
private volatile ThreadLocalIndex sendWhichQueue = new ThreadLocalIndex(0);
private volatile ThreadLocalIndex sendWhichQueue = new ThreadLocalIndex();
private TopicRouteData topicRouteData;
public boolean isOrderTopic() {
......
......@@ -27,7 +27,7 @@ import org.apache.rocketmq.client.common.ThreadLocalIndex;
public class LatencyFaultToleranceImpl implements LatencyFaultTolerance<String> {
private final ConcurrentHashMap<String, FaultItem> faultItemTable = new ConcurrentHashMap<String, FaultItem>(16);
private final ThreadLocalIndex whichItemWorst = new ThreadLocalIndex(0);
private final ThreadLocalIndex whichItemWorst = new ThreadLocalIndex();
@Override
public void updateFaultItem(final String name, final long currentLatency, final long notAvailableDuration) {
......
......@@ -17,17 +17,67 @@
package org.apache.rocketmq.client;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.MixAll;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown;
public class ValidatorsTest {
@Test
public void topicValidatorTest() throws MQClientException {
public void testCheckTopic_Success() throws MQClientException {
Validators.checkTopic("Hello");
Validators.checkTopic("%RETRY%Hello");
Validators.checkTopic("_%RETRY%Hello");
Validators.checkTopic("-%RETRY%Hello");
Validators.checkTopic("223-%RETRY%Hello");
}
@Test
public void testCheckTopic_HasIllegalCharacters() {
String illegalTopic = "TOPIC&*^";
try {
Validators.checkTopic(illegalTopic);
failBecauseExceptionWasNotThrown(MQClientException.class);
} catch (MQClientException e) {
assertThat(e).hasMessageStartingWith(String.format("The specified topic[%s] contains illegal characters, allowing only %s", illegalTopic, Validators.VALID_PATTERN_STR));
}
}
@Test
public void testCheckTopic_UseDefaultTopic() {
String defaultTopic = MixAll.DEFAULT_TOPIC;
try {
Validators.checkTopic(defaultTopic);
failBecauseExceptionWasNotThrown(MQClientException.class);
} catch (MQClientException e) {
assertThat(e).hasMessageStartingWith(String.format("The topic[%s] is conflict with default topic.", defaultTopic));
}
}
@Test
public void testCheckTopic_BlankTopic() {
String blankTopic = "";
try {
Validators.checkTopic(blankTopic);
failBecauseExceptionWasNotThrown(MQClientException.class);
} catch (MQClientException e) {
assertThat(e).hasMessageStartingWith("The specified topic is blank");
}
}
@Test
public void testCheckTopic_TooLongTopic() {
String tooLongTopic = StringUtils.rightPad("TooLongTopic", Validators.CHARACTER_MAX_LENGTH + 1, "_");
assertThat(tooLongTopic.length()).isGreaterThan(Validators.CHARACTER_MAX_LENGTH);
try {
Validators.checkTopic(tooLongTopic);
failBecauseExceptionWasNotThrown(MQClientException.class);
} catch (MQClientException e) {
assertThat(e).hasMessageStartingWith("The specified topic is longer than topic max length 255.");
}
}
}
/*
* 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.common;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ThreadLocalIndexTest {
@Test
public void getAndIncrement() throws Exception {
ThreadLocalIndex localIndex = new ThreadLocalIndex();
int initialVal = localIndex.getAndIncrement();
assertThat(localIndex.getAndIncrement()).isEqualTo(initialVal + 1);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册