diff options
Diffstat (limited to 'store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java')
-rw-r--r-- | store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java new file mode 100644 index 00000000..1b8b38c4 --- /dev/null +++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java | |||
@@ -0,0 +1,109 @@ | |||
1 | package tools.refinery.data.map.tests.fuzz; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.fail; | ||
4 | |||
5 | import java.util.HashMap; | ||
6 | import java.util.Map; | ||
7 | import java.util.Random; | ||
8 | import java.util.stream.Stream; | ||
9 | |||
10 | import org.junit.jupiter.api.Tag; | ||
11 | import org.junit.jupiter.api.Timeout; | ||
12 | import org.junit.jupiter.params.ParameterizedTest; | ||
13 | import org.junit.jupiter.params.provider.Arguments; | ||
14 | import org.junit.jupiter.params.provider.MethodSource; | ||
15 | |||
16 | import tools.refinery.data.map.ContinousHashProvider; | ||
17 | import tools.refinery.data.map.VersionedMapStore; | ||
18 | import tools.refinery.data.map.VersionedMapStoreImpl; | ||
19 | import tools.refinery.data.map.internal.VersionedMapImpl; | ||
20 | import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils; | ||
21 | import tools.refinery.data.map.tests.utils.MapTestEnvironment; | ||
22 | |||
23 | class RestoreFuzzTest { | ||
24 | private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency, | ||
25 | boolean evilHash) { | ||
26 | String[] values = MapTestEnvironment.prepareValues(maxValue); | ||
27 | ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash); | ||
28 | |||
29 | VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]); | ||
30 | |||
31 | iterativeRandomPutsAndCommitsThenRestore(scenario, store, steps, maxKey, values, seed, commitFrequency); | ||
32 | } | ||
33 | |||
34 | private void iterativeRandomPutsAndCommitsThenRestore(String scenario, VersionedMapStore<Integer, String> store, | ||
35 | int steps, int maxKey, String[] values, int seed, int commitFrequency) { | ||
36 | // 1. build a map with versions | ||
37 | Random r = new Random(seed); | ||
38 | VersionedMapImpl<Integer, String> versioned = (VersionedMapImpl<Integer, String>) store.createMap(); | ||
39 | Map<Integer, Long> index2Version = new HashMap<>(); | ||
40 | |||
41 | for (int i = 0; i < steps; i++) { | ||
42 | int index = i + 1; | ||
43 | int nextKey = r.nextInt(maxKey); | ||
44 | String nextValue = values[r.nextInt(values.length)]; | ||
45 | try { | ||
46 | versioned.put(nextKey, nextValue); | ||
47 | } catch (Exception exception) { | ||
48 | exception.printStackTrace(); | ||
49 | fail(scenario + ":" + index + ": exception happened: " + exception); | ||
50 | } | ||
51 | if (index % commitFrequency == 0) { | ||
52 | long version = versioned.commit(); | ||
53 | index2Version.put(i, version); | ||
54 | } | ||
55 | MapTestEnvironment.printStatus(scenario, index, steps, "building"); | ||
56 | } | ||
57 | // 2. create a non-versioned and | ||
58 | VersionedMapImpl<Integer, String> reference = (VersionedMapImpl<Integer, String>) store.createMap(); | ||
59 | r = new Random(seed); | ||
60 | |||
61 | for (int i = 0; i < steps; i++) { | ||
62 | int index = i + 1; | ||
63 | int nextKey = r.nextInt(maxKey); | ||
64 | String nextValue = values[r.nextInt(values.length)]; | ||
65 | try { | ||
66 | reference.put(nextKey, nextValue); | ||
67 | } catch (Exception exception) { | ||
68 | exception.printStackTrace(); | ||
69 | fail(scenario + ":" + index + ": exception happened: " + exception); | ||
70 | } | ||
71 | if (index % commitFrequency == 0) { | ||
72 | versioned.restore(index2Version.get(i)); | ||
73 | MapTestEnvironment.compareTwoMaps(scenario + ":" + index, reference, versioned); | ||
74 | } | ||
75 | MapTestEnvironment.printStatus(scenario, index, steps, "comparison"); | ||
76 | } | ||
77 | |||
78 | } | ||
79 | |||
80 | @ParameterizedTest(name = "Restore {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}") | ||
81 | @MethodSource | ||
82 | @Timeout(value = 10) | ||
83 | @Tag("smoke") | ||
84 | void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed, | ||
85 | boolean evilHash) { | ||
86 | runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues, | ||
87 | commitFrequency, evilHash); | ||
88 | } | ||
89 | |||
90 | static Stream<Arguments> parametrizedFastFuzz() { | ||
91 | return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 }, | ||
92 | new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 }, | ||
93 | new Object[] { false, true }); | ||
94 | } | ||
95 | |||
96 | @ParameterizedTest(name = "Restore {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}") | ||
97 | @MethodSource | ||
98 | @Tag("smoke") | ||
99 | @Tag("slow") | ||
100 | void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed, | ||
101 | boolean evilHash) { | ||
102 | runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues, | ||
103 | commitFrequency, evilHash); | ||
104 | } | ||
105 | |||
106 | static Stream<Arguments> parametrizedSlowFuzz() { | ||
107 | return FuzzTestUtils.changeStepCount(RestoreFuzzTest.parametrizedFastFuzz(), 1); | ||
108 | } | ||
109 | } | ||