66import org .dataloader .DataLoaderFactory ;
77import org .dataloader .DataLoaderOptions ;
88import org .dataloader .DispatchResult ;
9+ import org .dataloader .fixtures .Stopwatch ;
910import org .dataloader .fixtures .TestKit ;
1011import org .junit .jupiter .api .Test ;
1112
13+ import java .util .ArrayList ;
1214import java .util .List ;
1315import java .util .Set ;
1416import java .util .concurrent .CompletableFuture ;
15- import java .util .concurrent .atomic .AtomicLong ;
1617import java .util .concurrent .atomic .AtomicReference ;
1718
1819import static org .awaitility .Awaitility .await ;
@@ -30,20 +31,19 @@ class DataLoaderInstrumentationTest {
3031
3132 @ Test
3233 void canMonitorDispatching () {
33- AtomicLong timer = new AtomicLong ();
34+ Stopwatch stopwatch = Stopwatch . stopwatchUnStarted ();
3435 AtomicReference <DataLoader <?, ?>> dlRef = new AtomicReference <>();
3536
3637 DataLoaderInstrumentation instrumentation = new DataLoaderInstrumentation () {
3738
3839 @ Override
3940 public DataLoaderInstrumentationContext <DispatchResult <?>> beginDispatch (DataLoader <?, ?> dataLoader ) {
4041 dlRef .set (dataLoader );
41-
42- long then = System .currentTimeMillis ();
42+ stopwatch .start ();
4343 return new DataLoaderInstrumentationContext <>() {
4444 @ Override
4545 public void onCompleted (DispatchResult <?> result , Throwable t ) {
46- timer . set ( System . currentTimeMillis () - then );
46+ stopwatch . stop ( );
4747 }
4848 };
4949 }
@@ -54,24 +54,32 @@ public DataLoaderInstrumentationContext<List<?>> beginBatchLoader(DataLoader<?,
5454 }
5555 };
5656
57- DataLoaderOptions options = new DataLoaderOptions ().setInstrumentation (instrumentation ).setMaxBatchSize (5 );
57+ DataLoaderOptions options = new DataLoaderOptions ()
58+ .setInstrumentation (instrumentation )
59+ .setMaxBatchSize (5 );
5860
5961 DataLoader <String , String > dl = DataLoaderFactory .newDataLoader (snoozingBatchLoader , options );
6062
63+ List <String > keys = new ArrayList <>();
6164 for (int i = 0 ; i < 20 ; i ++) {
62- dl .load ("X" + i );
65+ String key = "X" + i ;
66+ keys .add (key );
67+ dl .load (key );
6368 }
6469
6570 CompletableFuture <List <String >> dispatch = dl .dispatch ();
6671
6772 await ().until (dispatch ::isDone );
68- assertThat (timer .get (), greaterThan (150L )); // we must have called batch load 4 times
73+ // we must have called batch load 4 times at 100ms snooze per call
74+ // but its in parallel via supplyAsync
75+ assertThat (stopwatch .elapsed (), greaterThan (75L ));
6976 assertThat (dlRef .get (), is (dl ));
77+ assertThat (dispatch .join (), equalTo (keys ));
7078 }
7179
7280 @ Test
7381 void canMonitorBatchLoading () {
74- AtomicLong timer = new AtomicLong ();
82+ Stopwatch stopwatch = Stopwatch . stopwatchUnStarted ();
7583 AtomicReference <BatchLoaderEnvironment > beRef = new AtomicReference <>();
7684 AtomicReference <DataLoader <?, ?>> dlRef = new AtomicReference <>();
7785
@@ -82,11 +90,11 @@ public DataLoaderInstrumentationContext<List<?>> beginBatchLoader(DataLoader<?,
8290 dlRef .set (dataLoader );
8391 beRef .set (environment );
8492
85- long then = System . currentTimeMillis ();
93+ stopwatch . start ();
8694 return new DataLoaderInstrumentationContext <>() {
8795 @ Override
8896 public void onCompleted (List <?> result , Throwable t ) {
89- timer . set ( System . currentTimeMillis () - then );
97+ stopwatch . stop ( );
9098 }
9199 };
92100 }
@@ -95,13 +103,13 @@ public void onCompleted(List<?> result, Throwable t) {
95103 DataLoaderOptions options = new DataLoaderOptions ().setInstrumentation (instrumentation );
96104 DataLoader <String , String > dl = DataLoaderFactory .newDataLoader (snoozingBatchLoader , options );
97105
98- dl .load ("A" );
99- dl .load ("B" );
106+ dl .load ("A" , "kcA" );
107+ dl .load ("B" , "kcB" );
100108
101109 CompletableFuture <List <String >> dispatch = dl .dispatch ();
102110
103111 await ().until (dispatch ::isDone );
104- assertThat (timer . get (), greaterThan (50L ));
112+ assertThat (stopwatch . elapsed (), greaterThan (50L ));
105113 assertThat (dlRef .get (), is (dl ));
106114 assertThat (beRef .get ().getKeyContexts ().keySet (), equalTo (Set .of ("A" , "B" )));
107115 }
0 commit comments