Skip to content

Commit

Permalink
Improve robustness of stream socket tests
Browse files Browse the repository at this point in the history
Make the test more robust by waiting for the expected error state to
be reached. "broken pipe" is the initial error sender gets when trying
to write to the disconnected socket, but it can not be reliably
observed due to lack of synchronization between the sender thread and
the test body.
  • Loading branch information
vickenty committed Mar 18, 2024
1 parent c18559c commit 8b33561
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/test/java/com/timgroup/statsd/UnixStreamSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public synchronized void handle(Exception exception) {
lastException = exception;
}

synchronized boolean lastExceptionMessageContains(String s) {
String msg = lastException.getMessage();
return msg != null && msg.contains(s);
}

@BeforeClass
public static void supportedOnly() throws IOException {
Assume.assumeTrue(TestHelpers.isUdsAvailable());
Expand Down Expand Up @@ -108,22 +113,19 @@ public void resist_dsd_restart() throws Exception {

// Close the server, client should throw an IOException
server.close();
while(lastException.getMessage() == null) {
while(!lastExceptionMessageContains("Connection refused")) {
client.gauge("mycount", 20);
Thread.sleep(10);
}
// Depending on the state of the client at that point we might get different messages.
assertThat(lastException.getMessage(), anyOf(containsString("Connection refused"), containsString("Broken pipe")));

// Delete the socket file, client should throw an IOException
lastException = new Exception();
socketFile.delete();

client.gauge("mycount", 21);
while(lastException.getMessage() == null) {
while(lastExceptionMessageContains("No such file or directory")) {
Thread.sleep(10);
}
assertThat(lastException.getMessage(), containsString("No such file or directory"));

// Re-open the server, next send should work OK
DummyStatsDServer server2;
Expand Down

0 comments on commit 8b33561

Please sign in to comment.