]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11878 fix scanner behaviour on /batch/project timeout (#3204)
authorMichal Duda <44705056+michal-duda-sonarsource@users.noreply.github.com>
Fri, 5 Apr 2019 12:04:42 +0000 (14:04 +0200)
committerGitHub <noreply@github.com>
Fri, 5 Apr 2019 12:04:42 +0000 (14:04 +0200)
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java

index c0c46730b8381063a0af3e5ecfa4763eb705073e..8b049e0f5ebe008dc8b4589e84ee3803322f5b7d 100644 (file)
@@ -31,7 +31,6 @@ import javax.annotation.Nullable;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.sonar.api.utils.MessageException;
 import org.sonar.scanner.bootstrap.ScannerWsClient;
 import org.sonar.scanner.util.ScannerUtils;
 import org.sonarqube.ws.WsBatch;
@@ -83,16 +82,12 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad
 
   private static boolean shouldThrow(Exception e) {
     for (Throwable t : Throwables.getCausalChain(e)) {
-      if (t instanceof HttpException) {
-        HttpException http = (HttpException) t;
-        return http.code() != HttpURLConnection.HTTP_NOT_FOUND;
-      }
-      if (t instanceof MessageException) {
-        return true;
+      if (t instanceof HttpException && ((HttpException) t).code() == HttpURLConnection.HTTP_NOT_FOUND) {
+        return false;
       }
     }
 
-    return false;
+    return true;
   }
 
   private static ProjectRepositories processStream(InputStream is, String projectKey) {
index bc6987e90ec3dd8629a2f90ee4d58342e2c76085..276050605924e4a3b7168f8c50439c4d3e2d1173 100644 (file)
@@ -24,6 +24,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.HttpURLConnection;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -37,6 +38,7 @@ import org.sonarqube.ws.client.WsRequest;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -53,20 +55,29 @@ public class DefaultProjectRepositoriesLoaderTest {
     wsClient = mock(ScannerWsClient.class);
     InputStream is = mockData();
     WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
+    InputStream isIssueMode = mockData();
+    WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F&issues_mode=true", isIssueMode);
     loader = new DefaultProjectRepositoriesLoader(wsClient);
   }
 
   @Test
-  public void continueOnError() {
+  public void continueOnHttp404Exception() {
+    when(wsClient.call(any(WsRequest.class))).thenThrow(new HttpException("/batch/project.protobuf?key=foo%3F", HttpURLConnection.HTTP_NOT_FOUND, ""));
+    ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
+    assertThat(proj.exists()).isEqualTo(false);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void failOnNonHttp404Exception() {
     when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class);
     ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
     assertThat(proj.exists()).isEqualTo(false);
   }
 
-  @Test
+  @Test(expected = IllegalStateException.class)
   public void parsingError() throws IOException {
     InputStream is = mock(InputStream.class);
-    when(is.read()).thenThrow(IOException.class);
+    when(is.read(any(byte[].class), anyInt(), anyInt())).thenThrow(IOException.class);
     WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
     loader.load(PROJECT_KEY, false, null);
   }
@@ -100,7 +111,7 @@ public class DefaultProjectRepositoriesLoaderTest {
   }
 
   @Test
-  public void deserializeResponse() throws IOException {
+  public void deserializeResponse() {
     loader.load(PROJECT_KEY, false, null);
   }