Skip to content

Commit

Permalink
Reduce flakiness of LayoutIntegrationTests (#3537)
Browse files Browse the repository at this point in the history
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 null-check as `objectMapper.readTree()` will never return `null` in Jackson 2.10.x, see FasterXML/jackson-databind#2211

(cherry picked from commit bdb829c)
  • Loading branch information
cleiner authored and joschi committed Nov 3, 2020
1 parent 40a294f commit d11e240
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions dropwizard-json-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<artifactId>dropwizard-configuration</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

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

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

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

JsonNode jsonNode = objectMapper.readTree(redirectedStream.toString());
assertThat(jsonNode).isNotNull();
assertThat(jsonNode.get("timestamp").isNumber()).isTrue();
assertThat(jsonNode.get("requestTime").isNumber()).isTrue();
assertThat(jsonNode.get("remoteAddress").asText()).isEqualTo("10.0.0.1");
Expand Down

0 comments on commit d11e240

Please sign in to comment.