Skip to content

Commit 95d4a61

Browse files
committed
Added more unit tests
1 parent 45eb15c commit 95d4a61

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
4+
package oracle.kubernetes.operator.helpers;
5+
6+
import oracle.kubernetes.operator.calls.CallFactory;
7+
import oracle.kubernetes.operator.calls.RequestParams;
8+
import oracle.kubernetes.operator.work.Step;
9+
10+
public interface AsyncRequestStepFactory {
11+
<T> Step createRequestAsync(ResponseStep<T> next, RequestParams requestParams, CallFactory<T> factory,
12+
ClientPool helper, int timeoutSeconds, int maxRetryCount,
13+
String fieldSelector, String labelSelector, String resourceVersion);
14+
}

operator/src/main/java/oracle/kubernetes/operator/helpers/CallBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,10 @@ public Step deleteIngressAsync(String name, String namespace, V1DeleteOptions de
977977
}
978978

979979

980+
private static final AsyncRequestStepFactory STEP_FACTORY = AsyncRequestStep::new;
981+
980982
private <T> Step createRequestAsync(ResponseStep<T> next, RequestParams requestParams, CallFactory<T> factory) {
981-
return new AsyncRequestStep<>(next, requestParams, factory, helper, timeoutSeconds, maxRetryCount, fieldSelector, labelSelector, resourceVersion);
983+
return STEP_FACTORY.createRequestAsync(next, requestParams, factory, helper, timeoutSeconds, maxRetryCount, fieldSelector, labelSelector, resourceVersion);
982984
}
983985

984986

operator/src/test/java/oracle/kubernetes/operator/calls/AsyncRequestStepTest.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ public void afterFiber_timeoutStepScheduled() throws Exception {
8787
assertTrue(schedule.containsStepAt(TIMEOUT_SECONDS, TimeUnit.SECONDS));
8888
}
8989

90+
@Test
91+
public void afterTimeout_newRequestSent() throws Exception {
92+
callFactory.clearRequest();
93+
94+
schedule.setTime(TIMEOUT_SECONDS, TimeUnit.SECONDS);
95+
96+
assertTrue(callFactory.invokedWith(requestParams));
97+
}
98+
9099
@Test
91100
public void afterSuccessfulCallback_nextStepAppliedWithValue() throws Exception {
92101
callFactory.sendSuccessfulCallback(17);
@@ -103,16 +112,31 @@ public void afterSuccessfulCallback_packetDoesNotContainsResponse() throws Excep
103112

104113
@Test
105114
public void afterFailedCallback_packetContainsRetryStrategy() throws Exception {
106-
schedule.execute(() ->
107-
callFactory.sendFailedCallback(new ApiException("test failure"), HttpURLConnection.HTTP_UNAVAILABLE));
115+
sendFailedCallback(HttpURLConnection.HTTP_UNAVAILABLE);
108116

109117
assertThat(packet.getComponents().get(RESPONSE_COMPONENT_NAME).getSPI(RetryStrategy.class), notNullValue());
110118
}
111119

120+
private void sendFailedCallback(int statusCode) {
121+
schedule.execute(() -> callFactory.sendFailedCallback(new ApiException("test failure"), statusCode));
122+
}
123+
124+
@Test
125+
public void afterFailedCallback_retrySentAfterDelay() throws Exception {
126+
sendFailedCallback(HttpURLConnection.HTTP_UNAVAILABLE);
127+
callFactory.clearRequest();
128+
129+
schedule.setTime(TIMEOUT_SECONDS-1, TimeUnit.SECONDS);
130+
131+
assertTrue(callFactory.invokedWith(requestParams));
132+
}
133+
112134
// todo tests
113-
// after timeout, packet contains retry strategy
114-
// after either failure, setting time to before the timeout causes new request
115-
// after new request, success leads to invocation
135+
// can new request clear timeout action?
136+
// what is accessContinue?
137+
// test CONFLICT (409) status
138+
// no retry if status not handled
139+
// test exceeded retry count
116140

117141

118142
static class TestStep extends ResponseStep<Integer> {
@@ -135,15 +159,19 @@ static class CallFactoryStub implements CallFactory<Integer> {
135159
private RequestParams requestParams;
136160
private ApiCallback<Integer> callback;
137161

138-
private boolean invokedWith(RequestParams requestParams) {
162+
void clearRequest() {
163+
requestParams = null;
164+
}
165+
166+
boolean invokedWith(RequestParams requestParams) {
139167
return requestParams == this.requestParams;
140168
}
141169

142-
private void sendSuccessfulCallback(Integer callbackValue) {
170+
void sendSuccessfulCallback(Integer callbackValue) {
143171
callback.onSuccess(callbackValue, HttpURLConnection.HTTP_OK, Collections.emptyMap());
144172
}
145173

146-
private void sendFailedCallback(ApiException exception, int statusCode) {
174+
void sendFailedCallback(ApiException exception, int statusCode) {
147175
callback.onFailure(exception, statusCode, Collections.emptyMap());
148176
}
149177

0 commit comments

Comments
 (0)