Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Remove busy loop in waitProjectStart
Browse files Browse the repository at this point in the history
Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>
  • Loading branch information
gciavarrini committed Apr 12, 2023
1 parent 0ccb24f commit b6edb94
Showing 1 changed file with 38 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@

package com.redhat.parodos.examples.integration.utils;

import com.redhat.parodos.sdk.api.ApiCallback;
import com.redhat.parodos.sdk.api.ApiException;
import com.redhat.parodos.sdk.api.ProjectApi;
import com.redhat.parodos.sdk.model.ProjectResponseDTO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static org.junit.Assert.fail;

/**
* @author Gloria Ciavarrini (Github: gciavarrini)
*/
@Slf4j
public final class ExamplesUtils {

static final int maxTry = 100;

public static void waitProjectStart(ProjectApi projectApi) throws ApiException, InterruptedException {

AtomicInteger tryCount = new AtomicInteger(0);
AsyncResult<List<ProjectResponseDTO>> asyncResult = new AsyncResult<>();
AsyncResult asyncResult = new AsyncResult();
Lock lock = new ReentrantLock();
Condition response = lock.newCondition();
ApiCallback<List<ProjectResponseDTO>> apiCallback = new ApiCallback<>() {
AtomicInteger failureCounter = new AtomicInteger(0);

Expand All @@ -35,15 +36,23 @@ public void onFailure(ApiException e, int statusCode, Map<String, List<String>>
int i = failureCounter.incrementAndGet();
if (i >= 100) {
asyncResult.setError(e.getMessage());
asyncResult.setStatus(statusCode);
signal();
}
else {
try {
projectApi.getProjectsAsync(this);
}
catch (ApiException apie) {
asyncResult.setError(apie.getMessage());
signal();
}
}
}

@Override
public void onSuccess(List<ProjectResponseDTO> result, int statusCode,
Map<String, List<String>> responseHeaders) {
asyncResult.setResponse(result);
asyncResult.setStatus(statusCode);
signal();
}

@Override
Expand All @@ -53,29 +62,29 @@ public void onUploadProgress(long bytesWritten, long contentLength, boolean done
@Override
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
}
};

do {
if (tryCount.get() >= maxTry) {
fail("Can get results from getProjectsAsync, timeout reached.");
}
Thread.sleep(1000);
tryCount.incrementAndGet();

if (asyncResult.responseReceived()) {
if (asyncResult.getError() != null) {
fail("An error occurred while executing getProjectsAsync: " + asyncResult.getError()
+ ". Status code: " + asyncResult.getStatus());
private void signal() {
lock.lock();
try {
response.signal();
}
if (asyncResult.getResponse() != null) {
break;
finally {
lock.unlock();
}
// retry async call
projectApi.getProjectsAsync(apiCallback);
}
};
projectApi.getProjectsAsync(apiCallback);
lock.lock();
try {
// should be more than enough
response.await(60, TimeUnit.SECONDS);
if (asyncResult.getError() != null) {
fail("An error occurred while executing getProjectsAsync: " + asyncResult.getError());
}
}
while (!asyncResult.responseReceived());

finally {
lock.unlock();
}
}

@Nullable
Expand All @@ -88,28 +97,10 @@ public static ProjectResponseDTO getProjectByNameAndDescription(List<ProjectResp
}

@Data
private static class AsyncResult<T> {
private static class AsyncResult {

private String error;

private Integer status;

private T response;

private AsyncResult() {
clear();
}

private boolean responseReceived() {
return error != null && status != null && response != null;
}

private void clear() {
error = null;
status = null;
response = null;
}

}

}

0 comments on commit b6edb94

Please sign in to comment.