Skip to content

Commit

Permalink
[#185024471] Initial Subscription inaccuracies still exist
Browse files Browse the repository at this point in the history
- [x] close down all cached connections upon socket time out (see)[square/okhttp#3146]
- [x] units and impls
  • Loading branch information
lwoydziak committed Apr 27, 2023
1 parent a55de60 commit 2058968
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The Xenon View Java SDK is the Java SDK to interact with [XenonView](https://xen
<br/>

## What"s New <a id="whats-new"></a>
* v0.1.8 - On socket timeout, close all cached connections and start new see #185024471.
* v0.1.7 - Add options for term/price for all subscription related calls.
* v0.1.6 - Add initial subscriptions options for term/price.
* v0.1.5 - Fix typo
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
mavenCentral()
}

project.version = "0.1.7"
project.version = "0.1.8"
project.description = 'The Xenon View Java SDK is the Java SDK to interact with https://xenonview.com.'
project.group = "io.github.xenonview-com"
project.archivesBaseName = 'xenon-view-sdk'
Expand Down
22 changes: 17 additions & 5 deletions lib/src/main/java/xenon/view/sdk/api/fetch/JsonFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@
public class JsonFetcher implements Fetchable {
private OkHttpClient client;
private OkHttpClient.Builder builder = new OkHttpClient.Builder();
public interface NewHttpClientPointer {
OkHttpClient newClient();
}
private static OkHttpClient newClient(){
return new OkHttpClient();
}
private NewHttpClientPointer httpClientMaker = JsonFetcher::newClient;


public JsonFetcher(){
client = new OkHttpClient();
client = JsonFetcher.newClient();
}

public JsonFetcher(OkHttpClient _client){
client = _client;
public JsonFetcher(NewHttpClientPointer _clientMaker){
httpClientMaker = _clientMaker;
client = httpClientMaker.newClient();
}
public JsonFetcher(OkHttpClient.Builder _builder, OkHttpClient _client){
this(_client);
public JsonFetcher(OkHttpClient.Builder _builder, NewHttpClientPointer _clientMaker){
this(_clientMaker);
builder = _builder;
}

Expand Down Expand Up @@ -79,6 +88,9 @@ public CompletableFuture<Json> fetch(JSONObject data) throws JSONException{
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
//HTTP request exception
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
client = httpClientMaker.newClient();
completableFuture.completeExceptionally(e);
}

Expand Down
29 changes: 25 additions & 4 deletions lib/src/test/java/xenon/view/sdk/api/fetch/JsonFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.*;
Expand Down Expand Up @@ -58,7 +59,7 @@ public class JsonFetcherTest {
AtomicReference<CompletableFuture<Json>> completableFuture = new AtomicReference<>();
BeforeEach(() -> {
data.get().put("method", "GET");
unit.set(new JsonFetcher(client));
unit.set(new JsonFetcher(()->{return client;}));
when(client.newCall(any())).thenAnswer(invocation -> {
request.set((Request) invocation.getArguments()[0]);
return enqueuer;
Expand Down Expand Up @@ -112,9 +113,15 @@ public class JsonFetcherTest {
});
});
Describe("when the request is not successful", () -> {
Dispatcher theDispatcher = mock(Dispatcher.class);
ExecutorService theService = mock(ExecutorService.class);
ConnectionPool thePool = mock(ConnectionPool.class);
BeforeEach(() -> {
Call theCall = mock(Call.class);
when(theCall.request()).thenReturn(request.get());
when(client.dispatcher()).thenReturn(theDispatcher);
when(theDispatcher.executorService()).thenReturn(theService);
when(client.connectionPool()).thenReturn(thePool);
callback.get().onFailure(theCall, new IOException("{\"response\":\"failed\"}"));
});

Expand All @@ -124,6 +131,10 @@ public class JsonFetcherTest {
return new Json(err.getMessage());
});
});
It("closes connection pool", () -> {
verify(theService).shutdown();
verify(thePool).evictAll();
});
});
Describe("when the request unauthorized", () -> {
final Response response = mock(Response.class);
Expand Down Expand Up @@ -158,9 +169,15 @@ public class JsonFetcherTest {
});
});
Describe("when the request errors", () -> {
Dispatcher theDispatcher = mock(Dispatcher.class);
ExecutorService theService = mock(ExecutorService.class);
ConnectionPool thePool = mock(ConnectionPool.class);
BeforeEach(() -> {
Call theCall = mock(Call.class);
when(theCall.request()).thenReturn(request.get());
when(client.dispatcher()).thenReturn(theDispatcher);
when(theDispatcher.executorService()).thenReturn(theService);
when(client.connectionPool()).thenReturn(thePool);
callback.get().onFailure(theCall, new IOException("No Internet Connection"));
});
It("rejects the promise", () -> {
Expand All @@ -169,6 +186,10 @@ public class JsonFetcherTest {
return new Json(err.getMessage());
});
});
It("closes connection pool", () -> {
verify(theService).shutdown();
verify(thePool).evictAll();
});
});
Describe("when the request generally errors", () -> {
final Response response = mock(Response.class);
Expand Down Expand Up @@ -233,7 +254,7 @@ public class JsonFetcherTest {
data.get().put("body", new JSONObject() {{
put("test", "body");
}});
unit.set(new JsonFetcher(client));
unit.set(new JsonFetcher(()->{return client;}));
when(client.newCall(any())).thenAnswer(invocation -> {
request.set((Request) invocation.getArguments()[0]);
return enqueuer;
Expand Down Expand Up @@ -269,7 +290,7 @@ public class JsonFetcherTest {
JSONObject headers = new JSONObject();
headers.put("authorization", "Bearer <token>");
data.get().put("requestHeaders", headers);
unit.set(new JsonFetcher(client));
unit.set(new JsonFetcher(()->{return client;}));
when(client.newCall(any())).thenAnswer(invocation -> {
request.set((Request) invocation.getArguments()[0]);
return enqueuer;
Expand Down Expand Up @@ -304,7 +325,7 @@ public class JsonFetcherTest {
headers.put("authorization", "Bearer <token>");
data.get().put("requestHeaders", headers);
data.get().put("ignore-certificate-errors", true);
unit.set(new JsonFetcher(builder, client));
unit.set(new JsonFetcher(builder, ()->{return client;}));
when(builder.sslSocketFactory(any(), any())).thenReturn(builder);
when(builder.hostnameVerifier(any())).thenReturn(builder);
when(builder.build()).thenReturn(client);
Expand Down

0 comments on commit 2058968

Please sign in to comment.