From 81dfbfd4749fd7740eb867deec403edc2a3d3484 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Sun, 18 Oct 2026 16:28:21 +0200 Subject: [PATCH] Fixed flaky TwoFactorAuthTest pre-verification token WebSocket test The server accepts the WebSocket handshake and closes the session right away when it rejects a pre-verification token in afterConnectionEstablished. Java-WebSocket's connectBlocking() releases its latch on both open and close and then returns engine.isOpen(), so it returns false when the client reads the close frame before the caller checks the state. The asserted connect in buildAndConnectWebSocketClient then failed. The test now connects without asserting the connect result, waits for the close, and asserts the server close code (1011), so it still proves the token was rejected. --- .../server/controller/AbstractControllerTest.java | 6 +++++- .../server/controller/TbTestWebSocketClient.java | 3 +++ .../thingsboard/server/controller/TwoFactorAuthTest.java | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/AbstractControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/AbstractControllerTest.java index aaa878193f..6b7b0201f4 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AbstractControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AbstractControllerTest.java @@ -92,7 +92,7 @@ public abstract class AbstractControllerTest extends AbstractNotifyEntityTest { } protected TbTestWebSocketClient buildAndConnectWebSocketClient(String path) throws URISyntaxException, InterruptedException { - TbTestWebSocketClient wsClient = new TbTestWebSocketClient(new URI(WS_URL + wsPort + path)); + TbTestWebSocketClient wsClient = buildWebSocketClient(path); assertThat(wsClient.connectBlocking(TIMEOUT, TimeUnit.SECONDS)).isTrue(); if (!path.contains("token=")) { wsClient.authenticate(token); @@ -100,4 +100,8 @@ public abstract class AbstractControllerTest extends AbstractNotifyEntityTest { return wsClient; } + protected TbTestWebSocketClient buildWebSocketClient(String path) throws URISyntaxException { + return new TbTestWebSocketClient(new URI(WS_URL + wsPort + path)); + } + } diff --git a/application/src/test/java/org/thingsboard/server/controller/TbTestWebSocketClient.java b/application/src/test/java/org/thingsboard/server/controller/TbTestWebSocketClient.java index 4a9f2673a9..1b5817ac21 100644 --- a/application/src/test/java/org/thingsboard/server/controller/TbTestWebSocketClient.java +++ b/application/src/test/java/org/thingsboard/server/controller/TbTestWebSocketClient.java @@ -42,6 +42,8 @@ public class TbTestWebSocketClient extends WebSocketClient { @Getter private volatile String lastMsg; + @Getter + private volatile Integer closeCode; private volatile CountDownLatch reply; private volatile CountDownLatch update; @@ -74,6 +76,7 @@ public class TbTestWebSocketClient extends WebSocketClient { @Override public void onClose(int i, String s, boolean b) { + closeCode = i; log.info("CLOSED."); } diff --git a/application/src/test/java/org/thingsboard/server/controller/TwoFactorAuthTest.java b/application/src/test/java/org/thingsboard/server/controller/TwoFactorAuthTest.java index 4618ebf4e0..99b500de1e 100644 --- a/application/src/test/java/org/thingsboard/server/controller/TwoFactorAuthTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/TwoFactorAuthTest.java @@ -4,6 +4,7 @@ package org.thingsboard.server.controller; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; +import org.java_websocket.framing.CloseFrame; import org.jboss.aerogear.security.otp.Totp; import org.junit.After; import org.junit.Before; @@ -161,9 +162,14 @@ public class TwoFactorAuthTest extends AbstractControllerTest { configureTotpTwoFa(); logInWithPreVerificationToken(username, password); - wsClient = buildAndConnectWebSocketClient("/api/ws?token=" + token); + // The server accepts the handshake and closes the session right away from afterConnectionEstablished(). + // connectBlocking() returns false when that close frame is processed before it checks the open state, + // so its result is not asserted here; the server-side close code proves the token was rejected. + wsClient = buildWebSocketClient("/api/ws?token=" + token); + wsClient.connectBlocking(TIMEOUT, TimeUnit.SECONDS); await().atMost(TIMEOUT, TimeUnit.SECONDS).until(wsClient::isClosed); + assertThat(wsClient.getCloseCode()).isEqualTo(CloseFrame.UNEXPECTED_CONDITION); } @Test