Skip to content

Commit 712ead8

Browse files
author
Max Presman
committed
adding retry logic
1 parent f70f83e commit 712ead8

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎src/main/java/com/pubnub/api/endpoints/Endpoint.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public abstract class Endpoint<Input, Output> {
3232

3333
@Getter(AccessLevel.PROTECTED)
3434
private PubNub pubnub;
35+
@Getter(AccessLevel.NONE)
36+
private PNCallback<Output> cachedCallback;
37+
3538
private static final int SERVER_RESPONSE_SUCCESS = 200;
3639
private static final int SERVER_RESPONSE_FORBIDDEN = 403;
3740
private static final int SERVER_RESPONSE_BAD_REQUEST = 400;
@@ -81,7 +84,7 @@ public final Output sync() throws PubNubException {
8184
}
8285

8386
public final Call<Input> async(final PNCallback<Output> callback) {
84-
87+
cachedCallback = callback;
8588
Call<Input> call;
8689

8790
try {
@@ -179,9 +182,15 @@ public void onFailure(final Call<Input> call, final Throwable throwable) {
179182
return call;
180183
}
181184

185+
public void retry() {
186+
async(cachedCallback);
187+
};
188+
182189
private PNStatus createStatusResponse(PNStatusCategory category, Response<Input> response, Exception throwable) {
183190
PNStatus.PNStatusBuilder pnStatus = PNStatus.builder();
184191

192+
pnStatus.executedEndpoint(this);
193+
185194
if (response == null || throwable != null) {
186195
pnStatus.error(true);
187196
}

‎src/main/java/com/pubnub/api/models/consumer/PNStatus.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.pubnub.api.models.consumer;
22

3+
import com.pubnub.api.endpoints.Endpoint;
34
import com.pubnub.api.enums.PNOperationType;
45
import com.pubnub.api.enums.PNStatusCategory;
6+
import lombok.AccessLevel;
57
import lombok.Builder;
68
import lombok.Getter;
79

@@ -31,11 +33,15 @@ public class PNStatus {
3133
private List<String> affectedChannels;
3234
private List<String> affectedChannelGroups;
3335

34-
/*
35-
public void retry(){
36-
// TODO
36+
@Getter(AccessLevel.NONE)
37+
private Endpoint executedEndpoint;
38+
39+
40+
public void retry() {
41+
executedEndpoint.retry();
3742
}
3843

44+
/*
3945
public void cancelAutomaticRetry() {
4046
// TODO
4147
}

‎src/test/java/com/pubnub/api/endpoints/TimeEndpointTest.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,31 @@ public void onResponse(PNTimeResult result, PNStatus status) {
8484
.untilAtomic(atomic, org.hamcrest.core.IsEqual.equalTo(1));
8585
}
8686

87+
@org.junit.Test
88+
public void testAsyncRetrySuccess() throws IOException, PubNubException {
89+
stubFor(get(urlPathEqualTo("/time/0"))
90+
.willReturn(aResponse().withBody("[14593046077243110]")));
91+
final AtomicInteger atomic = new AtomicInteger(0);
92+
partialTime.async(new TimeCallback(){
93+
94+
@Override
95+
public void onResponse(PNTimeResult result, PNStatus status) {
96+
97+
98+
assertTrue(result.getTimetoken().equals(14593046077243110L));
99+
atomic.incrementAndGet();
100+
101+
if (atomic.get() == 1) {
102+
status.retry();
103+
}
104+
105+
}
106+
});
107+
108+
Awaitility.await().atMost(5, TimeUnit.SECONDS)
109+
.untilAtomic(atomic, org.hamcrest.core.IsEqual.equalTo(2));
110+
}
111+
87112
@org.junit.Test
88113
public void testAsyncBrokenWithString() throws IOException, PubNubException {
89114
stubFor(get(urlPathEqualTo("/time/0"))
@@ -146,4 +171,4 @@ public void onResponse(PNTimeResult result, PNStatus status) {
146171
}
147172

148173

149-
}
174+
}

0 commit comments

Comments
 (0)