SplitKeyDelegatingTransactionService implements TransactionService { @CheckForNull @Override public Long get(long startTimestamp) { return AtlasFutures.getUnchecked(getInternal(keyedSyncServices, startTimestamp)); } SplitKeyDelegatingTransactionService(
            Function<Long, T> timestampToServiceKey,
            Map<T, TransactionService> keyedServices); @CheckForNull @Override Long get(long startTimestamp); @Override Map<Long, Long> get(Iterable<Long> startTimestamps); @Override ListenableFuture<Long> getAsync(long startTimestamp); @Override ListenableFuture<Map<Long, Long>> getAsync(Iterable<Long> startTimestamps); @Override void putUnlessExists(long startTimestamp, long commitTimestamp); @Override void close();  }
@Test public void canCallGetOnDelegate() { when(delegate1.get(1L)).thenReturn(2L); assertThat(delegatingTransactionService.get(1L)).isEqualTo(2L); verify(delegate1).get(1L); }
@Test public void delegatesGetBasedOnFunction() { when(delegate1.get(1L)).thenReturn(2L); when(delegate2.get(2L)).thenReturn(4L); assertThat(delegatingTransactionService.get(1L)).isEqualTo(2L); assertThat(delegatingTransactionService.get(2L)).isEqualTo(4L); verify(delegate1).get(1L); verify(delegate2).get(2L); }
@Test public void getThrowsIfFunctionReturnsUnmappedValue() { assertThatThrownBy(() -> delegatingTransactionService.get(7L)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Could not find a transaction service for timestamp {}"); }
@Test public void rethrowsExceptionsFromMappingFunction() { RuntimeException ex = new IllegalStateException("bad"); TransactionService unusableService = new SplitKeyDelegatingTransactionService<>( num -> { throw ex; }, transactionServiceMap); assertThatThrownBy(() -> unusableService.get(5L)).isEqualTo(ex); }
@Test public void getMultipleDelegatesRequestsToGetMultipleOnDelegates() { when(delegate1.get(any())).thenReturn(ImmutableMap.of(1L, 2L)); assertThat(delegatingTransactionService.get(ImmutableList.of(1L))) .isEqualTo(ImmutableMap.of(1L, 2L)); verifyDelegateHadMultigetCalledWith(delegate1, 1L); }
@Test public void getMultiplePartitionsRequestsAndMergesMaps() { when(delegate1.get(any())).thenReturn(ImmutableMap.of(1L, 8L, 41L, 48L)); when(delegate2.get(any())).thenReturn(ImmutableMap.of(12L, 28L, 32L, 38L)); assertThat(delegatingTransactionService.get(ImmutableList.of(1L, 12L, 32L, 41L))) .isEqualTo(ImmutableMap.of(1L, 8L, 12L, 28L, 32L, 38L, 41L, 48L)); verifyDelegateHadMultigetCalledWith(delegate1, 1L, 41L); verifyDelegateHadMultigetCalledWith(delegate2, 12L, 32L); }
@Test public void getMultipleThrowsAndDoesNotMakeRequestsIfAnyTimestampsCannotBeMapped() { when(delegate1.get(any())).thenReturn(ImmutableMap.of(1L, 8L, 41L, 48L)); assertThatThrownBy(() -> delegatingTransactionService.get(ImmutableList.of(1L, 7L, 41L))) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("A batch of timestamps {} produced some transaction service keys which are" + " unknown"); Mockito.verifyNoMoreInteractions(delegate1); }
@Test public void ignoreUnknownIgnoresUnknownTimestampServicesForSingleTimestamps() { assertThat(lastDigitFiveImpliesUnknownTransactionService.get(5L)).isNull(); }
@Test public void ignoreUnknownIgnoresUnknownTimestampServicesForMultipleTimestamps() { when(delegate1.get(any())).thenReturn(ImmutableMap.of(1L, 8L, 41L, 48L)); assertThat(lastDigitFiveImpliesUnknownTransactionService.get(ImmutableList.of(1L, 5L, 35L, 41L))) .isEqualTo(ImmutableMap.of(1L, 8L, 41L, 48L)); verifyDelegateHadMultigetCalledWith(delegate1, 1L, 41L); }
@Test public void ignoreUnknownFailsIfSeeingATimestampServiceItDoesNotRecognizeForSingleTimestamps() { assertThatThrownBy(() -> lastDigitFiveImpliesUnknownTransactionService.get(7L)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Could not find a transaction service for timestamp {}"); }
@Test public void ignoreUnknownFailsIfSeeingATimestampServiceItDoesNotRecognizeForMultipleTimestamps() { when(delegate1.get(any())).thenReturn(ImmutableMap.of(1L, 8L, 41L, 48L)); assertThatThrownBy(() -> lastDigitFiveImpliesUnknownTransactionService.get(ImmutableList.of(1L, 5L, 7L, 41L))) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("A batch of timestamps {} produced some transaction service keys which are" + " unknown"); Mockito.verifyNoMoreInteractions(delegate1); }