Skip to content

Commit

Permalink
Test the retry of downloadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Sep 11, 2023
1 parent b2a588a commit c21f9c1
Showing 1 changed file with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.jenkins.plugins.httpclient;

import static org.hamcrest.MatcherAssert.*;
Expand All @@ -30,6 +29,7 @@

import hudson.AbortException;
import hudson.model.TaskListener;
import hudson.model.UnprotectedRootAction;
import java.io.File;
import java.io.IOException;
import java.net.URL;
Expand All @@ -42,6 +42,9 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;

public class RobustHTTPClientIntegrationTest {

Expand All @@ -66,16 +69,37 @@ public void testDownloadFile() throws Exception {
assertThat(Files.readString(f.toPath()), containsString("<title>Dashboard [Jenkins]</title>"));
}

/**
* Wait no more than TIMEOUT seconds for response
*/
private static final int TIMEOUT = 2;

@Test
public void testDownloadFileWithTimeoutException() throws Exception {
client.setStopAfterAttemptNumber(1);
client.setTimeout(50, TimeUnit.MICROSECONDS);
client.setTimeout(TIMEOUT, TimeUnit.SECONDS);
URL hangingURL = new URL(j.getURL().toString() + "intentionally-hangs-always");
final IOException e = assertThrows(IOException.class, () -> {
client.downloadFile(f, j.getURL(), TaskListener.NULL);
client.downloadFile(f, hangingURL, TaskListener.NULL);
});
assertThat(e.getCause(), isA(TimeoutException.class));
}

@Test
public void testDownloadFileWithRetry() throws Exception {
// Try up to three times, should succeed on second try
client.setStopAfterAttemptNumber(3);
// Wait 213 milliseconds before retry (no need for a long wait)
client.setWaitMultiplier(213, TimeUnit.MILLISECONDS);
// Wait no more than 3x the TIMEOUT between retries (upper bound)
client.setWaitMaximum(TIMEOUT * 3, TimeUnit.SECONDS);
// Retry if no response in TIMEOUT seconds
client.setTimeout(TIMEOUT, TimeUnit.SECONDS);
URL hangsOnceURL = new URL(j.getURL().toString() + "intentionally-hangs-once");
client.downloadFile(f, hangsOnceURL, TaskListener.NULL);
assertThat(Files.readString(f.toPath()), containsString("a-dubious-response-from-hangs-once"));
}

@Test
public void testDownloadFileFromNonExistentLocation() throws Exception {
URL badURL = new URL(j.getURL().toString() + "/page/does/not/exist");
Expand All @@ -94,4 +118,68 @@ public void testDownloadFileStopAfterOneAttempt() throws Exception {
});
assertThat(e.getMessage(), containsString("Failed to download "));
}

@TestExtension
public static class IntentionallyHangsAlwaysAction implements UnprotectedRootAction {

@Override
public String getIconFileName() {
return null;
}

@Override
public String getDisplayName() {
return null;
}

@Override
public String getUrlName() {
return "intentionally-hangs-always";
}

public HttpResponse doIndex() {
try {
// Sleep 3x longer than the timeout
Thread.sleep(TIMEOUT * 3L * 1000L);
} catch (InterruptedException ie) {
// Ignore the exception
}
return HttpResponses.text("a-dubious-response-from-hangs-always");
}
}

@TestExtension
public static class IntentionallyHangsOnceAction implements UnprotectedRootAction {

private int requestCount = 0;

@Override
public String getIconFileName() {
return null;
}

@Override
public String getDisplayName() {
return null;
}

@Override
public String getUrlName() {
return "intentionally-hangs-once";
}

public HttpResponse doIndex() {
requestCount++;
if (requestCount <= 1) {
/* Wait 3x longer than timeout on first request */
try {
// Sleep 3x longer than the timeout
Thread.sleep(TIMEOUT * 3L * 1000L);
} catch (InterruptedException ie) {
// Ignore the exception
}
}
return HttpResponses.text("a-dubious-response-from-hangs-once");
}
}
}

0 comments on commit c21f9c1

Please sign in to comment.