@ -66,7 +66,9 @@ import org.thingsboard.server.gen.edge.v1.WidgetsBundleUpdateMsg;
import java.lang.reflect.Field ;
import java.lang.reflect.Field ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Optional ;
import java.util.Optional ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.ThreadLocalRandom ;
import java.util.concurrent.ThreadLocalRandom ;
@ -78,6 +80,7 @@ import java.util.stream.Collectors;
@Slf4j
@Slf4j
public class EdgeImitator {
public class EdgeImitator {
private static final int MAX_DOWNLINK_FAILS = 2 ;
private final String routingKey ;
private final String routingKey ;
private final String routingSecret ;
private final String routingSecret ;
@ -93,6 +96,7 @@ public class EdgeImitator {
private boolean randomFailuresOnTimeseriesDownlink = false ;
private boolean randomFailuresOnTimeseriesDownlink = false ;
@Setter
@Setter
private double failureProbability = 0 . 0 ;
private double failureProbability = 0 . 0 ;
private final Map < Integer , Integer > downlinkFailureCountMap = new HashMap < > ( ) ;
@Getter
@Getter
private EdgeConfiguration configuration ;
private EdgeConfiguration configuration ;
@ -244,8 +248,11 @@ public class EdgeImitator {
if ( downlinkMsg . getEntityDataCount ( ) > 0 ) {
if ( downlinkMsg . getEntityDataCount ( ) > 0 ) {
for ( EntityDataProto entityData : downlinkMsg . getEntityDataList ( ) ) {
for ( EntityDataProto entityData : downlinkMsg . getEntityDataList ( ) ) {
if ( randomFailuresOnTimeseriesDownlink ) {
if ( randomFailuresOnTimeseriesDownlink ) {
if ( getRandomBoolean ( ) ) {
int downlinkMsgId = downlinkMsg . getDownlinkMsgId ( ) ;
if ( getRandomBoolean ( ) & & checkFailureThreshold ( downlinkMsgId ) ) {
result . add ( Futures . immediateFailedFuture ( new RuntimeException ( "Random failure. This is expected error for edge test" ) ) ) ;
result . add ( Futures . immediateFailedFuture ( new RuntimeException ( "Random failure. This is expected error for edge test" ) ) ) ;
downlinkFailureCountMap . put ( downlinkMsgId , downlinkFailureCountMap . getOrDefault ( downlinkMsgId , 0 ) + 1 ) ;
} else {
} else {
result . add ( saveDownlinkMsg ( entityData ) ) ;
result . add ( saveDownlinkMsg ( entityData ) ) ;
}
}
@ -354,6 +361,12 @@ public class EdgeImitator {
return Futures . allAsList ( result ) ;
return Futures . allAsList ( result ) ;
}
}
private boolean checkFailureThreshold ( int downlinkMsgId ) {
return failureProbability = = 100 | |
downlinkFailureCountMap . get ( downlinkMsgId ) = = null | |
downlinkFailureCountMap . get ( downlinkMsgId ) < MAX_DOWNLINK_FAILS ;
}
private boolean getRandomBoolean ( ) {
private boolean getRandomBoolean ( ) {
double randomValue = ThreadLocalRandom . current ( ) . nextDouble ( ) * 100 ;
double randomValue = ThreadLocalRandom . current ( ) . nextDouble ( ) * 100 ;
return randomValue < = this . failureProbability ;
return randomValue < = this . failureProbability ;