Skip to content

Commit d7744af

Browse files
committed
fix tests
1 parent 45ae988 commit d7744af

5 files changed

Lines changed: 338 additions & 411 deletions

File tree

src/main/java/info/unterrainer/websocketserver/WebsocketServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public WebsocketServer wsOauth(String path, WsOauthHandlerBase handler) {
155155
handler.setTokenHandler(tokenManager);
156156
wss.ws(path, ws -> {
157157
ws.onConnect(handler::onConnect);
158-
ws.onMessage(handler::onMessage);
158+
ws.onMessage(handler::onMsg);
159159
ws.onBinaryMessage(handler::onBinaryMsg);
160160
ws.onClose(handler::onClose);
161161
ws.onError(handler::onError);

src/main/java/info/unterrainer/websocketserver/WsOauthHandlerBase.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void removeClient(Session session) {
6969
log.debug("(" + name + ") Removing client: [{}]", session.getRemoteAddress());
7070
clientsConnected.removeIf(client -> client.session.equals(session));
7171
clientsQuarantined.removeIf(client -> client.session.equals(session));
72+
tenantIdsBySession.remove(session);
7273
}
7374

7475
public WsConnectContext getClient(Session session) {
@@ -93,6 +94,10 @@ public boolean isConnected(Session session) {
9394

9495
@Override
9596
public void onConnect(WsConnectContext ctx) throws Exception {
97+
handleConnect(ctx);
98+
}
99+
100+
protected void handleConnect(WsConnectContext ctx) throws Exception {
96101
log.debug("(" + name + ") New client tries to connect: [{}]", ctx.session.getRemoteAddress());
97102
String token = ctx.header("Authorization");
98103
if (token == null || token.isEmpty()) {
@@ -116,9 +121,10 @@ public void onConnect(WsConnectContext ctx) throws Exception {
116121

117122
@Override
118123
public void onMsg(WsMessageContext ctx) throws Exception {
124+
handleMessage(ctx);
119125
}
120126

121-
public final void onMessage(WsMessageContext ctx) throws Exception {
127+
protected void handleMessage(WsMessageContext ctx) throws Exception {
122128
log.debug("(" + name + ") Received from [{}]: [{}]", ctx.session.getRemoteAddress(), ctx.message());
123129
if (isQuarantined(ctx.session)) {
124130
log.warn(
@@ -153,9 +159,10 @@ public final void onMessage(WsMessageContext ctx) throws Exception {
153159

154160
@Override
155161
public void onBinaryMsg(WsBinaryMessageContext ctx) throws Exception {
162+
handleBinaryMessage(ctx);
156163
}
157164

158-
public void onBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
165+
protected void handleBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
159166
log.debug("(" + name + ") Received binary message from [{}]: [{}] bytes", ctx.session.getRemoteAddress(),
160167
ctx.data().length);
161168
if (isQuarantined(ctx.session)) {
@@ -170,12 +177,20 @@ public void onBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
170177

171178
@Override
172179
public void onClose(WsCloseContext ctx) throws Exception {
180+
handleClose(ctx);
181+
}
182+
183+
protected void handleClose(WsCloseContext ctx) throws Exception {
173184
log.debug("(" + name + ") Disconnected client: [{}]", ctx.session.getRemoteAddress());
174185
removeClient(ctx.session);
175186
}
176187

177188
@Override
178189
public void onError(WsErrorContext ctx) throws Exception {
190+
handleError(ctx);
191+
}
192+
193+
protected void handleError(WsErrorContext ctx) throws Exception {
179194
Throwable t = ctx.error();
180195
if (t instanceof EOFException || t instanceof IOException) {
181196
log.debug("(" + name + ") Client disconnected [{}].", ctx.session.getRemoteAddress());

src/test/java/info/unterrainer/websocketserver/WebsocketReconnectionIntegrationTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void testClientDisconnectsAndReconnects() throws Exception {
9797
assertThat(connectionIds).hasSize(2);
9898

9999
session2.close();
100+
server.stop();
100101
}
101102

102103
@Test
@@ -145,14 +146,14 @@ void testServerForcesDisconnectAndClientReconnects() throws Exception {
145146
assertThat(session2.isOpen()).isTrue();
146147

147148
session2.close();
149+
server.stop();
148150
}
149151

150152
@Test
151153
void testMessageLossOnReconnection() throws Exception {
152154
server = new WebsocketServer("message-loss-server");
153155
CountDownLatch firstConnect = new CountDownLatch(1);
154156
CountDownLatch secondConnect = new CountDownLatch(1);
155-
AtomicInteger messageCounter = new AtomicInteger(0);
156157
List<Integer> receivedMessages = new ArrayList<>();
157158

158159
server.ws("/message-loss", ws -> {
@@ -211,6 +212,7 @@ void testMessageLossOnReconnection() throws Exception {
211212
assertThat(receivedMessages).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
212213

213214
session2.close();
215+
server.stop();
214216
}
215217

216218
@Test
@@ -250,6 +252,7 @@ void testRapidReconnectionAttempts() throws Exception {
250252

251253
assertThat(connectionCount.get()).isEqualTo(attempts);
252254
assertThat(disconnectionCount.get()).isEqualTo(attempts);
255+
server.stop();
253256
}
254257

255258
@Test
@@ -297,6 +300,7 @@ void testReconnectionWithDifferentClientInstance() throws Exception {
297300

298301
session2.close();
299302
client2.stop();
303+
server.stop();
300304
}
301305

302306
@Test
@@ -366,6 +370,7 @@ void testReconnectionPreservesSessionState() throws Exception {
366370
assertThat(firstSessionId.get()).isNotEqualTo(secondSessionId.get());
367371

368372
session2.close();
373+
server.stop();
369374
}
370375

371376
@Test
@@ -399,6 +404,7 @@ void testNetworkInterruptionSimulation() throws Exception {
399404

400405
// Handler should detect the closure
401406
assertThat(handler.closeLatch.await(5, TimeUnit.SECONDS)).isTrue();
407+
server.stop();
402408
}
403409

404410
@WebSocket

src/test/java/info/unterrainer/websocketserver/WsHandlerBaseTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class WsHandlerBaseTest {
3131

3232
@BeforeEach
3333
void setUp() {
34+
// Initialize mocks explicitly to avoid relying on Mockito annotations processing
35+
connectContext = mock(WsConnectContext.class);
36+
messageContext = mock(WsMessageContext.class);
37+
binaryMessageContext = mock(WsBinaryMessageContext.class);
38+
closeContext = mock(WsCloseContext.class);
39+
errorContext = mock(WsErrorContext.class);
3440
handler = new TestWsHandler();
3541
}
3642

@@ -157,4 +163,4 @@ public void onError(WsErrorContext ctx) throws Exception {
157163
lastError = ctx.error();
158164
}
159165
}
160-
}
166+
}

0 commit comments

Comments
 (0)