use of org.mockito.internal.stubbing.answers.AnswersWithDelay in project cassandra by apache.
the class KeyCacheTest method testKeyCacheLoadCacheLoadTimeExceedingLimit.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testKeyCacheLoadCacheLoadTimeExceedingLimit() throws Exception {
DatabaseDescriptor.setCacheLoadTimeout(2);
int delayMillis = 1000;
int numberOfRows = 100;
String cf = COLUMN_FAMILY9;
createAndInvalidateCache(Collections.singletonList(Pair.create(KEYSPACE1, cf)), numberOfRows);
// Testing cache load. Here using custom built AutoSavingCache instance as simulating delay is not possible with
// 'CacheService.instance.keyCache'. 'AutoSavingCache.loadSaved()' is returning no.of entries loaded so we don't need
// to instantiate ICache.class.
CacheService.KeyCacheSerializer keyCacheSerializer = new CacheService.KeyCacheSerializer();
CacheService.KeyCacheSerializer keyCacheSerializerSpy = Mockito.spy(keyCacheSerializer);
AutoSavingCache autoSavingCache = new AutoSavingCache(mock(ICache.class), CacheService.CacheType.KEY_CACHE, keyCacheSerializerSpy);
doAnswer(new AnswersWithDelay(delayMillis, answer -> keyCacheSerializer.deserialize(answer.getArgument(0), answer.getArgument(1)))).when(keyCacheSerializerSpy).deserialize(any(DataInputPlus.class), any(ColumnFamilyStore.class));
long maxExpectedKeyCache = Math.min(numberOfRows, 1 + TimeUnit.SECONDS.toMillis(DatabaseDescriptor.getCacheLoadTimeout()) / delayMillis);
long keysLoaded = autoSavingCache.loadSaved();
assertThat(keysLoaded, Matchers.lessThanOrEqualTo(maxExpectedKeyCache));
assertNotEquals(0, keysLoaded);
Mockito.verify(keyCacheSerializerSpy, Mockito.times(1)).cleanupAfterDeserialize();
}
Aggregations