Skip to content

Commit 4864d27

Browse files
author
Max Presman
committed
some subscription tests
1 parent e1d9de9 commit 4864d27

6 files changed

Lines changed: 164 additions & 17 deletions

File tree

‎src/main/java/com/pubnub/api/core/Pubnub.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ public String getVersion() {
174174
return sdkVersion;
175175
}
176176

177+
/**
178+
* Stop the SDK and terminate all listeners.
179+
*/
180+
public void stop() {
181+
subscriptionManager.stop();
182+
}
183+
177184
private String fetchSDKVersion() {
178185
byte[] encoded;
179186
try {

‎src/main/java/com/pubnub/api/core/models/consumer_facing/PNErrorData.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.pubnub.api.core.models.consumer_facing;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@AllArgsConstructor
7+
@Getter
38
public class PNErrorData {
49

510
String information;

‎src/main/java/com/pubnub/api/core/models/consumer_facing/PNErrorStatus.java‎

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

3-
/**
4-
* Created by Max on 4/11/16.
5-
*/
3+
import lombok.Data;
4+
5+
@Data
66
public class PNErrorStatus extends PNStatus {
77

88
PNErrorData errorData;

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.pubnub.api.core.PubnubError;
77
import com.pubnub.api.core.PubnubException;
88
import com.pubnub.api.core.enums.PNOperationType;
9+
import com.pubnub.api.core.models.consumer_facing.PNErrorData;
910
import com.pubnub.api.core.models.consumer_facing.PNErrorStatus;
1011
import com.pubnub.api.core.utils.Base64;
1112
import okhttp3.OkHttpClient;
@@ -81,7 +82,7 @@ public final Call<Input> async(final PNCallback<Output> callback) {
8182

8283
@Override
8384
public void onResponse(final Call<Input> call, final Response<Input> response) {
84-
Output callbackResponse = null;
85+
Output callbackResponse;
8586
PNErrorStatus pnErrorStatus = new PNErrorStatus();
8687

8788

@@ -96,7 +97,7 @@ public void onResponse(final Call<Input> call, final Response<Input> response) {
9697
}
9798

9899
PubnubException ex = new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, responseBodyText, response.code());
99-
writeFieldsToResponse(response, pnErrorStatus, false, null);
100+
writeFieldsToResponse(response, pnErrorStatus, true, ex);
100101
callback.onResponse(null, pnErrorStatus);
101102
return;
102103
}
@@ -105,19 +106,20 @@ public void onResponse(final Call<Input> call, final Response<Input> response) {
105106
callbackResponse = createResponse(response);
106107
} catch (PubnubException e) {
107108
PubnubException pubnubException = new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, e.getMessage(), response.code());
108-
writeFieldsToResponse(response, pnErrorStatus, false, pubnubException);
109+
writeFieldsToResponse(response, pnErrorStatus, true, pubnubException);
109110
callback.onResponse(null, pnErrorStatus);
110111
return;
111112
}
112113

113-
writeFieldsToResponse(response, pnErrorStatus, true, null);
114+
writeFieldsToResponse(response, pnErrorStatus, false, null);
114115
callback.onResponse(callbackResponse, pnErrorStatus);
115116
}
116117

117118
@Override
118119
public void onFailure(final Call<Input> call, final Throwable throwable) {
119120
PNErrorStatus pnErrorStatus = new PNErrorStatus();
120-
writeFieldsToResponse(null, pnErrorStatus, true, throwable);
121+
PubnubException pubnubException = new PubnubException(PubnubError.PNERROBJ_HTTP_ERROR, throwable.getMessage(), null);
122+
writeFieldsToResponse(null, pnErrorStatus, true, pubnubException);
121123
callback.onResponse(null, pnErrorStatus);
122124

123125
}
@@ -126,9 +128,20 @@ public void onFailure(final Call<Input> call, final Throwable throwable) {
126128
return call;
127129
}
128130

129-
private void writeFieldsToResponse(Response<Input> response, PNErrorStatus pnErrorStatus, boolean isError, Throwable throwable) {
131+
private void writeFieldsToResponse(Response<Input> response, PNErrorStatus pnErrorStatus, boolean isError, Exception throwable) {
132+
133+
if (response != null) {
134+
pnErrorStatus.setStatusCode(response.code());
135+
}
136+
130137
pnErrorStatus.setOperation(getOperationType());
131138
pnErrorStatus.setError(isError);
139+
140+
if (isError) {
141+
PNErrorData errorData = new PNErrorData(null, throwable);
142+
pnErrorStatus.setErrorData(errorData);
143+
}
144+
132145
}
133146

134147
protected String signSHA256(String key, String data) throws PubnubException {

‎src/main/java/com/pubnub/api/managers/SubscriptionManager.java‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public SubscriptionManager(Pubnub pubnub) {
4747
registerHeartbeatTimer();
4848
}
4949

50+
public void stop() {
51+
stopHeartbeatTimer();
52+
stopSubscribeLoop();
53+
}
54+
5055
public void addListener(SubscribeCallback listener) {
5156
listeners.add(listener);
5257
}
@@ -113,10 +118,8 @@ public void onResponse(Boolean result, PNErrorStatus status) {
113118
}
114119

115120
private void registerHeartbeatTimer() {
116-
117-
if (timer != null) {
118-
timer.cancel();
119-
}
121+
// make sure only one timer is running at a time.
122+
stopHeartbeatTimer();
120123

121124
timer = new Timer();
122125
timer.schedule(new TimerTask() {
@@ -128,11 +131,16 @@ public void run() {
128131

129132
}
130133

131-
private void startSubscribeLoop() {
132-
133-
if (subscribeCall != null && !subscribeCall.isExecuted() && !subscribeCall.isCanceled()) {
134-
subscribeCall.cancel();
134+
private void stopHeartbeatTimer() {
135+
if (timer != null) {
136+
timer.cancel();
137+
timer = null;
135138
}
139+
}
140+
141+
private void startSubscribeLoop() {
142+
// this function can be called from different points, make sure any old loop is closed
143+
stopSubscribeLoop();
136144

137145
List<String> combinedChannels = new ArrayList<>();
138146
List<String> combinedChannelGroups = new ArrayList<>();
@@ -158,6 +166,11 @@ private void startSubscribeLoop() {
158166
.async(new PNCallback<SubscribeEnvelope>() {
159167
@Override
160168
public void onResponse(SubscribeEnvelope result, PNErrorStatus status) {
169+
if (status.isError()) {
170+
announce(status);
171+
return;
172+
}
173+
161174
if (result.getMessages().size() != 0) {
162175
processIncomingMessages(result.getMessages());
163176
}
@@ -168,6 +181,12 @@ public void onResponse(SubscribeEnvelope result, PNErrorStatus status) {
168181
});
169182
}
170183

184+
private void stopSubscribeLoop() {
185+
if (subscribeCall != null && !subscribeCall.isExecuted() && !subscribeCall.isCanceled()) {
186+
subscribeCall.cancel();
187+
}
188+
}
189+
171190
private void performHeartbeatLoop() {
172191
if (heartbeatCall != null && !heartbeatCall.isCanceled() && !heartbeatCall.isExecuted()) {
173192
heartbeatCall.cancel();
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.pubnub.api.managers;
2+
3+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
4+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
5+
import com.jayway.awaitility.Awaitility;
6+
import com.pubnub.api.callbacks.SubscribeCallback;
7+
import com.pubnub.api.core.Pubnub;
8+
import com.pubnub.api.core.models.consumer_facing.PNMessageResult;
9+
import com.pubnub.api.core.models.consumer_facing.PNPresenceEventResult;
10+
import com.pubnub.api.core.models.consumer_facing.PNStatus;
11+
import com.pubnub.api.endpoints.EndpointTest;
12+
import org.junit.*;
13+
14+
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.concurrent.TimeUnit;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
22+
23+
public class SubscriptionManagerTest extends EndpointTest {
24+
25+
private Pubnub pubnub;
26+
private SubscriptionManager instance;
27+
28+
@Before
29+
public void beforeEach() throws IOException {
30+
pubnub = this.createPubNubInstance(8080);
31+
instance = new SubscriptionManager(pubnub);
32+
}
33+
34+
@After
35+
public void afterEach() {
36+
pubnub.stop();
37+
}
38+
39+
@Rule
40+
public WireMockRule wireMockRule = new WireMockRule();
41+
42+
@Test
43+
public void testSubscribeBuilder() {
44+
final AtomicInteger atomic = new AtomicInteger(0);
45+
stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch1/0"))
46+
.willReturn(aResponse().withBody("{\"t\":{\"t\":\"14607577960932487\",\"r\":1},\"m\":[{\"a\":\"4\",\"f\":0,\"i\":\"Client-g5d4g\",\"p\":{\"t\":\"14607577960925503\",\"r\":1},\"k\":\"sub-c-4cec9f8e-01fa-11e6-8180-0619f8945a4f\",\"c\":\"coolChannel\",\"d\":{\"text\":\"Enter Message Here\"},\"b\":\"coolChan-bnel\"}]}")));
47+
48+
instance.addListener(new SubscribeCallback() {
49+
@Override
50+
public void status(Pubnub pubnub, PNStatus status) {
51+
}
52+
53+
@Override
54+
public void message(Pubnub pubnub, PNMessageResult message) {
55+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
56+
Assert.assertEquals(1, requests.size());
57+
atomic.addAndGet(1);
58+
}
59+
60+
@Override
61+
public void presence(Pubnub pubnub, PNPresenceEventResult presence) {
62+
}
63+
});
64+
65+
instance.adaptSubscribeBuilder(Arrays.asList("ch1", "ch2"), new ArrayList<String>(), false);
66+
67+
Awaitility.await().atMost(5, TimeUnit.SECONDS)
68+
.untilAtomic(atomic, org.hamcrest.core.IsEqual.equalTo(1));
69+
70+
}
71+
72+
@Test
73+
public void testSubscribePresenceBuilder() {
74+
final AtomicInteger atomic = new AtomicInteger(0);
75+
stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch2-pnpres,ch1,ch1-pnpres/0"))
76+
.willReturn(aResponse().withBody("{\"t\":{\"t\":\"14607577960932487\",\"r\":1},\"m\":[{\"a\":\"4\",\"f\":0,\"i\":\"Client-g5d4g\",\"p\":{\"t\":\"14607577960925503\",\"r\":1},\"k\":\"sub-c-4cec9f8e-01fa-11e6-8180-0619f8945a4f\",\"c\":\"coolChannel\",\"d\":{\"text\":\"Enter Message Here\"},\"b\":\"coolChan-bnel\"}]}")));
77+
78+
instance.addListener(new SubscribeCallback() {
79+
@Override
80+
public void status(Pubnub pubnub, PNStatus status) {
81+
}
82+
83+
@Override
84+
public void message(Pubnub pubnub, PNMessageResult message) {
85+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/v2/subscribe.*")));
86+
Assert.assertEquals(1, requests.size());
87+
Assert.assertEquals("{text=Enter Message Here}", message.getData().getMessage().toString());
88+
atomic.addAndGet(1);
89+
}
90+
91+
@Override
92+
public void presence(Pubnub pubnub, PNPresenceEventResult presence) {
93+
}
94+
});
95+
96+
instance.adaptSubscribeBuilder(Arrays.asList("ch1", "ch2"), new ArrayList<String>(), true);
97+
98+
Awaitility.await().atMost(5, TimeUnit.SECONDS)
99+
.untilAtomic(atomic, org.hamcrest.core.IsEqual.equalTo(1));
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)