Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
qdpham13 committed Apr 23, 2024
1 parent f47a400 commit 2a4b6ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ public void run() {
}

public void setRealtimeBackgroundState(boolean backgroundState) {
// Make changes in synchronized block so that everything is updated in a single atomic
// transaction.
// Make changes in synchronized block so only one thread sets the background state and calls
// disconnect.
synchronized (isInBackground) {
isInBackground = backgroundState;
if (configAutoFetch != null) {
Expand Down Expand Up @@ -562,7 +562,7 @@ public void beginRealtimeHttpStream() {
}
} finally {
// Close HTTP connection and associated streams.
closeAllRealtimeHttpStreams(inputStream, errorStream);
closeRealtimeHttpConnection(inputStream, errorStream);
setIsHttpConnectionRunning(false);

boolean connectionFailed =
Expand Down Expand Up @@ -605,13 +605,13 @@ public void beginRealtimeHttpStream() {
});
}

private void closeHttpConnectionInputStream(InputStream stream) {
if (stream == null) {
private void closeHttpConnectionInputStream(InputStream inputStream) {
if (inputStream == null) {
return;
}

try {
stream.close();
inputStream.close();
} catch (IOException ex) {
Log.d(TAG, "Exception thrown when closing connection stream. Retrying connection...", ex);
}
Expand All @@ -620,7 +620,7 @@ private void closeHttpConnectionInputStream(InputStream stream) {
// Pauses Http stream listening by disconnecting the HttpUrlConnection and underlying InputStream
// and ErrorStream if they exist.
@VisibleForTesting
public void closeAllRealtimeHttpStreams(InputStream inputStream, InputStream errorStream) {
public void closeRealtimeHttpConnection(InputStream inputStream, InputStream errorStream) {
if (httpURLConnection != null && !isInBackground) {
httpURLConnection.disconnect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ public void realtime_redirectStatusCode_noRetries() throws Exception {
.createRealtimeConnection();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(301);

configRealtimeHttpClientSpy.beginRealtimeHttpStream();
Expand All @@ -1331,7 +1331,7 @@ public void realtime_okStatusCode_startAutofetchAndRetries() throws Exception {
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);

configRealtimeHttpClientSpy.beginRealtimeHttpStream();
Expand All @@ -1350,7 +1350,7 @@ public void realtime_badGatewayStatusCode_noAutofetchButRetries() throws Excepti
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(502);

configRealtimeHttpClientSpy.beginRealtimeHttpStream();
Expand All @@ -1369,7 +1369,7 @@ public void realtime_retryableStatusCode_increasesConfigMetadataFailedStreams()
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(502);
int failedStreams = configRealtimeHttpClientSpy.getNumberOfFailedStreams();

Expand All @@ -1388,7 +1388,7 @@ public void realtime_retryableStatusCode_increasesConfigMetadataBackoffDate() th
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(502);
Date backoffDate = configRealtimeHttpClientSpy.getBackoffEndTime();

Expand All @@ -1409,7 +1409,7 @@ public void realtime_successfulStatusCode_doesNotIncreaseConfigMetadataFailedStr
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
int failedStreams = configRealtimeHttpClientSpy.getNumberOfFailedStreams();

Expand All @@ -1430,7 +1430,7 @@ public void realtime_successfulStatusCode_doesNotIncreaseConfigMetadataBackoffDa
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
Date backoffDate = configRealtimeHttpClientSpy.getBackoffEndTime();

Expand All @@ -1448,7 +1448,7 @@ public void realtime_forbiddenStatusCode_returnsStreamError() throws Exception {
.createRealtimeConnection();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getErrorStream())
.thenReturn(
new ByteArrayInputStream(FORBIDDEN_ERROR_MESSAGE.getBytes(StandardCharsets.UTF_8)));
Expand All @@ -1471,7 +1471,7 @@ public void realtime_exceptionThrown_noAutofetchButRetries() throws Exception {
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));

configRealtimeHttpClientSpy.beginRealtimeHttpStream();
flushScheduledTasks();
Expand Down Expand Up @@ -1523,7 +1523,7 @@ public void realtime_stream_listen_backgrounded_disconnects() throws Exception {
doNothing().when(configRealtimeHttpClientSpy).retryHttpConnectionWhenBackoffEnds();
doNothing()
.when(configRealtimeHttpClientSpy)
.closeAllRealtimeHttpStreams(any(InputStream.class), any(InputStream.class));
.closeRealtimeHttpConnection(any(InputStream.class), any(InputStream.class));
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
flushScheduledTasks();
Expand Down

0 comments on commit 2a4b6ee

Please sign in to comment.