|
|
|
@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.extension.ExtendWith; |
|
|
|
import org.mockito.ArgumentMatcher; |
|
|
|
import org.mockito.InOrder; |
|
|
|
import org.mockito.InjectMocks; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
|
|
@ -13,31 +14,44 @@ import org.springframework.test.util.ReflectionTestUtils; |
|
|
|
import org.thingsboard.rule.engine.api.AttributesSaveRequest; |
|
|
|
import org.thingsboard.server.cache.SimpleTbCacheValueWrapper; |
|
|
|
import org.thingsboard.server.cache.TbTransactionalCache; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
import org.thingsboard.server.common.data.edge.Edge; |
|
|
|
import org.thingsboard.server.common.data.id.EdgeId; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.notification.rule.trigger.EdgeConnectionTrigger; |
|
|
|
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTrigger; |
|
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
|
import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor; |
|
|
|
import org.thingsboard.server.common.msg.queue.ServiceType; |
|
|
|
import org.thingsboard.server.queue.discovery.PartitionService; |
|
|
|
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
|
|
|
import org.thingsboard.server.service.edge.EdgeContextComponent; |
|
|
|
import org.thingsboard.server.service.edge.rpc.EdgeEventStorageSettings; |
|
|
|
import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
import java.util.concurrent.ScheduledFuture; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ConcurrentMap; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.anyLong; |
|
|
|
import static org.mockito.ArgumentMatchers.isNull; |
|
|
|
import static org.mockito.ArgumentMatchers.argThat; |
|
|
|
import static org.mockito.Mockito.inOrder; |
|
|
|
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; |
|
|
|
import static org.thingsboard.server.service.state.DefaultDeviceStateService.ACTIVITY_STATE; |
|
|
|
import static org.thingsboard.server.service.state.DefaultDeviceStateService.LAST_DISCONNECT_TIME; |
|
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class) |
|
|
|
public class EdgeGrpcServiceTest { |
|
|
|
@ -60,9 +74,18 @@ public class EdgeGrpcServiceTest { |
|
|
|
@Mock |
|
|
|
private TelemetrySubscriptionService tsSubService; |
|
|
|
|
|
|
|
@Mock |
|
|
|
private TbClusterService clusterService; |
|
|
|
|
|
|
|
@Mock |
|
|
|
private PartitionService partitionService; |
|
|
|
|
|
|
|
@Mock |
|
|
|
private EdgeEventStorageSettings edgeEventStorageSettings; |
|
|
|
|
|
|
|
@Mock |
|
|
|
private ScheduledExecutorService edgeEventProcessingExecutorService; |
|
|
|
|
|
|
|
@InjectMocks |
|
|
|
private EdgeGrpcService edgeGrpcService; |
|
|
|
|
|
|
|
@ -180,6 +203,7 @@ public class EdgeGrpcServiceTest { |
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(tsSubService, never()).saveAttributes(argThat(inactiveStateWrite())); |
|
|
|
verify(tsSubService, never()).saveAttributes(argThat(lastDisconnectTimeWrite())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -214,6 +238,90 @@ public class EdgeGrpcServiceTest { |
|
|
|
verify(tsSubService, never()).saveAttributes(argThat(inactiveStateWrite())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenSessionReplacedOnThisNodeDuringDisconnect_whenStaleCallbackRuns_thenNoDisconnectSideEffects() { |
|
|
|
// the edge reconnects to this node after the stale session was detached, so the cache names this node
|
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
sessions().put(edgeId, sessionFor(sessionId)); |
|
|
|
reconnectToThisNodeOnOwnershipCheck(); |
|
|
|
|
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(tsSubService, never()).saveAttributes(argThat(inactiveStateWrite())); |
|
|
|
verify(tsSubService, never()).saveAttributes(argThat(lastDisconnectTimeWrite())); |
|
|
|
verify(clusterService, never()).pushMsgToRuleEngine(any(TenantId.class), any(EntityId.class), any(TbMsg.class), isNull()); |
|
|
|
verify(edgeIdServiceIdCache, never()).evict(edgeId); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenReconnectDuringInactiveWrite_whenDisconnect_thenAttachWaitsForTheWrite() throws Exception { |
|
|
|
// a same-node reconnect racing the guard must be ordered after the inactive write, so its active=true wins
|
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
sessions().put(edgeId, sessionFor(sessionId)); |
|
|
|
when(edgeIdServiceIdCache.get(edgeId)).thenReturn(null); |
|
|
|
EdgeGrpcSession replacement = mock(EdgeGrpcSession.class); |
|
|
|
Thread attach = new Thread(() -> ReflectionTestUtils.invokeMethod(edgeGrpcService, "attachSession", edgeId, replacement)); |
|
|
|
AtomicBoolean attachBlockedDuringWrite = new AtomicBoolean(); |
|
|
|
lenient().doAnswer(invocation -> { |
|
|
|
attach.start(); |
|
|
|
attach.join(200); |
|
|
|
attachBlockedDuringWrite.set(attach.isAlive()); |
|
|
|
return null; |
|
|
|
}).when(tsSubService).saveAttributes(argThat(inactiveStateWrite())); |
|
|
|
|
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
attach.join(5000); |
|
|
|
|
|
|
|
assertThat(attachBlockedDuringWrite).isTrue(); |
|
|
|
assertThat(sessions()).containsEntry(edgeId, replacement); |
|
|
|
verify(tsSubService, times(1)).saveAttributes(argThat(lastDisconnectTimeWrite())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenEdgeReconnectedToAnotherNode_whenDisconnect_thenDisconnectEventNotPushed() { |
|
|
|
// DISCONNECT_EVENT carries active=false, a Save Attributes node would write it back
|
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
sessions().put(edgeId, sessionFor(sessionId)); |
|
|
|
when(serviceInfoProvider.getServiceId()).thenReturn(THIS_NODE); |
|
|
|
when(edgeIdServiceIdCache.get(edgeId)).thenReturn(SimpleTbCacheValueWrapper.wrap(OTHER_NODE)); |
|
|
|
when(partitionService.getAllServiceIds(ServiceType.TB_CORE)).thenReturn(Set.of(THIS_NODE, OTHER_NODE)); |
|
|
|
|
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(clusterService, never()).pushMsgToRuleEngine(any(TenantId.class), any(EntityId.class), any(TbMsg.class), isNull()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenEdgeGenuinelyDisconnected_whenDisconnect_thenDisconnectEventPushed() { |
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
sessions().put(edgeId, sessionFor(sessionId)); |
|
|
|
when(serviceInfoProvider.getServiceId()).thenReturn(THIS_NODE); |
|
|
|
when(edgeIdServiceIdCache.get(edgeId)).thenReturn(SimpleTbCacheValueWrapper.wrap(THIS_NODE)); |
|
|
|
|
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(clusterService, times(1)).pushMsgToRuleEngine(any(TenantId.class), any(EntityId.class), any(TbMsg.class), isNull()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenConnect_whenClaimingOwnership_thenCachePutHappensBeforeActiveStateWrite() { |
|
|
|
// the previous node reads this cache entry on disconnect, so the claim must be visible before active=true
|
|
|
|
EdgeGrpcSession session = mock(EdgeGrpcSession.class); |
|
|
|
when(session.getSessionId()).thenReturn(UUID.randomUUID()); |
|
|
|
when(session.getEdge()).thenReturn(edge); |
|
|
|
when(serviceInfoProvider.getServiceId()).thenReturn(THIS_NODE); |
|
|
|
when(ctx.getEdgeEventStorageSettings()).thenReturn(edgeEventStorageSettings); |
|
|
|
ReflectionTestUtils.setField(edgeGrpcService, "edgeEventProcessingExecutorService", edgeEventProcessingExecutorService); |
|
|
|
when(edgeEventProcessingExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))) |
|
|
|
.thenAnswer(invocation -> mock(ScheduledFuture.class)); |
|
|
|
|
|
|
|
ReflectionTestUtils.invokeMethod(edgeGrpcService, "onEdgeConnect", edgeId, session); |
|
|
|
|
|
|
|
InOrder inOrder = inOrder(edgeIdServiceIdCache, tsSubService); |
|
|
|
inOrder.verify(edgeIdServiceIdCache).put(edgeId, THIS_NODE); |
|
|
|
inOrder.verify(tsSubService).saveAttributes(argThat(activeStateWrite())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenEdgeOwnedByThisNode_whenDisconnect_thenInactiveStateIsPersisted() { |
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
@ -224,6 +332,7 @@ public class EdgeGrpcServiceTest { |
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(tsSubService, times(1)).saveAttributes(argThat(inactiveStateWrite())); |
|
|
|
verify(tsSubService, times(1)).saveAttributes(argThat(lastDisconnectTimeWrite())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -237,12 +346,55 @@ public class EdgeGrpcServiceTest { |
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(tsSubService, times(1)).saveAttributes(argThat(inactiveStateWrite())); |
|
|
|
verify(tsSubService, times(1)).saveAttributes(argThat(lastDisconnectTimeWrite())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenZeroDelayAndSessionReplacedOnThisNode_whenStaleCallbackRuns_thenNoDisconnectNotification() { |
|
|
|
// with zero delay the notification skips the liveness re-check, so only the disconnect guard prevents it
|
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
sessions().put(edgeId, sessionFor(sessionId)); |
|
|
|
reconnectToThisNodeOnOwnershipCheck(); |
|
|
|
lenient().when(ctx.getRuleProcessor()).thenReturn(ruleProcessor); |
|
|
|
|
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(ruleProcessor, never()).process(argThat(disconnectTrigger())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void givenZeroDelayAndEdgeGenuinelyDisconnected_whenDisconnect_thenNotificationSent() { |
|
|
|
UUID sessionId = UUID.randomUUID(); |
|
|
|
sessions().put(edgeId, sessionFor(sessionId)); |
|
|
|
when(edgeIdServiceIdCache.get(edgeId)).thenReturn(null); |
|
|
|
when(ctx.getRuleProcessor()).thenReturn(ruleProcessor); |
|
|
|
|
|
|
|
onEdgeDisconnect(sessionId); |
|
|
|
|
|
|
|
verify(ruleProcessor, times(1)).process(argThat(disconnectTrigger())); |
|
|
|
} |
|
|
|
|
|
|
|
private void onEdgeDisconnect(UUID sessionId) { |
|
|
|
ReflectionTestUtils.invokeMethod(edgeGrpcService, "onEdgeDisconnect", edge, sessionId); |
|
|
|
} |
|
|
|
|
|
|
|
// simulates a reconnect to this node landing after the stale session was detached, before the guard runs
|
|
|
|
private void reconnectToThisNodeOnOwnershipCheck() { |
|
|
|
when(serviceInfoProvider.getServiceId()).thenReturn(THIS_NODE); |
|
|
|
when(edgeIdServiceIdCache.get(edgeId)).thenAnswer(invocation -> { |
|
|
|
sessions().put(edgeId, mock(EdgeGrpcSession.class)); |
|
|
|
return SimpleTbCacheValueWrapper.wrap(THIS_NODE); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private ArgumentMatcher<AttributesSaveRequest> activeStateWrite() { |
|
|
|
return request -> request != null |
|
|
|
&& edgeId.equals(request.getEntityId()) |
|
|
|
&& request.getEntries().stream().anyMatch(entry -> |
|
|
|
ACTIVITY_STATE.equals(entry.getKey()) |
|
|
|
&& Boolean.TRUE.equals(entry.getBooleanValue().orElse(null))); |
|
|
|
} |
|
|
|
|
|
|
|
private EdgeGrpcSession sessionFor(UUID sessionId) { |
|
|
|
EdgeGrpcSession session = mock(EdgeGrpcSession.class); |
|
|
|
when(session.getSessionId()).thenReturn(sessionId); |
|
|
|
@ -285,4 +437,10 @@ public class EdgeGrpcServiceTest { |
|
|
|
return trigger -> trigger instanceof EdgeConnectionTrigger edgeTrigger && !edgeTrigger.isConnected(); |
|
|
|
} |
|
|
|
|
|
|
|
private ArgumentMatcher<AttributesSaveRequest> lastDisconnectTimeWrite() { |
|
|
|
return request -> request != null |
|
|
|
&& edgeId.equals(request.getEntityId()) |
|
|
|
&& request.getEntries().stream().anyMatch(entry -> LAST_DISCONNECT_TIME.equals(entry.getKey())); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|