From: Michal Duda <44705056+michal-duda-sonarsource@users.noreply.github.com> Date: Fri, 5 Apr 2019 12:04:42 +0000 (+0200) Subject: SONAR-11878 fix scanner behaviour on /batch/project timeout (#3204) X-Git-Tag: 6.7.7~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ce1ee69bf93e3cbed65b442282b6264290f163e5;p=sonarqube.git SONAR-11878 fix scanner behaviour on /batch/project timeout (#3204) --- diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java index c0c46730b83..8b049e0f5eb 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java @@ -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) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java index bc6987e90ec..27605060592 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java @@ -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); }