Skip to content

Commit

Permalink
Close client used for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Feb 27, 2019
1 parent c62a92b commit f57b404
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class Aria2Tester extends BaseTester<Boolean> {
super(context, profile, listener);
}

private void publishError(Exception ex, boolean authenticated) {
private void publishError(@NonNull Exception ex, boolean authenticated) {
publishMessage(ex.getClass().getName() + ": " + ex.getMessage(), Level.ERROR);
if (authenticated)
publishMessage("Your token or username and password may be wrong", Level.ERROR);
}

private <O> Object runRequest(AbstractClient.AriaRequestWithResult<O> request, Aria2Helper helper) {
@Nullable
private <O> Object runRequest(@NonNull AbstractClient.AriaRequestWithResult<O> request, @NonNull Aria2Helper helper) {
final AtomicReference<Object> lock = new AtomicReference<>(null);
helper.request(request, new AbstractClient.OnResult<O>() {
@Override
Expand Down Expand Up @@ -58,13 +59,7 @@ public void onException(@NonNull Exception ex) {
}

@Nullable
public Boolean call(@Nullable Object prevResult) {
AbstractClient client;
if (prevResult instanceof AbstractClient)
client = (AbstractClient) prevResult;
else
throw new IllegalStateException("Previous result should be a client, but was " + prevResult);

private Boolean call(@NonNull AbstractClient client) {
Aria2Helper helper = new Aria2Helper(client);
publishMessage("Started unauthenticated request...", Level.INFO);

Expand Down Expand Up @@ -92,8 +87,21 @@ public Boolean call(@Nullable Object prevResult) {
}
}

@Nullable
public Boolean call(@Nullable Object prevResult) {
AbstractClient client;
if (prevResult instanceof AbstractClient)
client = (AbstractClient) prevResult;
else
throw new IllegalStateException("Previous result should be a client, but was " + prevResult);

Boolean result = call(client);
client.close();
return result;
}

@NonNull
private Object runAuthenticated(Aria2Helper helper) {
private Object runAuthenticated(@NonNull Aria2Helper helper) {
publishMessage("Started authenticated request...", Level.INFO);
Object result = runRequest(AriaRequests.getVersion(), helper);
if (result instanceof Exception) publishError((Exception) result, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public Boolean call(@Nullable Object prevResult) {
} catch (IOException | NetUtils.InvalidUrlException ex) {
publishError(ex);
return null;
} finally {
client.connectionPool().evictAll();
client.dispatcher().executorService().shutdown();
}
}

Expand Down

0 comments on commit f57b404

Please sign in to comment.