Skip to content

Commit 97364ed

Browse files
committed
Instrumentatin support for dataloader - better tests
1 parent e55b643 commit 97364ed

File tree

2 files changed

+79
-14
lines changed

2 files changed

+79
-14
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.dataloader.fixtures;
2+
3+
import java.time.Duration;
4+
5+
public class Stopwatch {
6+
7+
public static Stopwatch stopwatchStarted() {
8+
return new Stopwatch().start();
9+
}
10+
11+
public static Stopwatch stopwatchUnStarted() {
12+
return new Stopwatch();
13+
}
14+
15+
private long started = -1;
16+
private long stopped = -1;
17+
18+
public Stopwatch start() {
19+
synchronized (this) {
20+
if (started != -1) {
21+
throw new IllegalStateException("You have started it before");
22+
}
23+
started = System.currentTimeMillis();
24+
}
25+
return this;
26+
}
27+
28+
private Stopwatch() {
29+
}
30+
31+
public long elapsed() {
32+
synchronized (this) {
33+
if (started == -1) {
34+
throw new IllegalStateException("You haven't started it");
35+
}
36+
if (stopped == -1) {
37+
return System.currentTimeMillis() - started;
38+
} else {
39+
return stopped - started;
40+
}
41+
}
42+
}
43+
44+
public Duration duration() {
45+
return Duration.ofMillis(elapsed());
46+
}
47+
48+
public Duration stop() {
49+
synchronized (this) {
50+
if (started != -1) {
51+
throw new IllegalStateException("You have started it");
52+
}
53+
stopped = System.currentTimeMillis();
54+
return duration();
55+
}
56+
}
57+
}

src/test/java/org/dataloader/instrumentation/DataLoaderInstrumentationTest.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import org.dataloader.DataLoaderFactory;
77
import org.dataloader.DataLoaderOptions;
88
import org.dataloader.DispatchResult;
9+
import org.dataloader.fixtures.Stopwatch;
910
import org.dataloader.fixtures.TestKit;
1011
import org.junit.jupiter.api.Test;
1112

13+
import java.util.ArrayList;
1214
import java.util.List;
1315
import java.util.Set;
1416
import java.util.concurrent.CompletableFuture;
15-
import java.util.concurrent.atomic.AtomicLong;
1617
import java.util.concurrent.atomic.AtomicReference;
1718

1819
import 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

Comments
 (0)