Search in sources :

Example 6 with CoverageFile

use of com.teamscale.report.jacoco.CoverageFile in project teamscale-jacoco-agent by cqse.

the class DelayedUploaderTest method shouldAsynchronouslyStoreToDestinationOnceCommitIsKnown.

@Test
public void shouldAsynchronouslyStoreToDestinationOnceCommitIsKnown(@TempDir Path outputPath) throws Exception {
    Path coverageFilePath = outputPath.resolve(String.format("jacoco-%d.xml", ZonedDateTime.now().toInstant().toEpochMilli()));
    CoverageFile coverageFile = new CoverageFile(Files.createFile(coverageFilePath).toFile());
    InMemoryUploader destination = new InMemoryUploader();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    DelayedUploader<String> store = new DelayedUploader<>(commit -> destination, outputPath, executor);
    store.upload(coverageFile);
    store.setCommitAndTriggerAsynchronousUpload("a2afb54566aaa");
    executor.shutdown();
    executor.awaitTermination(5, TimeUnit.SECONDS);
    assertThat(Files.list(outputPath).collect(Collectors.toList())).doesNotContain(coverageFilePath);
    assertThat(destination.getUploadedFiles()).contains(coverageFile);
}
Also used : Path(java.nio.file.Path) CoverageFile(com.teamscale.report.jacoco.CoverageFile) InMemoryUploader(com.teamscale.jacoco.agent.util.InMemoryUploader) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 7 with CoverageFile

use of com.teamscale.report.jacoco.CoverageFile in project teamscale-jacoco-agent by cqse.

the class HttpZipUploaderBase method tryUpload.

/**
 * Performs the upload and returns <code>true</code> if successful.
 */
protected boolean tryUpload(CoverageFile coverageFile) {
    logger.debug("Uploading coverage to {}", uploadUrl);
    File zipFile;
    try {
        zipFile = createZipFile(coverageFile);
    } catch (IOException e) {
        logger.error("Failed to compile coverage zip file for upload to {}", uploadUrl, e);
        return false;
    }
    try {
        Response<ResponseBody> response = uploadCoverageZip(zipFile);
        if (response.isSuccessful()) {
            return true;
        }
        String errorBody = "<no server response>";
        if (response.errorBody() != null) {
            errorBody = response.errorBody().string();
        }
        logger.error("Failed to upload coverage to {}. Request failed with error code {}. Error:\n{}", uploadUrl, response.code(), errorBody);
        return false;
    } catch (IOException e) {
        logger.error("Failed to upload coverage to {}. Probably a network problem", uploadUrl, e);
        return false;
    } catch (UploaderException e) {
        logger.error("Failed to upload coverage to {}. The configuration is probably incorrect", uploadUrl, e);
        return false;
    } finally {
        zipFile.delete();
    }
}
Also used : IOException(java.io.IOException) CoverageFile(com.teamscale.report.jacoco.CoverageFile) File(java.io.File) ResponseBody(okhttp3.ResponseBody)

Example 8 with CoverageFile

use of com.teamscale.report.jacoco.CoverageFile in project teamscale-jacoco-agent by cqse.

the class HttpZipUploaderBase method createZipFile.

/**
 * Creates the zip file in the system temp directory to upload which includes the given coverage XML and all {@link
 * #additionalMetaDataFiles}. The file is marked to be deleted on exit.
 */
private File createZipFile(CoverageFile coverageFile) throws IOException {
    File zipFile = Files.createTempFile(coverageFile.getNameWithoutExtension(), ".zip").toFile();
    zipFile.deleteOnExit();
    try (FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
        fillZipFile(zipOutputStream, coverageFile);
        return zipFile;
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) CoverageFile(com.teamscale.report.jacoco.CoverageFile) File(java.io.File)

Aggregations

CoverageFile (com.teamscale.report.jacoco.CoverageFile)8 File (java.io.File)5 Test (org.junit.jupiter.api.Test)5 InMemoryUploader (com.teamscale.jacoco.agent.util.InMemoryUploader)4 Path (java.nio.file.Path)4 IOException (java.io.IOException)2 ExecutorService (java.util.concurrent.ExecutorService)2 GitPropertiesLocator (com.teamscale.jacoco.agent.commit_resolution.git_properties.GitPropertiesLocator)1 GitPropertiesLocatorUtils (com.teamscale.jacoco.agent.commit_resolution.git_properties.GitPropertiesLocatorUtils)1 Benchmark (com.teamscale.jacoco.agent.util.Benchmark)1 EmptyReportException (com.teamscale.report.jacoco.EmptyReportException)1 Dump (com.teamscale.report.jacoco.dump.Dump)1 FileOutputStream (java.io.FileOutputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 HttpUrl (okhttp3.HttpUrl)1 ResponseBody (okhttp3.ResponseBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1