Skip to content

Commit

Permalink
Merge pull request #45 from launchdarkly/eb/ch112576/shutdown-on-error
Browse files Browse the repository at this point in the history
fix logic for shutting down after an unrecoverable error
  • Loading branch information
eli-darkly authored Jun 24, 2021
2 parents 54fa96f + aea06fe commit 989d4e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main/java/com/launchdarkly/eventsource/EventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,21 +300,22 @@ private void newConnectionAttempt(AtomicLong connectedTime) {
if (response.isSuccessful()) {
connectedTime.set(System.currentTimeMillis());
handleSuccessfulResponse(response);

// If handleSuccessfulResponse returned without throwing an exception, it means the server
// ended the stream. We don't call the handler's onError() method in this case; but we will
// call the ConnectionErrorHandler with an EOFException, in case it wants to do something
// special in this scenario (like choose not to retry the connection). However, first we
// should check the state in case we've been deliberately closed from elsewhere.
ReadyState state = readyState.get();
if (state != SHUTDOWN && state != CLOSED) {
logger.warn("Connection unexpectedly closed");
errorHandlerAction = connectionErrorHandler.onConnectionError(new EOFException());
}
} else {
logger.debug("Unsuccessful response: {}", response);
errorHandlerAction = dispatchError(new UnsuccessfulResponseException(response.code()));
}
}
// If handleSuccessfulResponse returned without throwing an exception, it means the server
// ended the stream. We don't call the handler's onError() method in this case; but we will
// call the ConnectionErrorHandler with an EOFException, in case it wants to do something
// special in this scenario (like choose not to retry the connection). However, first we
// should check the state in case we've been deliberately closed from elsewhere.
ReadyState state = readyState.get();
if (state != SHUTDOWN && state != CLOSED) {
logger.warn("Connection unexpectedly closed");
errorHandlerAction = connectionErrorHandler.onConnectionError(new EOFException());
}
} catch (IOException e) {
ReadyState state = readyState.get();
if (state != SHUTDOWN && state != CLOSED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.launchdarkly.eventsource.StubServer.Handlers.stream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
* End-to-end tests with real HTTP, specifically for the client's reconnect behavior.
Expand Down Expand Up @@ -211,6 +212,10 @@ public Action onConnectionError(Throwable t) {
// Therefore we don't expect to see any items in eventSink.
eventSink.assertNoMoreLogItems();

assertNotNull(server.awaitRequest(Duration.ZERO));
assertNull(server.awaitRequest(Duration.ZERO)); // no more requests should have been made
assertEquals(0, receivedError.size()); // error handler should have only been called once

assertEquals(ReadyState.SHUTDOWN, es.getState());
}
}
Expand Down

0 comments on commit 989d4e6

Please sign in to comment.