1+ package org .dataloader .graphql ;
2+
3+ import graphql .ExecutionResult ;
4+ import graphql .execution .instrumentation .InstrumentationContext ;
5+ import org .dataloader .BatchLoader ;
6+ import org .dataloader .DataLoader ;
7+ import org .dataloader .DataLoaderRegistry ;
8+ import org .junit .Test ;
9+
10+ import java .util .ArrayList ;
11+ import java .util .List ;
12+ import java .util .concurrent .CompletableFuture ;
13+ import java .util .concurrent .atomic .AtomicInteger ;
14+
15+ import static java .util .Arrays .asList ;
16+ import static java .util .Collections .singletonList ;
17+ import static org .hamcrest .Matchers .equalTo ;
18+ import static org .junit .Assert .assertThat ;
19+
20+ public class DataLoaderDispatcherInstrumentationTest {
21+
22+ @ Test
23+ public void basic_invocation () throws Exception {
24+
25+ AtomicInteger invocationCount = new AtomicInteger ();
26+ final List <Object > loadedKeys = new ArrayList <>();
27+ final BatchLoader <Object , Object > identityBatchLoader = keys -> {
28+ invocationCount .incrementAndGet ();
29+ loadedKeys .add (keys );
30+ return CompletableFuture .completedFuture (keys );
31+ };
32+
33+ DataLoader <Object , Object > dlA = new DataLoader <>(identityBatchLoader );
34+ DataLoader <Object , Object > dlB = new DataLoader <>(identityBatchLoader );
35+ DataLoader <Object , Object > dlC = new DataLoader <>(identityBatchLoader );
36+ DataLoaderRegistry registry = new DataLoaderRegistry ()
37+ .register (dlA )
38+ .register (dlB )
39+ .register (dlC );
40+
41+ DataLoaderDispatcherInstrumentation dispatcher = new DataLoaderDispatcherInstrumentation (registry );
42+ InstrumentationContext <CompletableFuture <ExecutionResult >> context = dispatcher .beginExecutionStrategy (null );
43+
44+ // cause some activity
45+ dlA .load ("A" );
46+ dlB .load ("B" );
47+ dlC .load ("C" );
48+
49+ context .onEnd (null , null );
50+
51+ assertThat (invocationCount .get (), equalTo (3 ));
52+
53+ // will be [[A],[B],[C]]
54+ assertThat (loadedKeys , equalTo (asList (singletonList ("A" ), singletonList ("B" ), singletonList ("C" ))));
55+ }
56+ }
0 commit comments