|
| 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 | +} |
0 commit comments