forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE apache#4384] Add RetryPolicy interface
- Loading branch information
Showing
7 changed files
with
247 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
common/src/main/java/org/apache/rocketmq/common/subscription/RetryPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.common.subscription; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public interface RetryPolicy { | ||
/** | ||
* Compute message's next delay duration by specify reconsumeTimes | ||
* | ||
* @param reconsumeTimes Message reconsumeTimes | ||
* @param timeUnit Given timeUnit | ||
* @return Message's nextDelayDuration in given timeUnit | ||
*/ | ||
long nextDelayDuration(int reconsumeTimes, TimeUnit timeUnit); | ||
} |
44 changes: 44 additions & 0 deletions
44
common/src/test/java/org/apache/rocketmq/common/subscription/CustomizedRetryPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.common.subscription; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CustomizedRetryPolicyTest { | ||
|
||
@Test | ||
public void testNextDelayDuration() { | ||
CustomizedRetryPolicy customizedRetryPolicy = new CustomizedRetryPolicy(); | ||
long actual = customizedRetryPolicy.nextDelayDuration(0, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(10)); | ||
actual = customizedRetryPolicy.nextDelayDuration(10, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.MINUTES.toMillis(9)); | ||
} | ||
|
||
@Test | ||
public void testNextDelayDurationOutOfRange() { | ||
CustomizedRetryPolicy customizedRetryPolicy = new CustomizedRetryPolicy(); | ||
long actual = customizedRetryPolicy.nextDelayDuration(-1, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(10)); | ||
actual = customizedRetryPolicy.nextDelayDuration(100, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.HOURS.toMillis(2)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
common/src/test/java/org/apache/rocketmq/common/subscription/ExponentialRetryPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.common.subscription; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ExponentialRetryPolicyTest { | ||
|
||
@Test | ||
public void testNextDelayDuration() { | ||
ExponentialRetryPolicy exponentialRetryPolicy = new ExponentialRetryPolicy(); | ||
long actual = exponentialRetryPolicy.nextDelayDuration(0, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(5)); | ||
actual = exponentialRetryPolicy.nextDelayDuration(10, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(1024 * 5)); | ||
} | ||
|
||
@Test | ||
public void testNextDelayDurationOutOfRange() { | ||
ExponentialRetryPolicy exponentialRetryPolicy = new ExponentialRetryPolicy(); | ||
long actual = exponentialRetryPolicy.nextDelayDuration(-1, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(5)); | ||
actual = exponentialRetryPolicy.nextDelayDuration(100, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.HOURS.toMillis(2)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
common/src/test/java/org/apache/rocketmq/common/subscription/GroupRetryPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.common.subscription; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GroupRetryPolicyTest { | ||
|
||
@Test | ||
public void testGetRetryPolicy() { | ||
GroupRetryPolicy groupRetryPolicy = new GroupRetryPolicy(); | ||
RetryPolicy retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(CustomizedRetryPolicy.class); | ||
groupRetryPolicy.setType(GroupRetryPolicyType.EXPONENTIAL); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(CustomizedRetryPolicy.class); | ||
|
||
groupRetryPolicy.setType(GroupRetryPolicyType.CUSTOMIZED); | ||
groupRetryPolicy.setCustomizedRetryPolicy(new CustomizedRetryPolicy()); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(CustomizedRetryPolicy.class); | ||
|
||
groupRetryPolicy.setType(GroupRetryPolicyType.EXPONENTIAL); | ||
groupRetryPolicy.setExponentialRetryPolicy(new ExponentialRetryPolicy()); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(ExponentialRetryPolicy.class); | ||
|
||
groupRetryPolicy.setType(null); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(CustomizedRetryPolicy.class); | ||
} | ||
} |