Skip to content

Commit f609320

Browse files
authored
Fix NPE in RetryParameters and add unit tests to ExecuteActivityParameters (#946)
1 parent 48f66e6 commit f609320

File tree

3 files changed

+244
-2
lines changed

3 files changed

+244
-2
lines changed

src/main/java/com/uber/cadence/internal/common/RetryParameters.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ public List<String> getNonRetriableErrorReasons() {
9494
}
9595

9696
public void setNonRetriableErrorReasons(List<String> nonRetriableErrorReasons) {
97-
this.nonRetriableErrorReasons = nonRetriableErrorReasons;
97+
if (nonRetriableErrorReasons == null) {
98+
this.nonRetriableErrorReasons = null;
99+
} else {
100+
this.nonRetriableErrorReasons = nonRetriableErrorReasons;
101+
}
98102
}
99103

100104
public int getExpirationIntervalInSeconds() {
@@ -108,7 +112,11 @@ public void setExpirationIntervalInSeconds(int expirationIntervalInSeconds) {
108112
public RetryParameters copy() {
109113
RetryParameters result = new RetryParameters();
110114
result.setMaximumIntervalInSeconds(maximumIntervalInSeconds);
111-
result.setNonRetriableErrorReasons(new ImmutableList<>(nonRetriableErrorReasons));
115+
if (nonRetriableErrorReasons != null) {
116+
result.setNonRetriableErrorReasons(new ImmutableList<>(nonRetriableErrorReasons));
117+
} else {
118+
result.setNonRetriableErrorReasons(null);
119+
}
112120
result.setInitialIntervalInSeconds(initialIntervalInSeconds);
113121
result.setMaximumAttempts(maximumAttempts);
114122
result.setExpirationIntervalInSeconds(expirationIntervalInSeconds);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
7+
* except in compliance with the License. A copy of the License is located at
8+
*
9+
* <p>http://aws.amazon.com/apache2.0
10+
*
11+
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
* specific language governing permissions and limitations under the License.
14+
*/
15+
package com.uber.cadence.internal.common;
16+
17+
import static junit.framework.TestCase.assertEquals;
18+
import static org.junit.Assert.*;
19+
20+
import java.util.Arrays;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
public class RetryParametersTest {
25+
26+
private RetryParameters original;
27+
28+
@Before
29+
public void setUp() {
30+
original = new RetryParameters();
31+
original.setMaximumIntervalInSeconds(120);
32+
original.setInitialIntervalInSeconds(10);
33+
original.setMaximumAttempts(5);
34+
original.setExpirationIntervalInSeconds(600);
35+
original.setBackoffCoefficient(2.0);
36+
original.setNonRetriableErrorReasons(Arrays.asList("Error1", "Error2"));
37+
}
38+
39+
@Test
40+
public void testCopy() {
41+
RetryParameters copy = original.copy();
42+
43+
// Verify that all properties are copied correctly
44+
assertEquals(original.getMaximumIntervalInSeconds(), copy.getMaximumIntervalInSeconds());
45+
assertEquals(original.getInitialIntervalInSeconds(), copy.getInitialIntervalInSeconds());
46+
assertEquals(original.getMaximumAttempts(), copy.getMaximumAttempts());
47+
assertEquals(original.getExpirationIntervalInSeconds(), copy.getExpirationIntervalInSeconds());
48+
assertEquals(original.getBackoffCoefficient(), copy.getBackoffCoefficient(), 0.0);
49+
assertEquals(original.getNonRetriableErrorReasons(), copy.getNonRetriableErrorReasons());
50+
51+
// Verify that nonRetriableErrorReasons is deeply copied
52+
assertNotSame(original.getNonRetriableErrorReasons(), copy.getNonRetriableErrorReasons());
53+
}
54+
55+
@Test
56+
public void testCopyWithNullNonRetriableErrorReasons() {
57+
original.setNonRetriableErrorReasons(null);
58+
59+
RetryParameters copy = original.copy();
60+
61+
// Check that properties are copied as expected
62+
assertNull(copy.getNonRetriableErrorReasons());
63+
assertEquals(original.getMaximumIntervalInSeconds(), copy.getMaximumIntervalInSeconds());
64+
assertEquals(original.getInitialIntervalInSeconds(), copy.getInitialIntervalInSeconds());
65+
assertEquals(original.getMaximumAttempts(), copy.getMaximumAttempts());
66+
assertEquals(original.getExpirationIntervalInSeconds(), copy.getExpirationIntervalInSeconds());
67+
assertEquals(original.getBackoffCoefficient(), copy.getBackoffCoefficient(), 0.0);
68+
}
69+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
7+
* except in compliance with the License. A copy of the License is located at
8+
*
9+
* <p>http://aws.amazon.com/apache2.0
10+
*
11+
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
* specific language governing permissions and limitations under the License.
14+
*/
15+
package com.uber.cadence.internal.replay;
16+
17+
import static junit.framework.TestCase.assertEquals;
18+
import static org.junit.Assert.*;
19+
20+
import com.uber.cadence.ActivityType;
21+
import com.uber.cadence.common.RetryOptions;
22+
import com.uber.cadence.internal.common.RetryParameters;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class ExecuteActivityParametersTest {
29+
30+
private ExecuteActivityParameters parameters;
31+
private ActivityType activityType;
32+
private RetryParameters retryParameters;
33+
34+
@Before
35+
public void setUp() {
36+
RetryOptions retryOptions = new RetryOptions.Builder().build();
37+
parameters = new ExecuteActivityParameters();
38+
activityType = new ActivityType();
39+
retryParameters = new RetryParameters(retryOptions);
40+
}
41+
42+
@Test
43+
public void testActivityId() {
44+
parameters.setActivityId("activity123");
45+
assertEquals("activity123", parameters.getActivityId());
46+
47+
parameters.withActivityId("activity456");
48+
assertEquals("activity456", parameters.getActivityId());
49+
}
50+
51+
@Test
52+
public void testActivityType() {
53+
parameters.setActivityType(activityType);
54+
assertEquals(activityType, parameters.getActivityType());
55+
56+
ActivityType newType = new ActivityType();
57+
parameters.withActivityType(newType);
58+
assertEquals(newType, parameters.getActivityType());
59+
}
60+
61+
@Test
62+
public void testInput() {
63+
byte[] input = {1, 2, 3};
64+
parameters.setInput(input);
65+
assertArrayEquals(input, parameters.getInput());
66+
67+
byte[] newInput = {4, 5, 6};
68+
parameters.withInput(newInput);
69+
assertArrayEquals(newInput, parameters.getInput());
70+
}
71+
72+
@Test
73+
public void testTimeouts() {
74+
parameters.setHeartbeatTimeoutSeconds(100L);
75+
assertEquals(100L, parameters.getHeartbeatTimeoutSeconds());
76+
77+
parameters.withHeartbeatTimeoutSeconds(200L);
78+
assertEquals(200L, parameters.getHeartbeatTimeoutSeconds());
79+
80+
parameters.setScheduleToStartTimeoutSeconds(300L);
81+
assertEquals(300L, parameters.getScheduleToStartTimeoutSeconds());
82+
83+
parameters.withScheduleToStartTimeoutSeconds(400L);
84+
assertEquals(400L, parameters.getScheduleToStartTimeoutSeconds());
85+
86+
parameters.setScheduleToCloseTimeoutSeconds(500L);
87+
assertEquals(500L, parameters.getScheduleToCloseTimeoutSeconds());
88+
89+
parameters.withScheduleToCloseTimeoutSeconds(600L);
90+
assertEquals(600L, parameters.getScheduleToCloseTimeoutSeconds());
91+
92+
parameters.setStartToCloseTimeoutSeconds(700L);
93+
assertEquals(700L, parameters.getStartToCloseTimeoutSeconds());
94+
95+
parameters.withStartToCloseTimeoutSeconds(800L);
96+
assertEquals(800L, parameters.getStartToCloseTimeoutSeconds());
97+
}
98+
99+
@Test
100+
public void testTaskList() {
101+
parameters.setTaskList("sampleTaskList");
102+
assertEquals("sampleTaskList", parameters.getTaskList());
103+
104+
parameters.withTaskList("newTaskList");
105+
assertEquals("newTaskList", parameters.getTaskList());
106+
}
107+
108+
@Test
109+
public void testRetryParameters() {
110+
parameters.setRetryParameters(retryParameters);
111+
assertEquals(retryParameters, parameters.getRetryParameters());
112+
113+
RetryParameters newRetryParameters = new RetryParameters();
114+
parameters.withRetryParameters(newRetryParameters);
115+
assertEquals(newRetryParameters, parameters.getRetryParameters());
116+
}
117+
118+
@Test
119+
public void testContext() {
120+
Map<String, byte[]> context = new HashMap<>();
121+
context.put("key1", new byte[] {1});
122+
parameters.setContext(context);
123+
assertEquals(context, parameters.getContext());
124+
125+
Map<String, byte[]> newContext = new HashMap<>();
126+
newContext.put("key2", new byte[] {2});
127+
parameters.withContext(newContext);
128+
assertEquals(newContext, parameters.getContext());
129+
}
130+
131+
@Test
132+
public void testCopy() {
133+
parameters.setActivityId("copyTest");
134+
parameters.setActivityType(activityType);
135+
parameters.setInput(new byte[] {10, 20});
136+
parameters.setHeartbeatTimeoutSeconds(900L);
137+
parameters.setRetryParameters(retryParameters);
138+
139+
ExecuteActivityParameters copy = parameters.copy();
140+
141+
assertEquals(parameters.getActivityId(), copy.getActivityId());
142+
assertEquals(parameters.getActivityType(), copy.getActivityType());
143+
assertArrayEquals(parameters.getInput(), copy.getInput());
144+
assertEquals(parameters.getHeartbeatTimeoutSeconds(), copy.getHeartbeatTimeoutSeconds());
145+
assertEquals(
146+
parameters.getRetryParameters().initialIntervalInSeconds,
147+
copy.getRetryParameters().initialIntervalInSeconds);
148+
assertEquals(
149+
parameters.getRetryParameters().backoffCoefficient,
150+
copy.getRetryParameters().backoffCoefficient);
151+
assertEquals(
152+
parameters.getRetryParameters().maximumAttempts, copy.getRetryParameters().maximumAttempts);
153+
}
154+
155+
@Test
156+
public void testToString() {
157+
parameters.setActivityId("toStringTest");
158+
parameters.setActivityType(activityType);
159+
parameters.setInput(new byte[] {10, 20});
160+
String expectedString =
161+
"ExecuteActivityParameters{activityId='toStringTest', activityType=ActivityType(), heartbeatTimeoutSeconds=0, input=[10, 20], scheduleToCloseTimeoutSeconds=0, scheduleToStartTimeoutSeconds=0, startToCloseTimeoutSeconds=0, taskList='null', retryParameters=null, context='null}";
162+
163+
assertEquals(expectedString, parameters.toString());
164+
}
165+
}

0 commit comments

Comments
 (0)