Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from snyk/feat/custom_timeout_wating_analysis_…
Browse files Browse the repository at this point in the history
…results

feat: added param "waiting results" timeout [ROAD-389]
  • Loading branch information
ArtsiomCh authored Oct 6, 2021
2 parents 7536ce7 + cd6a757 commit 58419e2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.1.11] - 2020-10-05
- added param: "waiting results" timeout.

## [2.0.18] - 2020-10-05
- fix exception when Null markers received.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

group = "io.snyk.code.sdk"
archivesBaseName = "snyk-code-client"
version = "2.1.10"
version = "2.1.11"

repositories {
mavenCentral()
Expand Down
23 changes: 5 additions & 18 deletions src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ public class DeepCodeRestApiTest {
@Test
public void _010_newLogin() {
System.out.println("\n--------------New Login----------------\n");
LoginResponse response = null;
response = DeepCodeRestApi.newLogin(userAgent);
assertEquals(200, response.getStatusCode());
assertTrue(response.getLoginURL().contains(response.getSessionToken()));
System.out.printf(
"New login request passed with returned: \nsession token: %1$s \nlogin URL: %2$s\n",
response.getSessionToken(), response.getLoginURL());
try {
DeepCodeRestApi.newLogin(userAgent);
} catch (UnsupportedOperationException e) {
System.out.printf(e.getMessage());
}
}

@Test
Expand Down Expand Up @@ -157,17 +155,6 @@ public void _031_createBundle_wrong_request() {
"Create Bundle call with malformed token [%1$s] is not accepted by server with Status code [%2$d].\n",
brokenToken, response.getStatusCode());

final String incompleteLoginToken = DeepCodeRestApi.newLogin(userAgent).getSessionToken();
response = DeepCodeRestApi.createBundle(incompleteLoginToken, files);
assertNotNull(response);
assertEquals(
"Create Bundle call with incomplete login token should not be accepted by server",
401,
response.getStatusCode());
System.out.printf(
"Create Bundle call with incomplete login token is not accepted by server with Status code [%2$d].\n",
brokenToken, response.getStatusCode());

// seems to be a bug on server: it returns 200
/*
response =
Expand Down
24 changes: 3 additions & 21 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class DeepCodeRestApi {

private DeepCodeRestApi() {}

private static final String API_URL = "https://www.deepcode.ai/";
private static final String API_URL = "https://deeproxy.snyk.io/";

private static Retrofit retrofit = buildRetrofit(API_URL, false);

Expand Down Expand Up @@ -113,26 +113,8 @@ private interface LoginCall {
* @return {@link LoginResponse} instance
*/
@NotNull
public static LoginResponse newLogin(@NotNull String userAgent) {
final LoginCall loginCall = retrofit.create(LoginCall.class);
try {
final SourceString source = new SourceString(userAgent);
final Response<LoginResponse> retrofitResponse = loginCall.doNewLogin(source).execute();
LoginResponse result = retrofitResponse.body();
if (result == null) return new LoginResponse();
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200:
result.setStatusDescription("The new login request was successful");
break;
default:
result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
break;
}
return result;
} catch (IOException e) {
return new LoginResponse();
}
public static LoginResponse newLogin(@NotNull String userAgent) throws UnsupportedOperationException {
throw new UnsupportedOperationException("login request is not handled anymore");
}

private interface CheckSessionCall {
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ private GetAnalysisResponse doGetAnalysis(
List<String> filesToAnalyse) {
GetAnalysisResponse response;
int counter = 0;
final int timeout = 100; // seconds
final int attempts = timeout * 1000 / pdUtils.DEFAULT_DELAY;
final long timeout = deepCodeParams.getTimeoutForGettingAnalysesMs();
final long attempts = timeout / pdUtils.DEFAULT_DELAY;
do {
if (counter > 0) pdUtils.delay(pdUtils.DEFAULT_DELAY, progress);
response =
Expand All @@ -566,19 +566,20 @@ private GetAnalysisResponse doGetAnalysis(
return new GetAnalysisResponse();

double responseProgress = response.getProgress();
if (responseProgress <= 0 || responseProgress > 1)
if (responseProgress <= 0 || responseProgress > 1) {
responseProgress = ((double) counter) / attempts;
}
pdUtils.progressSetFraction(progress, responseProgress);
pdUtils.progressSetText(
progress, WAITING_FOR_ANALYSIS_TEXT + (int) (responseProgress * 100) + "% done");

if (counter >= attempts) {
dcLogger.logWarn("Timeout expire for waiting analysis results.");
/*
DeepCodeNotifications.showWarn(
"Can't get analysis results from the server. Network or server internal error. Please, try again later.",
project);
*/
pdUtils.showWarn(
"Can't get analysis results from the server. Timeout of " + timeout/1000 + " sec. is reached." +
" Please, increase timeout or try again later.",
project,
false);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ai.deepcode.javaclient.DeepCodeRestApi;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

public abstract class DeepCodeParamsBase {

// Settings
Expand All @@ -16,6 +18,7 @@ public abstract class DeepCodeParamsBase {
// Inner params
private String loginUrl;
private String ideProductName;
private Supplier<Long> getTimeoutForGettingAnalysesMs;

protected DeepCodeParamsBase(
boolean isEnable,
Expand All @@ -25,7 +28,9 @@ protected DeepCodeParamsBase(
int minSeverity,
String sessionToken,
String loginUrl,
String ideProductName) {
String ideProductName,
Supplier<Long> getTimeoutForGettingAnalysesMs
) {
this.isEnable = isEnable;
this.apiUrl = apiUrl;
this.disableSslVerification = disableSslVerification;
Expand All @@ -34,6 +39,7 @@ protected DeepCodeParamsBase(
this.sessionToken = sessionToken;
this.loginUrl = loginUrl;
this.ideProductName = ideProductName;
this.getTimeoutForGettingAnalysesMs = getTimeoutForGettingAnalysesMs;
}

public void clearLoginParams() {
Expand Down Expand Up @@ -116,4 +122,8 @@ public void setEnable(boolean isEnable) {
public String getIdeProductName() {
return ideProductName;
}

public long getTimeoutForGettingAnalysesMs() {
return getTimeoutForGettingAnalysesMs.get();
}
}

0 comments on commit 58419e2

Please sign in to comment.