Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ApiClientUtils.getDefaultTransport() should return a cached instance to an ApacheHttp2Transport #1059

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static JsonFactory getDefaultJsonFactory() {
}

public static HttpTransport getDefaultTransport() {
return new ApacheHttp2Transport();
return TransportInstanceHolder.INSTANCE;
}

private static class TransportInstanceHolder {
static final HttpTransport INSTANCE = new ApacheHttp2Transport();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ public void testReadTimeoutAuthorizedPost() throws FirebaseException {

@Test(timeout = 10_000L)
public void testWriteTimeoutAuthorizedGet() throws FirebaseException {
// Use a fresh transport so that writeTimeout triggers while waiting for the transport to
// be ready to receive data.
app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(MOCK_CREDENTIALS)
.setWriteTimeout(100)
.setHttpTransport(new ApacheHttp2Transport())
.build(), "test-app");
ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, app);
HttpRequestInfo request = HttpRequestInfo.buildGetRequest(GET_URL);
Expand All @@ -217,9 +220,12 @@ public void testWriteTimeoutAuthorizedGet() throws FirebaseException {

@Test(timeout = 10_000L)
public void testWriteTimeoutAuthorizedPost() throws FirebaseException {
// Use a fresh transport so that writeTimeout triggers while waiting for the transport to
// be ready to receive data.
app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(MOCK_CREDENTIALS)
.setWriteTimeout(100)
.setHttpTransport(new ApacheHttp2Transport())
.build(), "test-app");
ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, app);
HttpRequestInfo request = HttpRequestInfo.buildJsonPostRequest(POST_URL, payload);
Expand Down Expand Up @@ -292,6 +298,21 @@ public void testVerifyProxyIsRespected() {
}
}

@Test
public void testVerifyDefaultTransportReused() {
FirebaseOptions o1 = FirebaseOptions.builder()
.setCredentials(MOCK_CREDENTIALS)
.build();

FirebaseOptions o2 = FirebaseOptions.builder()
.setCredentials(MOCK_CREDENTIALS)
.build();

HttpTransport t1 = o1.getHttpTransport();
HttpTransport t2 = o2.getHttpTransport();
assertEquals(t1, t2);
}

private static ErrorHandlingHttpClient<FirebaseException> getHttpClient(boolean authorized,
FirebaseApp app) {
HttpRequestFactory requestFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
Expand Down Expand Up @@ -128,4 +129,12 @@ public void disconnect() throws IOException {

assertTrue(lowLevelResponse.isDisconnected());
}

@Test
public void testVerifyDefaultTransportReused() {
HttpTransport t1 = ApiClientUtils.getDefaultTransport();
HttpTransport t2 = ApiClientUtils.getDefaultTransport();
assertEquals(t1, t2);
}

}
Loading