Skip to content

Commit

Permalink
Always close streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Seibert committed Apr 4, 2018
1 parent 1db14f9 commit 39d7862
Showing 1 changed file with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,42 +92,56 @@ protected boolean isStreamValid(String data) {
public void testInfiniteStream() throws Exception {
executeHystrixCommand(); // Execute a Hystrix command so that metrics are initialized.
EventInput stream = getStream(); // Invoke Stream API which returns a steady stream output.
validateStream(stream, 1000); // Validate the stream.
System.out.println("Validated Stream Output 1");
executeHystrixCommand(); // Execute Hystrix Command again so that request count is updated.
validateStream(stream, 1000); // Stream should show updated request count
System.out.println("Validated Stream Output 2");
stream.close();
try {
validateStream(stream, 1000); // Validate the stream.
System.out.println("Validated Stream Output 1");
executeHystrixCommand(); // Execute Hystrix Command again so that request count is updated.
validateStream(stream, 1000); // Stream should show updated request count
System.out.println("Validated Stream Output 2");
} finally {
if (stream != null) {
stream.close();
}
}
}

@Test
public void testConcurrency() throws Exception {
executeHystrixCommand(); // Execute a Hystrix command so that metrics are initialized.
List<EventInput> streamList = new ArrayList<EventInput>();
// Fire 5 requests, validate their responses and hold these connections.
for (int i = 0; i < 5; i++) {
EventInput stream = getStream();
System.out.println("Received Response for Request#" + (i + 1));
streamList.add(stream);
validateStream(stream, 1000);
System.out.println("Validated Response#" + (i + 1));
}

// Sixth request should fail since max configured connection is 5.
try {
streamList.add(getStreamFailFast());
Assert.fail("Expected 'ServiceUnavailableException' but, request went through.");
} catch (ServiceUnavailableException e) {
System.out.println("Got ServiceUnavailableException as expected.");
}
// Fire 5 requests, validate their responses and hold these connections.
for (int i = 0; i < 5; i++) {
EventInput stream = getStream();
System.out.println("Received Response for Request#" + (i + 1));
streamList.add(stream);
validateStream(stream, 1000);
System.out.println("Validated Response#" + (i + 1));
}

// Close one of the connections
streamList.get(0).close();
// Sixth request should fail since max configured connection is 5.
try {
streamList.add(getStreamFailFast());
Assert.fail("Expected 'ServiceUnavailableException' but, request went through.");
} catch (ServiceUnavailableException e) {
System.out.println("Got ServiceUnavailableException as expected.");
}

// Try again after closing one of the connections. This request should go through.
EventInput eventInput = getStream();
streamList.add(eventInput);
validateStream(eventInput, 1000);
// Close one of the connections
streamList.get(0).close();
streamList.remove(0);

// Try again after closing one of the connections. This request should go through.
EventInput eventInput = getStream();
streamList.add(eventInput);
validateStream(eventInput, 1000);
} finally {
for (EventInput stream : streamList) {
if (stream != null) {
stream.close();
}
}
}

}

Expand Down

0 comments on commit 39d7862

Please sign in to comment.