Skip to content

Commit fe54e13

Browse files
committed
Reduce flakiness of LayoutIntegrationTests
Two of the tests were waiting for log output using a one-off call to Thread.sleep(). In addition to being brittle, the tests would fail with an NPE (while trying to assert on the "timestamp" field) that doesn't point to the cause of the failure directly. Also removed the redundant assertThat(jsonNode).isNotNull() as objectMapper.readTree() will never return null in Jackson 2.10.x, see FasterXML/jackson-databind#2211
1 parent 4603a36 commit fe54e13

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

dropwizard-json-logging/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
<artifactId>dropwizard-configuration</artifactId>
117117
<scope>test</scope>
118118
</dependency>
119+
<dependency>
120+
<groupId>org.awaitility</groupId>
121+
<artifactId>awaitility</artifactId>
122+
<scope>test</scope>
123+
</dependency>
119124
</dependencies>
120125

121126
</project>

dropwizard-json-logging/src/test/java/io/dropwizard/logging/json/LayoutIntegrationTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static java.util.Objects.requireNonNull;
3838
import static org.assertj.core.api.Assertions.assertThat;
3939
import static org.assertj.core.api.Assertions.entry;
40+
import static org.awaitility.Awaitility.await;
4041
import static org.mockito.Mockito.mock;
4142
import static org.mockito.Mockito.when;
4243

@@ -149,10 +150,10 @@ public void testLogJsonToConsole() throws Exception {
149150
defaultLoggingFactory.configure(new MetricRegistry(), "json-log-test");
150151
Marker marker = MarkerFactory.getMarker("marker");
151152
LoggerFactory.getLogger("com.example.app").info(marker, "Application log");
152-
Thread.sleep(100); // Need to wait, because the logger is async
153+
// Need to wait, because the logger is async
154+
await().atMost(1, TimeUnit.SECONDS).until(() -> !redirectedStream.toString().isEmpty());
153155

154156
JsonNode jsonNode = objectMapper.readTree(redirectedStream.toString());
155-
assertThat(jsonNode).isNotNull();
156157
assertThat(jsonNode.get("timestamp").isTextual()).isTrue();
157158
assertThat(jsonNode.get("level").asText()).isEqualTo("INFO");
158159
assertThat(jsonNode.get("logger").asText()).isEqualTo("com.example.app");
@@ -204,10 +205,10 @@ public void testLogAccessJsonToConsole() throws Exception {
204205
when(response.getHeader("Server")).thenReturn("Apache/2.4.12");
205206

206207
requestLog.log(request, response);
207-
Thread.sleep(100); // Need to wait, because the logger is async
208+
// Need to wait, because the logger is async
209+
await().atMost(1, TimeUnit.SECONDS).until(() -> !redirectedStream.toString().isEmpty());
208210

209211
JsonNode jsonNode = objectMapper.readTree(redirectedStream.toString());
210-
assertThat(jsonNode).isNotNull();
211212
assertThat(jsonNode.get("timestamp").isNumber()).isTrue();
212213
assertThat(jsonNode.get("requestTime").isNumber()).isTrue();
213214
assertThat(jsonNode.get("remoteAddress").asText()).isEqualTo("10.0.0.1");

0 commit comments

Comments
 (0)