-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDataLoaderCacheMapTest.java
More file actions
71 lines (54 loc) · 2.55 KB
/
DataLoaderCacheMapTest.java
File metadata and controls
71 lines (54 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.dataloader;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import static org.dataloader.DataLoaderFactory.newDataLoader;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
/**
* Tests for cacheMap functionality..
*/
@SuppressWarnings("NullableProblems")
public class DataLoaderCacheMapTest {
private <T> BatchLoader<T, T> keysAsValues() {
return CompletableFuture::completedFuture;
}
@Test
public void should_provide_all_futures_from_cache() {
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());
CompletableFuture<Integer> cf1 = dataLoader.load(1);
CompletableFuture<Integer> cf2 = dataLoader.load(2);
CompletableFuture<Integer> cf3 = dataLoader.load(3);
CacheMap<Object, Integer> cacheMap = dataLoader.getCacheMap();
Collection<CompletableFuture<Integer>> futures = cacheMap.getAll();
assertThat(futures.size(), equalTo(3));
assertThat(cacheMap.get(1), equalTo(cf1));
assertThat(cacheMap.get(2), equalTo(cf2));
assertThat(cacheMap.get(3), equalTo(cf3));
assertThat(cacheMap.containsKey(1), equalTo(true));
assertThat(cacheMap.containsKey(2), equalTo(true));
assertThat(cacheMap.containsKey(3), equalTo(true));
assertThat(cacheMap.containsKey(4), equalTo(false));
cacheMap.delete(1);
assertThat(cacheMap.containsKey(1), equalTo(false));
assertThat(cacheMap.containsKey(2), equalTo(true));
cacheMap.clear();
assertThat(cacheMap.containsKey(1), equalTo(false));
assertThat(cacheMap.containsKey(2), equalTo(false));
assertThat(cacheMap.containsKey(3), equalTo(false));
assertThat(cacheMap.containsKey(4), equalTo(false));
}
@Test
public void should_access_to_future_dependants() {
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());
dataLoader.load(1).handle((v, t) -> t);
dataLoader.load(2).handle((v, t) -> t);
dataLoader.load(1).handle((v, t) -> t);
Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll();
List<CompletableFuture<Integer>> futuresList = new ArrayList<>(futures);
assertThat(futuresList.get(0).getNumberOfDependents(), equalTo(4)); // instrumentation is depending on the CF completing
assertThat(futuresList.get(1).getNumberOfDependents(), equalTo(2));
}
}