Browse Source
A subscription command can register subscriptions after its context was stopped by a concurrent session cleanup, or create a context after the session was already removed. The registrations then outlive the session and keep answering alarm updates with queries whose results are dropped. - Serialize TbAbstractSubCtx.stop() with subscription registration through a lifecycle lock; registerSubscription() refuses stopped contexts and sessions unknown to the websocket service. - Route all context-owned addSubscription calls through registerSubscription(). - Skip alarm fetches on stopped contexts and ignore alarm and entity data commands for sessions that are no longer registered. - Add WebSocketService.hasSession(). - Unit tests for the registration guard and the stop/registration ordering.pull/16646/head
11 changed files with 192 additions and 15 deletions
@ -0,0 +1,115 @@ |
|||
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.subscription; |
|||
|
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.extension.ExtendWith; |
|||
import org.mockito.junit.jupiter.MockitoExtension; |
|||
import org.thingsboard.server.service.ws.WebSocketService; |
|||
import org.thingsboard.server.service.ws.WebSocketSessionRef; |
|||
|
|||
import java.util.List; |
|||
import java.util.concurrent.CopyOnWriteArrayList; |
|||
import java.util.concurrent.CountDownLatch; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.BDDMockito.willAnswer; |
|||
import static org.mockito.Mockito.lenient; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.never; |
|||
import static org.mockito.Mockito.times; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
public class TbAbstractSubCtxTest { |
|||
|
|||
private static final String SESSION_ID = "session-1"; |
|||
private static final int CMD_ID = 7; |
|||
|
|||
private final WebSocketService wsService = mock(WebSocketService.class); |
|||
private final TbLocalSubscriptionService localSubscriptionService = mock(TbLocalSubscriptionService.class); |
|||
private final WebSocketSessionRef sessionRef = mock(WebSocketSessionRef.class); |
|||
private final TbSubscription<?> subscription = mock(TbSubscription.class); |
|||
|
|||
private TbAbstractSubCtx subCtx; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
lenient().when(sessionRef.getSessionId()).thenReturn(SESSION_ID); |
|||
subCtx = new TbAbstractSubCtx("service", wsService, localSubscriptionService, mock(SubscriptionServiceStatistics.class), |
|||
sessionRef, CMD_ID) { |
|||
@Override |
|||
public boolean isDynamic() { |
|||
return false; |
|||
} |
|||
}; |
|||
} |
|||
|
|||
@Test |
|||
public void registersSubscriptionWhenContextActiveAndSessionOpen() { |
|||
when(wsService.hasSession(SESSION_ID)).thenReturn(true); |
|||
|
|||
subCtx.registerSubscription(subscription); |
|||
|
|||
verify(localSubscriptionService).addSubscription(subscription, sessionRef); |
|||
} |
|||
|
|||
@Test |
|||
public void doesNotRegisterSubscriptionAfterStop() { |
|||
subCtx.stop(); |
|||
|
|||
subCtx.registerSubscription(subscription); |
|||
|
|||
verify(localSubscriptionService, never()).addSubscription(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
public void doesNotRegisterSubscriptionForSessionUnknownToWebSocketService() { |
|||
when(wsService.hasSession(SESSION_ID)).thenReturn(false); |
|||
|
|||
subCtx.registerSubscription(subscription); |
|||
|
|||
verify(localSubscriptionService, never()).addSubscription(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
public void stopWaitsForInFlightRegistration() throws Exception { |
|||
when(wsService.hasSession(SESSION_ID)).thenReturn(true); |
|||
CountDownLatch registrationEntered = new CountDownLatch(1); |
|||
CountDownLatch releaseRegistration = new CountDownLatch(1); |
|||
List<String> events = new CopyOnWriteArrayList<>(); |
|||
willAnswer(invocation -> { |
|||
events.add("register"); |
|||
registrationEntered.countDown(); |
|||
releaseRegistration.await(10, TimeUnit.SECONDS); |
|||
return null; |
|||
}).given(localSubscriptionService).addSubscription(any(), any()); |
|||
|
|||
Thread registrar = new Thread(() -> subCtx.registerSubscription(subscription)); |
|||
registrar.start(); |
|||
assertThat(registrationEntered.await(10, TimeUnit.SECONDS)).isTrue(); |
|||
|
|||
Thread stopper = new Thread(() -> { |
|||
subCtx.stop(); |
|||
events.add("stop"); |
|||
}); |
|||
stopper.start(); |
|||
stopper.join(200); |
|||
assertThat(stopper.isAlive()).as("stop() must wait for the in-flight registration").isTrue(); |
|||
|
|||
releaseRegistration.countDown(); |
|||
registrar.join(10_000); |
|||
stopper.join(10_000); |
|||
|
|||
assertThat(events).containsExactly("register", "stop"); |
|||
assertThat(subCtx.isStopped()).isTrue(); |
|||
|
|||
subCtx.registerSubscription(subscription); |
|||
verify(localSubscriptionService, times(1)).addSubscription(any(), any()); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue