@ -28,27 +28,27 @@ import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatThrownBy ;
import static org.assertj.core.api.Assertions.assertThatThrownBy ;
class DeduplicatePersistence StrategyTest {
class DeduplicateProcessing StrategyTest {
final int deduplicationIntervalSecs = 10 ;
final int deduplicationIntervalSecs = 10 ;
DeduplicatePersistence Strategy strategy ;
DeduplicateProcessing Strategy strategy ;
@BeforeEach
@BeforeEach
void setup ( ) {
void setup ( ) {
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
}
}
@Test
@Test
void shouldThrowWhenDeduplicationIntervalIsLessThanOneSecond ( ) {
void shouldThrowWhenDeduplicationIntervalIsLessThanOneSecond ( ) {
assertThatThrownBy ( ( ) - > new DeduplicatePersistence Strategy ( 0 ) )
assertThatThrownBy ( ( ) - > new DeduplicateProcessing Strategy ( 0 ) )
. isInstanceOf ( IllegalArgumentException . class )
. isInstanceOf ( IllegalArgumentException . class )
. hasMessageContaining ( "Deduplication interval must be at least 1 second(s) and at most 86400 second(s), was 0 second(s)" ) ;
. hasMessageContaining ( "Deduplication interval must be at least 1 second(s) and at most 86400 second(s), was 0 second(s)" ) ;
}
}
@Test
@Test
void shouldThrowWhenDeduplicationIntervalIsMoreThan24Hours ( ) {
void shouldThrowWhenDeduplicationIntervalIsMoreThan24Hours ( ) {
assertThatThrownBy ( ( ) - > new DeduplicatePersistence Strategy ( 86401 ) )
assertThatThrownBy ( ( ) - > new DeduplicateProcessing Strategy ( 86401 ) )
. isInstanceOf ( IllegalArgumentException . class )
. isInstanceOf ( IllegalArgumentException . class )
. hasMessageContaining ( "Deduplication interval must be at least 1 second(s) and at most 86400 second(s), was 86401 second(s)" ) ;
. hasMessageContaining ( "Deduplication interval must be at least 1 second(s) and at most 86400 second(s), was 86401 second(s)" ) ;
}
}
@ -59,7 +59,7 @@ class DeduplicatePersistenceStrategyTest {
int deduplicationIntervalSecs = 1 ; // min deduplication interval duration
int deduplicationIntervalSecs = 1 ; // min deduplication interval duration
// WHEN
// WHEN
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
// THEN
// THEN
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
@ -76,7 +76,7 @@ class DeduplicatePersistenceStrategyTest {
int deduplicationIntervalSecs = ( int ) Duration . ofHours ( 1L ) . toSeconds ( ) ; // max deduplication interval duration
int deduplicationIntervalSecs = ( int ) Duration . ofHours ( 1L ) . toSeconds ( ) ; // max deduplication interval duration
// WHEN
// WHEN
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
// THEN
// THEN
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
@ -93,7 +93,7 @@ class DeduplicatePersistenceStrategyTest {
int deduplicationIntervalSecs = ( int ) Duration . ofDays ( 1L ) . toSeconds ( ) ; // max deduplication interval duration
int deduplicationIntervalSecs = ( int ) Duration . ofDays ( 1L ) . toSeconds ( ) ; // max deduplication interval duration
// WHEN
// WHEN
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
// THEN
// THEN
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
@ -110,7 +110,7 @@ class DeduplicatePersistenceStrategyTest {
int deduplicationIntervalSecs = 1 ; // min deduplication interval duration
int deduplicationIntervalSecs = 1 ; // min deduplication interval duration
// WHEN
// WHEN
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
// THEN
// THEN
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
@ -127,7 +127,7 @@ class DeduplicatePersistenceStrategyTest {
int deduplicationIntervalSecs = ( int ) Duration . ofHours ( 1L ) . toSeconds ( ) ;
int deduplicationIntervalSecs = ( int ) Duration . ofHours ( 1L ) . toSeconds ( ) ;
// WHEN
// WHEN
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
// THEN
// THEN
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
@ -144,7 +144,7 @@ class DeduplicatePersistenceStrategyTest {
int deduplicationIntervalSecs = ( int ) Duration . ofDays ( 1L ) . toSeconds ( ) ; // max deduplication interval duration
int deduplicationIntervalSecs = ( int ) Duration . ofDays ( 1L ) . toSeconds ( ) ; // max deduplication interval duration
// WHEN
// WHEN
strategy = new DeduplicatePersistence Strategy ( deduplicationIntervalSecs ) ;
strategy = new DeduplicateProcessing Strategy ( deduplicationIntervalSecs ) ;
// THEN
// THEN
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
var deduplicationCache = ( LoadingCache < Long , Set < UUID > > ) ReflectionTestUtils . getField ( strategy , "deduplicationCache" ) ;
@ -160,7 +160,7 @@ class DeduplicatePersistenceStrategyTest {
long ts = 1_000_000L ;
long ts = 1_000_000L ;
UUID originator = UUID . randomUUID ( ) ;
UUID originator = UUID . randomUUID ( ) ;
assertThat ( strategy . shouldPe rsi st ( ts , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( ts , originator ) ) . isTrue ( ) ;
}
}
@Test
@Test
@ -169,11 +169,11 @@ class DeduplicatePersistenceStrategyTest {
UUID originator = UUID . randomUUID ( ) ;
UUID originator = UUID . randomUUID ( ) ;
// Initial call should return true
// Initial call should return true
assertThat ( strategy . shouldPe rsi st ( baseTs , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs , originator ) ) . isTrue ( ) ;
// Subsequent call within the same interval should return false for the same originator
// Subsequent call within the same interval should return false for the same originator
long withinSameIntervalTs = baseTs + 1000L ;
long withinSameIntervalTs = baseTs + 1000L ;
assertThat ( strategy . shouldPe rsi st ( withinSameIntervalTs , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( withinSameIntervalTs , originator ) ) . isFalse ( ) ;
}
}
@Test
@Test
@ -183,12 +183,12 @@ class DeduplicatePersistenceStrategyTest {
UUID originator2 = UUID . randomUUID ( ) ;
UUID originator2 = UUID . randomUUID ( ) ;
// First call for different originators in the same interval should return true independently
// First call for different originators in the same interval should return true independently
assertThat ( strategy . shouldPe rsi st ( baseTs , originator1 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs , originator1 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( baseTs , originator2 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs , originator2 ) ) . isTrue ( ) ;
// Subsequent calls for the same originators within the same interval should return false
// Subsequent calls for the same originators within the same interval should return false
assertThat ( strategy . shouldPe rsi st ( baseTs + 500L , originator1 ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs + 500L , originator1 ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( baseTs + 500L , originator2 ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs + 500L , originator2 ) ) . isFalse ( ) ;
}
}
@Test
@Test
@ -197,11 +197,11 @@ class DeduplicatePersistenceStrategyTest {
long maxTs = Long . MAX_VALUE ;
long maxTs = Long . MAX_VALUE ;
UUID originator = UUID . randomUUID ( ) ;
UUID originator = UUID . randomUUID ( ) ;
assertThat ( strategy . shouldPe rsi st ( minTs , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( minTs , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( minTs + 1L , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( minTs + 1L , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( maxTs , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( maxTs , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( maxTs - 1L , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( maxTs - 1L , originator ) ) . isFalse ( ) ;
}
}
@Test
@Test
@ -213,22 +213,22 @@ class DeduplicatePersistenceStrategyTest {
long firstIntervalEnd = firstIntervalStart + Duration . ofSeconds ( deduplicationIntervalSecs ) . toMillis ( ) - 1L ;
long firstIntervalEnd = firstIntervalStart + Duration . ofSeconds ( deduplicationIntervalSecs ) . toMillis ( ) - 1L ;
long firstIntervalMiddle = calculateMiddle ( firstIntervalStart , firstIntervalEnd ) ;
long firstIntervalMiddle = calculateMiddle ( firstIntervalStart , firstIntervalEnd ) ;
assertThat ( strategy . shouldPe rsi st ( firstIntervalStart , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( firstIntervalStart , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( firstIntervalStart + 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( firstIntervalStart + 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( firstIntervalMiddle , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( firstIntervalMiddle , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( firstIntervalEnd - 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( firstIntervalEnd - 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( firstIntervalEnd , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( firstIntervalEnd , originator ) ) . isFalse ( ) ;
// check 2nd interval
// check 2nd interval
long secondIntervalStart = firstIntervalEnd + 1L ;
long secondIntervalStart = firstIntervalEnd + 1L ;
long secondIntervalEnd = secondIntervalStart + Duration . ofSeconds ( deduplicationIntervalSecs ) . toMillis ( ) - 1L ;
long secondIntervalEnd = secondIntervalStart + Duration . ofSeconds ( deduplicationIntervalSecs ) . toMillis ( ) - 1L ;
long secondIntervalMiddle = calculateMiddle ( secondIntervalStart , secondIntervalEnd ) ;
long secondIntervalMiddle = calculateMiddle ( secondIntervalStart , secondIntervalEnd ) ;
assertThat ( strategy . shouldPe rsi st ( secondIntervalStart , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( secondIntervalStart , originator ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( secondIntervalStart + 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( secondIntervalStart + 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( secondIntervalMiddle , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( secondIntervalMiddle , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( secondIntervalEnd - 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( secondIntervalEnd - 1 , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( secondIntervalEnd , originator ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( secondIntervalEnd , originator ) ) . isFalse ( ) ;
}
}
@Test
@Test
@ -238,19 +238,19 @@ class DeduplicatePersistenceStrategyTest {
long baseTs = 0L ;
long baseTs = 0L ;
// First interval for both originators
// First interval for both originators
assertThat ( strategy . shouldPe rsi st ( baseTs , originator1 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs , originator1 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( baseTs , originator2 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( baseTs , originator2 ) ) . isTrue ( ) ;
// Move to the next interval
// Move to the next interval
long nextIntervalTs = baseTs + Duration . ofSeconds ( 10 ) . toMillis ( ) ;
long nextIntervalTs = baseTs + Duration . ofSeconds ( 10 ) . toMillis ( ) ;
// Each originator should be allowed again in the new interval
// Each originator should be allowed again in the new interval
assertThat ( strategy . shouldPe rsi st ( nextIntervalTs , originator1 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( nextIntervalTs , originator1 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldPe rsi st ( nextIntervalTs , originator2 ) ) . isTrue ( ) ;
assertThat ( strategy . shouldProce ss ( nextIntervalTs , originator2 ) ) . isTrue ( ) ;
// Subsequent calls in the same new interval should return false
// Subsequent calls in the same new interval should return false
assertThat ( strategy . shouldPe rsi st ( nextIntervalTs + 500L , originator1 ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( nextIntervalTs + 500L , originator1 ) ) . isFalse ( ) ;
assertThat ( strategy . shouldPe rsi st ( nextIntervalTs + 500L , originator2 ) ) . isFalse ( ) ;
assertThat ( strategy . shouldProce ss ( nextIntervalTs + 500L , originator2 ) ) . isFalse ( ) ;
}
}
private static long calculateMiddle ( long start , long end ) {
private static long calculateMiddle ( long start , long end ) {