Skip to content

Commit bcf7ef6

Browse files
committed
add another jcstress test
1 parent ee8d568 commit bcf7ef6

File tree

7 files changed

+95
-4
lines changed

7 files changed

+95
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,5 @@ tasks.named("dependencyUpdates").configure {
263263
}
264264

265265
jcstress {
266-
verbose = true
266+
// verbose = true
267267
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@JCStressTest
1717
@State
1818
@Outcome(id = "2000, 2000", expect = ACCEPTABLE, desc = "accepted")
19-
public class DataLoaderBatchingAndCachingDispatchJCStress {
19+
public class DataLoader_Batching_Caching_JCStress {
2020

2121

2222
AtomicInteger counter = new AtomicInteger();
@@ -33,7 +33,7 @@ public class DataLoaderBatchingAndCachingDispatchJCStress {
3333
};
3434
DataLoader<String, String> dataLoader = DataLoaderFactory.newDataLoader(batchLoader);
3535

36-
public DataLoaderBatchingAndCachingDispatchJCStress() {
36+
public DataLoader_Batching_Caching_JCStress() {
3737

3838
}
3939

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.dataloader;
2+
3+
import org.openjdk.jcstress.annotations.Actor;
4+
import org.openjdk.jcstress.annotations.Arbiter;
5+
import org.openjdk.jcstress.annotations.JCStressTest;
6+
import org.openjdk.jcstress.annotations.Outcome;
7+
import org.openjdk.jcstress.annotations.State;
8+
import org.openjdk.jcstress.infra.results.II_Result;
9+
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.atomic.AtomicInteger;
12+
13+
import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE;
14+
import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE_INTERESTING;
15+
16+
@JCStressTest
17+
@State
18+
@Outcome(id = "2000, 2000", expect = ACCEPTABLE, desc = "No keys loaded twice")
19+
@Outcome(id = "2.*, 2000", expect = ACCEPTABLE_INTERESTING, desc = "Some keys loaded twice")
20+
public class DataLoader_NoBatching_Caching_JCStress {
21+
22+
23+
AtomicInteger batchLoaderCount = new AtomicInteger();
24+
volatile boolean finished1;
25+
volatile boolean finished2;
26+
27+
28+
BatchLoader<String, String> batchLoader = keys -> {
29+
batchLoaderCount.getAndAdd(keys.size());
30+
return CompletableFuture.completedFuture(keys);
31+
};
32+
DataLoader<String, String> dataLoader = DataLoaderFactory.newDataLoader(batchLoader, DataLoaderOptions.newOptions().setBatchingEnabled(false).build());
33+
34+
35+
@Actor
36+
public void load1() {
37+
for (int i = 0; i < 1000; i++) {
38+
dataLoader.load("load-1-" + i);
39+
}
40+
// we load the same keys again
41+
for (int i = 0; i < 1000; i++) {
42+
dataLoader.load("load-1-" + i);
43+
}
44+
finished1 = true;
45+
}
46+
47+
@Actor
48+
public void load2() {
49+
for (int i = 0; i < 1000; i++) {
50+
dataLoader.load("load-2-" + i);
51+
}
52+
// we load the same keys again
53+
for (int i = 0; i < 1000; i++) {
54+
dataLoader.load("load-1-" + i);
55+
}
56+
finished2 = true;
57+
}
58+
59+
60+
@Arbiter
61+
public void arbiter(II_Result r) {
62+
r.r1 = batchLoaderCount.get();
63+
r.r2 = dataLoader.getCacheMap().size();
64+
}
65+
66+
}

src/main/java/org/dataloader/CacheMap.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ static <K, V> CacheMap<K, V> simpleMap() {
7474
*
7575
* @return the cached value, or {@code null} if not found (depends on cache implementation)
7676
*/
77-
@Nullable CompletableFuture<V> get(K key);
77+
@Nullable CompletableFuture<V> get(K key);
7878

7979
/**
8080
* Gets a collection of CompletableFutures from the cache map.
81+
*
8182
* @return the collection of cached values
8283
*/
8384
Collection<CompletableFuture<V>> getAll();
@@ -107,4 +108,13 @@ static <K, V> CacheMap<K, V> simpleMap() {
107108
* @return the cache map for fluent coding
108109
*/
109110
CacheMap<K, V> clear();
111+
112+
/**
113+
* Returns the current size of the cache. This is not used by DataLoader directly
114+
* and intended for testing and debugging.
115+
* If a cache doesn't support it, it can throw an Exception.
116+
*
117+
* @return
118+
*/
119+
int size();
110120
}

src/main/java/org/dataloader/impl/DefaultCacheMap.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,9 @@ public CacheMap<K, V> clear() {
9393
cache.clear();
9494
return this;
9595
}
96+
97+
@Override
98+
public int size() {
99+
return cache.size();
100+
}
96101
}

src/test/java/ReadmeExamples.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ public CacheMap delete(Object key) {
278278
public CacheMap clear() {
279279
return null;
280280
}
281+
282+
@Override
283+
public int size() {
284+
return 0;
285+
}
281286
}
282287

283288
private void customCache() {

src/test/java/org/dataloader/fixtures/CustomCacheMap.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public CacheMap<String, Object> clear() {
4545
stash.clear();
4646
return this;
4747
}
48+
49+
@Override
50+
public int size() {
51+
return stash.size();
52+
}
4853
}

0 commit comments

Comments
 (0)