diff options
author | Harald Musum <musum@oath.com> | 2017-11-23 08:45:52 +0100 |
---|---|---|
committer | Harald Musum <musum@oath.com> | 2017-11-23 08:45:52 +0100 |
commit | eef3f7c477de9033584da18f830694211f1c180f (patch) | |
tree | ec0cfbe9e0556b4b9770e37b23bc80f125bc89e6 /filedistribution/src/test | |
parent | 0f3ec8f7dc2629b22f542fbbfdf51254cc537041 (diff) |
Add support for downloading from another config server
If a request for a file reference cannot be fulfilled, ask another
config server for the file. Handle connection errors when downloading files.
Fixed bundle issues.
Diffstat (limited to 'filedistribution/src/test')
-rw-r--r-- | filedistribution/src/test/java/com/yahoo/vespa/filedistribution/FileDownloaderTest.java | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/filedistribution/src/test/java/com/yahoo/vespa/filedistribution/FileDownloaderTest.java b/filedistribution/src/test/java/com/yahoo/vespa/filedistribution/FileDownloaderTest.java index 738b0888956..278c46dab8b 100644 --- a/filedistribution/src/test/java/com/yahoo/vespa/filedistribution/FileDownloaderTest.java +++ b/filedistribution/src/test/java/com/yahoo/vespa/filedistribution/FileDownloaderTest.java @@ -27,6 +27,7 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; +import static com.yahoo.jrt.ErrorCode.CONNECTION; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -45,7 +46,7 @@ public class FileDownloaderTest { try { downloadDir = Files.createTempDirectory("filedistribution").toFile(); connection = new MockConnection(); - fileDownloader = new FileDownloader(connection, downloadDir, Duration.ofMillis(3000)); + fileDownloader = new FileDownloader(connection, downloadDir, Duration.ofMillis(2000)); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); @@ -115,6 +116,38 @@ public class FileDownloaderTest { } @Test + public void getFileWhenConnectionError() throws IOException { + fileDownloader = new FileDownloader(connection, downloadDir, Duration.ofMillis(3000)); + File downloadDir = fileDownloader.downloadDirectory(); + + int timesToFail = 2; + MockConnection.ConnectionErrorResponseHandler responseHandler = new MockConnection.ConnectionErrorResponseHandler(timesToFail); + connection.setResponseHandler(responseHandler); + + FileReference fileReference = new FileReference("fileReference"); + File fileReferenceFullPath = fileReferenceFullPath(downloadDir, fileReference); + assertFalse(fileReferenceFullPath.getAbsolutePath(), fileDownloader.getFile(fileReference).isPresent()); + + // Verify download status + assertDownloadStatus(fileDownloader, fileReference, 0.0); + + // Receives fileReference, should return and make it available to caller + String filename = "abc.jar"; + receiveFile(fileReference, filename, "some other content"); + Optional<File> downloadedFile = fileDownloader.getFile(fileReference); + + assertTrue(downloadedFile.isPresent()); + File downloadedFileFullPath = new File(fileReferenceFullPath, filename); + assertEquals(downloadedFileFullPath.getAbsolutePath(), downloadedFile.get().getAbsolutePath()); + assertEquals("some other content", IOUtils.readFile(downloadedFile.get())); + + // Verify download status when downloaded + assertDownloadStatus(fileDownloader, fileReference, 100.0); + + assertEquals(timesToFail, responseHandler.failedTimes); + } + + @Test public void setFilesToDownload() throws IOException { Duration timeout = Duration.ofMillis(200); File downloadDir = Files.createTempDirectory("filedistribution").toFile(); @@ -271,7 +304,30 @@ public class FileDownloaderTest { request.returnValues().add(new Int32Value(0)); request.returnValues().add(new StringValue("OK")); } + } + } + + static class ConnectionErrorResponseHandler implements MockConnection.ResponseHandler { + private final int timesToFail; + private int failedTimes = 0; + + ConnectionErrorResponseHandler(int timesToFail) { + super(); + this.timesToFail = timesToFail; + } + + @Override + public void request(Request request) { + if (request.methodName().equals("filedistribution.serveFile")) { + if (failedTimes < timesToFail) { + request.setError(CONNECTION, "Connection error"); + failedTimes++; + } else { + request.returnValues().add(new Int32Value(0)); + request.returnValues().add(new StringValue("OK")); + } + } } } } |