6
6
import org .dataloader .DataLoaderFactory ;
7
7
import org .dataloader .DataLoaderOptions ;
8
8
import org .dataloader .DispatchResult ;
9
+ import org .dataloader .fixtures .Stopwatch ;
9
10
import org .dataloader .fixtures .TestKit ;
10
11
import org .junit .jupiter .api .Test ;
11
12
13
+ import java .util .ArrayList ;
12
14
import java .util .List ;
13
15
import java .util .Set ;
14
16
import java .util .concurrent .CompletableFuture ;
15
- import java .util .concurrent .atomic .AtomicLong ;
16
17
import java .util .concurrent .atomic .AtomicReference ;
17
18
18
19
import static org .awaitility .Awaitility .await ;
@@ -30,20 +31,19 @@ class DataLoaderInstrumentationTest {
30
31
31
32
@ Test
32
33
void canMonitorDispatching () {
33
- AtomicLong timer = new AtomicLong ();
34
+ Stopwatch stopwatch = Stopwatch . stopwatchUnStarted ();
34
35
AtomicReference <DataLoader <?, ?>> dlRef = new AtomicReference <>();
35
36
36
37
DataLoaderInstrumentation instrumentation = new DataLoaderInstrumentation () {
37
38
38
39
@ Override
39
40
public DataLoaderInstrumentationContext <DispatchResult <?>> beginDispatch (DataLoader <?, ?> dataLoader ) {
40
41
dlRef .set (dataLoader );
41
-
42
- long then = System .currentTimeMillis ();
42
+ stopwatch .start ();
43
43
return new DataLoaderInstrumentationContext <>() {
44
44
@ Override
45
45
public void onCompleted (DispatchResult <?> result , Throwable t ) {
46
- timer . set ( System . currentTimeMillis () - then );
46
+ stopwatch . stop ( );
47
47
}
48
48
};
49
49
}
@@ -54,24 +54,32 @@ public DataLoaderInstrumentationContext<List<?>> beginBatchLoader(DataLoader<?,
54
54
}
55
55
};
56
56
57
- DataLoaderOptions options = new DataLoaderOptions ().setInstrumentation (instrumentation ).setMaxBatchSize (5 );
57
+ DataLoaderOptions options = new DataLoaderOptions ()
58
+ .setInstrumentation (instrumentation )
59
+ .setMaxBatchSize (5 );
58
60
59
61
DataLoader <String , String > dl = DataLoaderFactory .newDataLoader (snoozingBatchLoader , options );
60
62
63
+ List <String > keys = new ArrayList <>();
61
64
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 );
63
68
}
64
69
65
70
CompletableFuture <List <String >> dispatch = dl .dispatch ();
66
71
67
72
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 ));
69
76
assertThat (dlRef .get (), is (dl ));
77
+ assertThat (dispatch .join (), equalTo (keys ));
70
78
}
71
79
72
80
@ Test
73
81
void canMonitorBatchLoading () {
74
- AtomicLong timer = new AtomicLong ();
82
+ Stopwatch stopwatch = Stopwatch . stopwatchUnStarted ();
75
83
AtomicReference <BatchLoaderEnvironment > beRef = new AtomicReference <>();
76
84
AtomicReference <DataLoader <?, ?>> dlRef = new AtomicReference <>();
77
85
@@ -82,11 +90,11 @@ public DataLoaderInstrumentationContext<List<?>> beginBatchLoader(DataLoader<?,
82
90
dlRef .set (dataLoader );
83
91
beRef .set (environment );
84
92
85
- long then = System . currentTimeMillis ();
93
+ stopwatch . start ();
86
94
return new DataLoaderInstrumentationContext <>() {
87
95
@ Override
88
96
public void onCompleted (List <?> result , Throwable t ) {
89
- timer . set ( System . currentTimeMillis () - then );
97
+ stopwatch . stop ( );
90
98
}
91
99
};
92
100
}
@@ -95,13 +103,13 @@ public void onCompleted(List<?> result, Throwable t) {
95
103
DataLoaderOptions options = new DataLoaderOptions ().setInstrumentation (instrumentation );
96
104
DataLoader <String , String > dl = DataLoaderFactory .newDataLoader (snoozingBatchLoader , options );
97
105
98
- dl .load ("A" );
99
- dl .load ("B" );
106
+ dl .load ("A" , "kcA" );
107
+ dl .load ("B" , "kcB" );
100
108
101
109
CompletableFuture <List <String >> dispatch = dl .dispatch ();
102
110
103
111
await ().until (dispatch ::isDone );
104
- assertThat (timer . get (), greaterThan (50L ));
112
+ assertThat (stopwatch . elapsed (), greaterThan (50L ));
105
113
assertThat (dlRef .get (), is (dl ));
106
114
assertThat (beRef .get ().getKeyContexts ().keySet (), equalTo (Set .of ("A" , "B" )));
107
115
}
0 commit comments