|  | 
|  | 1 | +package org.dataloader; | 
|  | 2 | + | 
|  | 3 | +import org.junit.Test; | 
|  | 4 | + | 
|  | 5 | +import java.util.ArrayList; | 
|  | 6 | +import java.util.Collection; | 
|  | 7 | +import java.util.List; | 
|  | 8 | +import java.util.concurrent.CompletableFuture; | 
|  | 9 | + | 
|  | 10 | +import static org.dataloader.DataLoaderFactory.newDataLoader; | 
|  | 11 | +import static org.hamcrest.Matchers.equalTo; | 
|  | 12 | +import static org.junit.Assert.assertThat; | 
|  | 13 | + | 
|  | 14 | +/** | 
|  | 15 | + * Tests for cacheMap functionality.. | 
|  | 16 | + */ | 
|  | 17 | +public class DataLoaderCacheMapTest { | 
|  | 18 | + | 
|  | 19 | + private <T> BatchLoader<T, T> keysAsValues() { | 
|  | 20 | + return CompletableFuture::completedFuture; | 
|  | 21 | + } | 
|  | 22 | + | 
|  | 23 | + @Test | 
|  | 24 | + public void should_provide_all_futures_from_cache() { | 
|  | 25 | + DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues()); | 
|  | 26 | + | 
|  | 27 | + dataLoader.load(1); | 
|  | 28 | + dataLoader.load(2); | 
|  | 29 | + dataLoader.load(1); | 
|  | 30 | + | 
|  | 31 | + Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll(); | 
|  | 32 | + assertThat(futures.size(), equalTo(2)); | 
|  | 33 | + } | 
|  | 34 | + | 
|  | 35 | + @Test | 
|  | 36 | + public void should_access_to_future_dependants() { | 
|  | 37 | + DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues()); | 
|  | 38 | + | 
|  | 39 | + dataLoader.load(1).handle((v, t) -> t); | 
|  | 40 | + dataLoader.load(2).handle((v, t) -> t); | 
|  | 41 | + dataLoader.load(1).handle((v, t) -> t); | 
|  | 42 | + | 
|  | 43 | + Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll(); | 
|  | 44 | + | 
|  | 45 | + List<CompletableFuture<Integer>> futuresList = new ArrayList<>(futures); | 
|  | 46 | + assertThat(futuresList.get(0).getNumberOfDependents(), equalTo(2)); | 
|  | 47 | + assertThat(futuresList.get(1).getNumberOfDependents(), equalTo(1)); | 
|  | 48 | + } | 
|  | 49 | +} | 
0 commit comments