[ROCKETMQ-52] Add unit tests for LatencyFaultTolerance
Project: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/commit/e06a26ce Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/tree/e06a26ce Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/diff/e06a26ce Branch: refs/heads/ROCKETMQ-57 Commit: e06a26cef72948603b54299f8a234faf468fc2c0 Parents: bbcb69e Author: yukon <yu...@apache.org> Authored: Tue Jan 17 22:03:07 2017 +0800 Committer: yukon <yu...@apache.org> Committed: Thu Jan 19 15:45:19 2017 +0800 ---------------------------------------------------------------------- .../latency/LatencyFaultToleranceImplTest.java | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/e06a26ce/client/src/test/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImplTest.java ---------------------------------------------------------------------- diff --git a/client/src/test/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImplTest.java b/client/src/test/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImplTest.java new file mode 100644 index 0000000..86690e4 --- /dev/null +++ b/client/src/test/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImplTest.java @@ -0,0 +1,67 @@ +/* + * 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.latency; + +import java.util.concurrent.TimeUnit; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LatencyFaultToleranceImplTest { + private LatencyFaultTolerance<String> latencyFaultTolerance; + private String brokerName = "BrokerA"; + private String anotherBrokerName = "BrokerB"; + + @Before + public void init() { + latencyFaultTolerance = new LatencyFaultToleranceImpl(); + } + + @Test + public void testUpdateFaultItem() throws Exception { + latencyFaultTolerance.updateFaultItem(brokerName, 3000, 3000); + assertThat(latencyFaultTolerance.isAvailable(brokerName)).isFalse(); + assertThat(latencyFaultTolerance.isAvailable(anotherBrokerName)).isTrue(); + } + + @Test + public void testIsAvailable() throws Exception { + latencyFaultTolerance.updateFaultItem(brokerName, 3000, 50); + assertThat(latencyFaultTolerance.isAvailable(brokerName)).isFalse(); + + TimeUnit.MILLISECONDS.sleep(70); + assertThat(latencyFaultTolerance.isAvailable(brokerName)).isTrue(); + } + + @Test + public void testRemove() throws Exception { + latencyFaultTolerance.updateFaultItem(brokerName, 3000, 3000); + assertThat(latencyFaultTolerance.isAvailable(brokerName)).isFalse(); + latencyFaultTolerance.remove(brokerName); + assertThat(latencyFaultTolerance.isAvailable(brokerName)).isTrue(); + } + + @Test + public void testPickOneAtLeast() throws Exception { + latencyFaultTolerance.updateFaultItem(brokerName, 1000, 3000); + assertThat(latencyFaultTolerance.pickOneAtLeast()).isEqualTo(brokerName); + + latencyFaultTolerance.updateFaultItem(anotherBrokerName, 1001, 3000); + assertThat(latencyFaultTolerance.pickOneAtLeast()).isEqualTo(brokerName); + } +} \ No newline at end of file