-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathChainedDataLoaderInstrumentationTest.java
More file actions
130 lines (94 loc) · 5.15 KB
/
ChainedDataLoaderInstrumentationTest.java
File metadata and controls
130 lines (94 loc) · 5.15 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.dataloader.instrumentation;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderFactory;
import org.dataloader.DataLoaderOptions;
import org.dataloader.fixtures.TestKit;
import org.dataloader.fixtures.parameterized.TestDataLoaderFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import static org.awaitility.Awaitility.await;
import static org.dataloader.DataLoaderOptions.newOptionsBuilder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class ChainedDataLoaderInstrumentationTest {
CapturingInstrumentation capturingA;
CapturingInstrumentation capturingB;
CapturingInstrumentation capturingButReturnsNull;
@BeforeEach
void setUp() {
capturingA = new CapturingInstrumentation("A");
capturingB = new CapturingInstrumentation("B");
capturingButReturnsNull = new CapturingInstrumentationReturnsNull("NULL");
}
@Test
void canChainTogetherZeroInstrumentation() {
// just to prove its useless but harmless
ChainedDataLoaderInstrumentation chainedItn = new ChainedDataLoaderInstrumentation();
DataLoaderOptions options = newOptionsBuilder().setInstrumentation(chainedItn).build();
DataLoader<String, String> dl = DataLoaderFactory.newDataLoader(TestKit.keysAsValues(), options);
dl.load("A");
dl.load("B");
CompletableFuture<List<String>> dispatch = dl.dispatch();
await().until(dispatch::isDone);
assertThat(dispatch.join(), equalTo(List.of("A", "B")));
}
@Test
void canChainTogetherOneInstrumentation() {
CapturingInstrumentation capturingA = new CapturingInstrumentation("A");
ChainedDataLoaderInstrumentation chainedItn = new ChainedDataLoaderInstrumentation()
.add(capturingA);
DataLoaderOptions options = newOptionsBuilder().setInstrumentation(chainedItn).build();
DataLoader<String, String> dl = DataLoaderFactory.newDataLoader(TestKit.keysAsValues(), options);
dl.load("X");
dl.load("Y");
CompletableFuture<List<String>> dispatch = dl.dispatch();
await().until(dispatch::isDone);
assertThat(capturingA.notLoads(), equalTo(List.of("A_beginDispatch",
"A_beginBatchLoader", "A_beginBatchLoader_onDispatched", "A_beginBatchLoader_onCompleted",
"A_beginDispatch_onDispatched", "A_beginDispatch_onCompleted")));
assertThat(capturingA.onlyLoads(), equalTo(List.of(
"A_beginLoad_k:X", "A_beginLoad_onDispatched_k:X", "A_beginLoad_k:Y", "A_beginLoad_onDispatched_k:Y",
"A_beginLoad_onCompleted_k:X", "A_beginLoad_onCompleted_k:Y"
)));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void canChainTogetherManyInstrumentationsWithDifferentBatchLoaders(TestDataLoaderFactory factory) {
ChainedDataLoaderInstrumentation chainedItn = new ChainedDataLoaderInstrumentation()
.add(capturingA)
.add(capturingB)
.add(capturingButReturnsNull);
DataLoaderOptions options = newOptionsBuilder().setInstrumentation(chainedItn).build();
DataLoader<String, String> dl = factory.idLoader(options);
dl.load("X");
dl.load("Y");
CompletableFuture<List<String>> dispatch = dl.dispatch();
await().until(dispatch::isDone);
//
// A_beginBatchLoader happens before A_beginDispatch_onDispatched because these are sync
// and no async - a batch scheduler or async batch loader would change that
//
assertThat(capturingA.notLoads(), equalTo(List.of("A_beginDispatch",
"A_beginBatchLoader", "A_beginBatchLoader_onDispatched", "A_beginBatchLoader_onCompleted",
"A_beginDispatch_onDispatched", "A_beginDispatch_onCompleted")));
assertThat(capturingA.onlyLoads(), equalTo(List.of(
"A_beginLoad_k:X", "A_beginLoad_onDispatched_k:X", "A_beginLoad_k:Y", "A_beginLoad_onDispatched_k:Y",
"A_beginLoad_onCompleted_k:X", "A_beginLoad_onCompleted_k:Y"
)));
assertThat(capturingB.notLoads(), equalTo(List.of("B_beginDispatch",
"B_beginBatchLoader", "B_beginBatchLoader_onDispatched", "B_beginBatchLoader_onCompleted",
"B_beginDispatch_onDispatched", "B_beginDispatch_onCompleted")));
// it returned null on all its contexts - nothing to call back on
assertThat(capturingButReturnsNull.notLoads(), equalTo(List.of("NULL_beginDispatch", "NULL_beginBatchLoader")));
}
@Test
void addition_works() {
ChainedDataLoaderInstrumentation chainedItn = new ChainedDataLoaderInstrumentation()
.add(capturingA).prepend(capturingB).addAll(List.of(capturingButReturnsNull));
assertThat(chainedItn.getInstrumentations(), equalTo(List.of(capturingB, capturingA, capturingButReturnsNull)));
}
}