Skip to content

Commit 0e416d0

Browse files
committedJun 23, 2023
Fix JVM specific test failure due to different JVM gc() dynamics
Signed-off-by: Marko Strukelj <marko.strukelj@gmail.com>
1 parent 60bde96 commit 0e416d0

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed
 

‎oauth-common/src/main/java/io/strimzi/kafka/oauth/services/Sessions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void removeAllWithMatchingAccessToken(String accessToken) {
7373
if (accessToken.equals(token.value())) {
7474
activeSessions.remove(token);
7575
if (log.isDebugEnabled()) {
76-
log.debug("Removed invalid sessions from sessions map (userId: {}, session: {}, token: {}). Will not refresh its grants any more.",
76+
log.debug("Removed invalid session from sessions map (userId: {}, session: {}, token: {})",
7777
token.principalName(), token.getSessionId(), mask(token.value()));
7878
}
7979
}

‎testsuite/common/src/main/java/io/strimzi/testsuite/oauth/common/TestContainersWatcher.java

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.testcontainers.containers.wait.strategy.WaitStrategy;
1616

1717
import java.io.File;
18+
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
20+
import java.nio.file.Files;
1821
import java.util.ArrayList;
1922
import java.util.List;
2023
import java.util.Optional;
@@ -86,6 +89,14 @@ protected void outputLogs() {
8689
environment.getContainerByServiceName("hydra-jwt_1").ifPresent(c -> System.out.println("\n\n'hydra-jwt' log:\n\n" + c.getLogs() + "\n"));
8790
environment.getContainerByServiceName("hydra-import_1").ifPresent(c -> System.out.println("\n\n'hydra-import' log:\n\n" + c.getLogs() + "\n"));
8891
environment.getContainerByServiceName("hydra-jwt-import_1").ifPresent(c -> System.out.println("\n\n'hydra-jwt-import' log:\n\n" + c.getLogs() + "\n"));
92+
File testLog = new File("target/test.log");
93+
if (testLog.isFile()) {
94+
try {
95+
System.out.println("\n\n'mockoauth-tests' test.log:\n\n" + new String(Files.readAllBytes(testLog.toPath()), StandardCharsets.ISO_8859_1) + "\n");
96+
} catch (IOException e) {
97+
System.out.println("Failed to read file: " + testLog.getAbsolutePath());
98+
}
99+
}
89100
}
90101

91102
public void collectLogsOnExit() {

‎testsuite/mockoauth-tests/src/test/java/io/strimzi/testsuite/oauth/mockoauth/KeycloakAuthorizerTest.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.ArrayList;
4747
import java.util.Collections;
4848
import java.util.HashMap;
49+
import java.util.LinkedList;
4950
import java.util.List;
5051
import java.util.Map;
5152
import java.util.concurrent.ExecutionException;
@@ -168,7 +169,7 @@ void doGrants401Test() throws IOException, InterruptedException, TimeoutExceptio
168169
lines = waitFor(logReader, "Done refreshing grants");
169170
Assert.assertTrue("Failed to fetch", checkLogForRegex(lines, "Failed to fetch grants .* status 401"));
170171
Assert.assertTrue("Removed user from grants cache", checkLogForRegex(lines, "Removed user from grants cache: alice"));
171-
Assert.assertTrue("Removed invalid sessions", checkLogForRegex(lines, "Removed invalid sessions from sessions map \\(userId: alice"));
172+
Assert.assertTrue("Removed invalid session", checkLogForRegex(lines, "Removed invalid session from sessions map \\(userId: alice"));
172173

173174
} finally {
174175
changeAuthServerMode("grants", "MODE_200");
@@ -518,6 +519,9 @@ void doConfigTests() throws IOException {
518519
void doGrantsGCTests() throws Exception {
519520
logStart("KeycloakAuthorizerTest :: Grants Garbage Collection Tests");
520521

522+
// Hold on to the created principals to prevent JVM gc() clearing the sessions
523+
List<OAuthKafkaPrincipal> principals = new LinkedList<>();
524+
521525
// make sure the token endpoint works fine
522526
changeAuthServerMode("token", "MODE_200");
523527

@@ -558,7 +562,7 @@ void doGrantsGCTests() throws Exception {
558562

559563
LOG.info("Authenticate (validate) as gcUser1");
560564
OAuthKafkaPrincipal principal = authenticate(authHandler, tokenInfo);
561-
565+
principals.add(principal);
562566

563567
// authorization
564568

@@ -590,6 +594,7 @@ void doGrantsGCTests() throws Exception {
590594

591595
LOG.info("Authenticate (validate) gcUser2");
592596
principal = authenticate(authHandler, tokenInfo);
597+
principals.add(principal);
593598

594599
LOG.info("Waiting for: active users count: 2, grantsCache size before: 1, grantsCache size after: 1"); // Make sure to not repeat the below condition in the string here
595600
// wait for cgGrants run on 2 users

0 commit comments

Comments
 (0)
Please sign in to comment.