Skip to content

Commit 8b33561

Browse files
committed
Improve robustness of stream socket tests
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.
1 parent c18559c commit 8b33561

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/test/java/com/timgroup/statsd/UnixStreamSocketTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public synchronized void handle(Exception exception) {
3434
lastException = exception;
3535
}
3636

37+
synchronized boolean lastExceptionMessageContains(String s) {
38+
String msg = lastException.getMessage();
39+
return msg != null && msg.contains(s);
40+
}
41+
3742
@BeforeClass
3843
public static void supportedOnly() throws IOException {
3944
Assume.assumeTrue(TestHelpers.isUdsAvailable());
@@ -108,22 +113,19 @@ public void resist_dsd_restart() throws Exception {
108113

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

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

122125
client.gauge("mycount", 21);
123-
while(lastException.getMessage() == null) {
126+
while(lastExceptionMessageContains("No such file or directory")) {
124127
Thread.sleep(10);
125128
}
126-
assertThat(lastException.getMessage(), containsString("No such file or directory"));
127129

128130
// Re-open the server, next send should work OK
129131
DummyStatsDServer server2;

0 commit comments

Comments
 (0)