fix(codeartifact): retry npm GET transport failures once (#9010)

* fix(codeartifact): retry npm GET transport failures once

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-19 13:50:41 -04:00
1 parent 9f0390b340
commit 76f9481bb1
2 files changed
+123 -1

No files matched your search

@@ -6,9 +6,14 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -21,6 +26,11 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* The wire contract towards the real public npm registry, exercised against a local {@code
@@ -63,6 +73,103 @@ class NpmExternalConnectionClientTest {
assertArrayEquals(content, result.get().content());
}
@Test
void fetchPackumentRetriesAConnectionResetBeforeReceivingTheBody() throws Exception {
HttpClient transport = mock(HttpClient.class);
HttpResponse<InputStream> response = mock(HttpResponse.class);
byte[] content = "{\"name\":\"widget\"}".getBytes(StandardCharsets.UTF_8);
when(response.statusCode()).thenReturn(200);
when(response.body()).thenReturn(new ByteArrayInputStream(content));
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenThrow(new IOException("Connection reset"))
.thenReturn(response);
NpmExternalConnectionClient retryClient =
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
Optional<NpmExternalConnectionClient.FetchedAsset> result =
retryClient.fetchPackument("fake", null, "widget");
assertTrue(result.isPresent());
assertArrayEquals(content, result.get().content());
verify(transport, times(2)).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
}
@Test
void fetchPackumentDoesNotRetryARequestTimeout() throws Exception {
HttpClient transport = mock(HttpClient.class);
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenThrow(new HttpTimeoutException("request timed out"));
NpmExternalConnectionClient retryClient =
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
}
@Test
void fetchPackumentStopsAfterTwoFailedTransportAttempts() throws Exception {
HttpClient transport = mock(HttpClient.class);
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenThrow(new IOException("Connection reset"));
NpmExternalConnectionClient retryClient =
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
verify(transport, times(2)).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
}
@Test
void fetchPackumentDoesNotRetryAnHttpNotFoundResponse() throws Exception {
HttpClient transport = mock(HttpClient.class);
HttpResponse<InputStream> response = mock(HttpResponse.class);
when(response.statusCode()).thenReturn(404);
when(response.body()).thenReturn(new ByteArrayInputStream(new byte[0]));
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(response);
NpmExternalConnectionClient retryClient =
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
}
@Test
void fetchPackumentDoesNotReplayAPartiallyConsumedBody() throws Exception {
HttpClient transport = mock(HttpClient.class);
HttpResponse<InputStream> response = mock(HttpResponse.class);
InputStream body = mock(InputStream.class);
when(response.statusCode()).thenReturn(200);
when(response.body()).thenReturn(body);
when(body.transferTo(any())).thenThrow(new IOException("Connection reset while reading"));
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(response);
NpmExternalConnectionClient retryClient =
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
verify(body).close();
}
@Test
void fetchPackumentPreservesInterruptionWithoutRetrying() throws Exception {
HttpClient transport = mock(HttpClient.class);
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
.thenThrow(new InterruptedException("cancelled"));
NpmExternalConnectionClient retryClient =
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
try {
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
assertTrue(Thread.currentThread().isInterrupted());
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
} finally {
Thread.interrupted();
}
}
/**
* Confirmed live against the real registry: a scoped package's single-package resource needs
* its scope separator percent-encoded as {@code %2F} to reach the package itself rather than a