From f8e666d81742755b1ef9385791a862654b35bee3 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Thu, 19 May 2022 20:22:56 -0700 Subject: [PATCH] Improve SpringSecurityTests Sometimes TestSpanHandler does not return the spans in time so these tests could skip the verification steps and not fail even if they should. These changes ensure that the tests will fail in these cases. --- .../sleuth/instrument/security/SpringSecurityTests.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/common/src/main/java/org/springframework/cloud/sleuth/instrument/security/SpringSecurityTests.java b/tests/common/src/main/java/org/springframework/cloud/sleuth/instrument/security/SpringSecurityTests.java index 838b378d5..3e2046de7 100644 --- a/tests/common/src/main/java/org/springframework/cloud/sleuth/instrument/security/SpringSecurityTests.java +++ b/tests/common/src/main/java/org/springframework/cloud/sleuth/instrument/security/SpringSecurityTests.java @@ -16,11 +16,13 @@ package org.springframework.cloud.sleuth.instrument.security; +import java.time.Duration; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -69,8 +71,10 @@ public abstract class SpringSecurityTests { then(entity.getStatusCode().is2xxSuccessful()).isTrue(); then(entity.getBody()).isEqualTo("authenticated"); + Awaitility.await().atMost(Duration.ofSeconds(1)).untilAsserted(() -> then(testSpanHandler).isNotEmpty()); List> authEvents = getAuthEvents(testSpanHandler.reportedSpans()); + then(authEvents).isNotEmpty(); for (int i = 0; i < authEvents.size(); i += 2) { String setEvent = authEvents.get(i).getValue(); then(setEvent).isEqualTo("Authentication set UsernamePasswordAuthenticationToken[USER]"); @@ -85,8 +89,10 @@ public abstract class SpringSecurityTests { then(entity.getStatusCode().is2xxSuccessful()).isTrue(); then(entity.getBody()).contains("html", "form"); + Awaitility.await().atMost(Duration.ofSeconds(1)).untilAsserted(() -> then(testSpanHandler).isNotEmpty()); List> authEvents = getAuthEvents(testSpanHandler.reportedSpans()); + then(authEvents).isNotEmpty(); for (int i = 0; i < authEvents.size(); i += 2) { String setEvent = authEvents.get(i).getValue(); then(setEvent).isEqualTo("Authentication set AnonymousAuthenticationToken[ROLE_ANONYMOUS]");