ShardProgress { public long updateLastSweptTimestamp(ShardAndStrategy shardAndStrategy, long timestamp) { return increaseValueFromToAtLeast(shardAndStrategy, getLastSweptTimestamp(shardAndStrategy), timestamp); } ShardProgress(KeyValueService kvs); int getNumberOfShards(); int updateNumberOfShards(int newNumber); long getLastSweptTimestamp(ShardAndStrategy shardAndStrategy); long updateLastSweptTimestamp(ShardAndStrategy shardAndStrategy, long timestamp);  }
@Test public void progressivelyFailingCasEventuallySucceeds() { KeyValueService mockKvs = mock(KeyValueService.class); when(mockKvs.get(any(), anyMap())) .thenReturn(ImmutableMap.of()) .thenReturn(ImmutableMap.of(DUMMY, createValue(5L))) .thenReturn(ImmutableMap.of(DUMMY, createValue(10L))) .thenReturn(ImmutableMap.of(DUMMY, createValue(15L))); doThrow(new CheckAndSetException("sadness")).when(mockKvs).checkAndSet(any()); ShardProgress instrumentedProgress = new ShardProgress(mockKvs); assertThat(instrumentedProgress.updateLastSweptTimestamp(CONSERVATIVE_TEN, 12L)) .isEqualTo(15L); }
@Test public void repeatedlyFailingCasThrows() { KeyValueService mockKvs = mock(KeyValueService.class); when(mockKvs.get(any(), anyMap())) .thenReturn(ImmutableMap.of()) .thenReturn(ImmutableMap.of(DUMMY, createValue(5L))) .thenReturn(ImmutableMap.of(DUMMY, createValue(10L))) .thenReturn(ImmutableMap.of(DUMMY, createValue(10L))); doThrow(new CheckAndSetException("sadness")).when(mockKvs).checkAndSet(any()); ShardProgress instrumentedProgress = new ShardProgress(mockKvs); assertThatThrownBy(() -> instrumentedProgress.updateLastSweptTimestamp(CONSERVATIVE_TEN, 12L)) .isInstanceOf(CheckAndSetException.class); }