Selaa lähdekoodia

SONAR-11878 fix scanner behaviour on /batch/project timeout

tags/7.8
Michal Duda 5 vuotta sitten
vanhempi
commit
b727216b31

+ 3
- 8
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java Näytä tiedosto

import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.bootstrap.ScannerWsClient; import org.sonar.scanner.bootstrap.ScannerWsClient;
import org.sonar.scanner.util.ScannerUtils; import org.sonar.scanner.util.ScannerUtils;
import org.sonarqube.ws.Batch.WsProjectResponse; import org.sonarqube.ws.Batch.WsProjectResponse;


private static boolean shouldThrow(Exception e) { private static boolean shouldThrow(Exception e) {
for (Throwable t : Throwables.getCausalChain(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) throws IOException { private static ProjectRepositories processStream(InputStream is) throws IOException {

+ 12
- 3
sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java Näytä tiedosto

import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;


} }


@Test @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, null);
assertThat(proj.exists()).isEqualTo(false);
}

@Test(expected = IllegalStateException.class)
public void failOnNonHttp404Exception() {
when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class); when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class);
ProjectRepositories proj = loader.load(PROJECT_KEY, null); ProjectRepositories proj = loader.load(PROJECT_KEY, null);
assertThat(proj.exists()).isEqualTo(false); assertThat(proj.exists()).isEqualTo(false);
} }


@Test
@Test(expected = IllegalStateException.class)
public void parsingError() throws IOException { public void parsingError() throws IOException {
InputStream is = mock(InputStream.class); 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); WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
loader.load(PROJECT_KEY, null); loader.load(PROJECT_KEY, null);
} }

Loading…
Peruuta
Tallenna