Skip to content

Commit 5983e10

Browse files
Add test affirming context is passed to collectors
1 parent 8ca99cc commit 5983e10

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

src/test/java/org/dataloader/DataLoaderStatsTest.java

+117
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
import org.dataloader.stats.Statistics;
66
import org.dataloader.stats.StatisticsCollector;
77
import org.dataloader.stats.context.IncrementBatchLoadCountByStatisticsContext;
8+
import org.dataloader.stats.context.IncrementBatchLoadExceptionCountStatisticsContext;
9+
import org.dataloader.stats.context.IncrementCacheHitCountStatisticsContext;
810
import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
11+
import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext;
912
import org.junit.Test;
1013

1114
import java.util.ArrayList;
1215
import java.util.List;
1316
import java.util.concurrent.CompletableFuture;
1417

1518
import static java.util.Arrays.asList;
19+
import static java.util.Collections.singletonList;
1620
import static java.util.concurrent.CompletableFuture.completedFuture;
1721
import static org.dataloader.DataLoaderFactory.newDataLoader;
1822
import static org.hamcrest.Matchers.equalTo;
23+
import static org.hamcrest.Matchers.hasSize;
1924
import static org.junit.Assert.assertThat;
2025

2126
/**
@@ -201,4 +206,116 @@ public void stats_are_collected_on_exceptions() {
201206
assertThat(stats.getBatchLoadExceptionCount(), equalTo(2L));
202207
assertThat(stats.getLoadErrorCount(), equalTo(3L));
203208
}
209+
210+
/**
211+
* A simple {@link StatisticsCollector} that stores the contexts passed to it.
212+
*/
213+
private static class ContextPassingStatisticsCollector implements StatisticsCollector {
214+
215+
public List<IncrementLoadCountStatisticsContext<?>> incrementLoadCountStatisticsContexts = new ArrayList<>();
216+
public List<IncrementLoadErrorCountStatisticsContext<?>> incrementLoadErrorCountStatisticsContexts = new ArrayList<>();
217+
public List<IncrementBatchLoadCountByStatisticsContext<?>> incrementBatchLoadCountByStatisticsContexts = new ArrayList<>();
218+
public List<IncrementBatchLoadExceptionCountStatisticsContext<?>> incrementBatchLoadExceptionCountStatisticsContexts = new ArrayList<>();
219+
public List<IncrementCacheHitCountStatisticsContext<?>> incrementCacheHitCountStatisticsContexts = new ArrayList<>();
220+
221+
@Override
222+
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
223+
incrementLoadCountStatisticsContexts.add(context);
224+
return 0;
225+
}
226+
227+
@Deprecated
228+
@Override
229+
public long incrementLoadCount() {
230+
return 0;
231+
}
232+
233+
@Override
234+
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
235+
incrementLoadErrorCountStatisticsContexts.add(context);
236+
return 0;
237+
}
238+
239+
@Deprecated
240+
@Override
241+
public long incrementLoadErrorCount() {
242+
return 0;
243+
}
244+
245+
@Override
246+
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
247+
incrementBatchLoadCountByStatisticsContexts.add(context);
248+
return 0;
249+
}
250+
251+
@Deprecated
252+
@Override
253+
public long incrementBatchLoadCountBy(long delta) {
254+
return 0;
255+
}
256+
257+
@Override
258+
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
259+
incrementBatchLoadExceptionCountStatisticsContexts.add(context);
260+
return 0;
261+
}
262+
263+
@Deprecated
264+
@Override
265+
public long incrementBatchLoadExceptionCount() {
266+
return 0;
267+
}
268+
269+
@Override
270+
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
271+
incrementCacheHitCountStatisticsContexts.add(context);
272+
return 0;
273+
}
274+
275+
@Deprecated
276+
@Override
277+
public long incrementCacheHitCount() {
278+
return 0;
279+
}
280+
281+
@Override
282+
public Statistics getStatistics() {
283+
return null;
284+
}
285+
}
286+
287+
@Test
288+
public void context_is_passed_through_to_collector() {
289+
ContextPassingStatisticsCollector statisticsCollector = new ContextPassingStatisticsCollector();
290+
DataLoader<String, Try<String>> loader = newDataLoader(batchLoaderThatBlows,
291+
DataLoaderOptions.newOptions().setStatisticsCollector(() -> statisticsCollector)
292+
);
293+
294+
loader.load("key", "keyContext");
295+
assertThat(statisticsCollector.incrementLoadCountStatisticsContexts, hasSize(1));
296+
assertThat(statisticsCollector.incrementLoadCountStatisticsContexts.get(0).getKey(), equalTo("key"));
297+
assertThat(statisticsCollector.incrementLoadCountStatisticsContexts.get(0).getCallContext(), equalTo("keyContext"));
298+
299+
loader.load("key", "keyContext");
300+
assertThat(statisticsCollector.incrementCacheHitCountStatisticsContexts, hasSize(1));
301+
assertThat(statisticsCollector.incrementCacheHitCountStatisticsContexts.get(0).getKey(), equalTo("key"));
302+
assertThat(statisticsCollector.incrementCacheHitCountStatisticsContexts.get(0).getCallContext(), equalTo("keyContext"));
303+
304+
loader.dispatch();
305+
assertThat(statisticsCollector.incrementBatchLoadCountByStatisticsContexts, hasSize(1));
306+
assertThat(statisticsCollector.incrementBatchLoadCountByStatisticsContexts.get(0).getKeys(), equalTo(singletonList("key")));
307+
assertThat(statisticsCollector.incrementBatchLoadCountByStatisticsContexts.get(0).getCallContexts(), equalTo(singletonList("keyContext")));
308+
309+
loader.load("exception", "exceptionKeyContext");
310+
loader.dispatch();
311+
assertThat(statisticsCollector.incrementBatchLoadExceptionCountStatisticsContexts, hasSize(1));
312+
assertThat(statisticsCollector.incrementBatchLoadExceptionCountStatisticsContexts.get(0).getKeys(), equalTo(singletonList("exception")));
313+
assertThat(statisticsCollector.incrementBatchLoadExceptionCountStatisticsContexts.get(0).getCallContexts(), equalTo(singletonList("exceptionKeyContext")));
314+
315+
loader.load("error", "errorKeyContext");
316+
loader.dispatch();
317+
assertThat(statisticsCollector.incrementLoadErrorCountStatisticsContexts, hasSize(1));
318+
assertThat(statisticsCollector.incrementLoadErrorCountStatisticsContexts.get(0).getKey(), equalTo("error"));
319+
assertThat(statisticsCollector.incrementLoadErrorCountStatisticsContexts.get(0).getCallContext(), equalTo("errorKeyContext"));
320+
}
204321
}

0 commit comments

Comments
 (0)