Skip to content

Commit

Permalink
#2878: sonardance
Browse files Browse the repository at this point in the history
  • Loading branch information
uweschaefer committed Jun 4, 2024
1 parent 64ffeec commit de2cd3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.redisson.config.Config;

@ExtendWith(MockitoExtension.class)
@SuppressWarnings("unchecked")
class AbstractRedisTxManagedProjectionTest {

@Mock private RedissonClient redisson;
Expand All @@ -57,7 +58,7 @@ void happyPath() {
RBucket<Object> bucket = mock(RBucket.class);
when(redisson.getBucket(any(), any())).thenReturn(bucket);

assertThat(underTest.stateBucket()).isNotNull().isInstanceOf(RBucket.class).isSameAs(bucket);
assertThat(underTest.stateBucket()).isInstanceOf(RBucket.class).isSameAs(bucket);
verify(redisson)
.getBucket(underTest.redisKey() + "_state_tracking", FactStreamPositionCodec.INSTANCE);
}
Expand All @@ -76,8 +77,8 @@ protected ProjectionWithBulkSet(RedissonClient redisson) {

@Test
void getMaxSize() {
AbstractRedisTxProjection underTest = new ProjectionWithBulkSet(mock(RedissonClient.class));
assertThat(underTest.maxBatchSizePerTransaction()).isEqualTo(12);
AbstractRedisTxProjection p = new ProjectionWithBulkSet(mock(RedissonClient.class));
assertThat(p.maxBatchSizePerTransaction()).isEqualTo(12);
}
}

Expand All @@ -87,13 +88,9 @@ class BucketFromTx {
@Test
void happyPath() {
RBucket<Object> bucket = mock(RBucket.class);
RTransaction tx = mock(RTransaction.class);
when(tx.getBucket(any(), any())).thenReturn(bucket);

assertThat(underTest.stateBucket(tx))
.isNotNull()
.isInstanceOf(RBucket.class)
.isSameAs(bucket);
assertThat(underTest.stateBucket(tx)).isInstanceOf(RBucket.class).isSameAs(bucket);
verify(tx)
.getBucket(underTest.redisKey() + "_state_tracking", FactStreamPositionCodec.INSTANCE);
}
Expand All @@ -106,7 +103,6 @@ void nonRunningTransaction() {

UUID id = new UUID(23, 43);
RBucket<Object> bucket = mock(RBucket.class);
RTransaction tx = mock(RTransaction.class);
when(redisson.getBucket(any(), any())).thenReturn(bucket);
when(bucket.get()).thenReturn(FactStreamPosition.of(id, -1L));

Expand All @@ -118,7 +114,6 @@ void nonRunningTransaction() {

@Test
void runningTransaction() {
RTransaction tx = mock(RTransaction.class);
when(redisson.createTransaction(any())).thenReturn(tx);

underTest.begin();
Expand Down Expand Up @@ -152,7 +147,6 @@ void nonRunningTransaction() {
@Test
void runningTransaction() {

RTransaction tx = mock(RTransaction.class);
when(redisson.createTransaction(any())).thenReturn(tx);

underTest.begin();
Expand All @@ -164,17 +158,6 @@ void runningTransaction() {

verify(bucket).set(FACT_STREAM_POSITION);
}

@Test
void noTxnoBatch() {

RBucket<Object> bucket = mock(RBucket.class);
when(redisson.getBucket(any(), any())).thenReturn(bucket);

underTest.factStreamPosition(FACT_STREAM_POSITION);

verify(bucket).set(FACT_STREAM_POSITION);
}
}

@Nested
Expand All @@ -188,11 +171,11 @@ void happyPath() {
when(redisson.getConfig()).thenReturn(config);
when(config.getLockWatchdogTimeout()).thenReturn(1000L);
when(lock.tryLock(anyLong(), any())).thenReturn(true);
AbstractRedisTxManagedProjection underTest = new TestProjection(redisson);
AbstractRedisTxManagedProjection testProjection = new TestProjection(redisson);

AutoCloseable wt = underTest.acquireWriteToken();
AutoCloseable wt = testProjection.acquireWriteToken();

assertThat(wt).isNotNull().isInstanceOf(RedisWriterToken.class);
assertThat(wt).isInstanceOf(RedisWriterToken.class);
verify(lock).tryLock(anyLong(), any(TimeUnit.class));
}

Expand All @@ -204,13 +187,13 @@ void passesWaitTime() {
when(redisson.getConfig()).thenReturn(config);
when(config.getLockWatchdogTimeout()).thenReturn(1000L);
when(lock.tryLock(anyLong(), any())).thenReturn(true);
AbstractRedisTxManagedProjection underTest = new TestProjection(redisson);
AbstractRedisTxManagedProjection testProjection = new TestProjection(redisson);

@NonNull Duration dur = Duration.ofMillis(127);
AutoCloseable wt = underTest.acquireWriteToken(dur);
AutoCloseable wt = testProjection.acquireWriteToken(dur);

verify(lock).tryLock(dur.toMillis(), TimeUnit.MILLISECONDS);
assertThat(wt).isNotNull().isInstanceOf(RedisWriterToken.class);
assertThat(wt).isInstanceOf(RedisWriterToken.class);
}

@SneakyThrows
Expand All @@ -219,10 +202,10 @@ void withWaitTimeExpiring() {

when(redisson.getLock(anyString())).thenReturn(lock);
when(lock.tryLock(anyLong(), any())).thenReturn(false);
AbstractRedisTxManagedProjection underTest = new TestProjection(redisson);
AbstractRedisTxManagedProjection testProjection = new TestProjection(redisson);

@NonNull Duration dur = Duration.ofMillis(127);
AutoCloseable wt = underTest.acquireWriteToken(dur);
AutoCloseable wt = testProjection.acquireWriteToken(dur);

verify(lock).tryLock(dur.toMillis(), TimeUnit.MILLISECONDS);
assertThat(wt).isNull();
Expand All @@ -234,10 +217,10 @@ void withWaitInterruption() {

when(redisson.getLock(anyString())).thenReturn(lock);
when(lock.tryLock(anyLong(), any())).thenThrow(InterruptedException.class);
AbstractRedisTxManagedProjection underTest = new TestProjection(redisson);
AbstractRedisTxManagedProjection testProjection = new TestProjection(redisson);

@NonNull Duration dur = Duration.ofMillis(127);
AutoCloseable wt = underTest.acquireWriteToken(dur);
AutoCloseable wt = testProjection.acquireWriteToken(dur);

verify(lock).tryLock(dur.toMillis(), TimeUnit.MILLISECONDS);
assertThat(wt).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static class Tx {}

@Mock private @NonNull TransactionAdapter<Tx> adapter;
@Mock private Tx runningTransaction;
@InjectMocks private TransactionBehavior underTest;
@InjectMocks private TransactionBehavior<Tx> underTest;

@Nested
class WhenBegining {
Expand Down

0 comments on commit de2cd3f

Please sign in to comment.