Skip to content

Commit

Permalink
Reset parameter generator expanding ranges for each scenario
Browse files Browse the repository at this point in the history
Fixes #179 

---------

Co-authored-by: Aleksandr.Potapov <[email protected]>
Co-authored-by: Nikita Koval <[email protected]>
  • Loading branch information
3 people authored May 12, 2023
1 parent 7cb4d28 commit 18e78a3
Show file tree
Hide file tree
Showing 33 changed files with 425 additions and 580 deletions.
21 changes: 15 additions & 6 deletions src/jvm/main/org/jetbrains/kotlinx/lincheck/CTestStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import org.jetbrains.kotlinx.lincheck.annotations.*;
import org.jetbrains.kotlinx.lincheck.execution.*;
import org.jetbrains.kotlinx.lincheck.paramgen.*;
import org.jetbrains.kotlinx.lincheck.strategy.stress.*;

import java.lang.reflect.*;
Expand All @@ -28,15 +27,17 @@
*/
public class CTestStructure {
public final List<ActorGenerator> actorGenerators;
public final List<ParameterGenerator<?>> parameterGenerators;
public final List<OperationGroup> operationGroups;
public final List<Method> validationFunctions;
public final Method stateRepresentation;

public final RandomProvider randomProvider;

private CTestStructure(List<ActorGenerator> actorGenerators, List<OperationGroup> operationGroups,
private CTestStructure(List<ActorGenerator> actorGenerators, List<ParameterGenerator<?>> parameterGenerators, List<OperationGroup> operationGroups,
List<Method> validationFunctions, Method stateRepresentation, RandomProvider randomProvider) {
this.actorGenerators = actorGenerators;
this.parameterGenerators = parameterGenerators;
this.operationGroups = operationGroups;
this.validationFunctions = validationFunctions;
this.stateRepresentation = stateRepresentation;
Expand All @@ -54,8 +55,10 @@ public static CTestStructure getFromTestClass(Class<?> testClass) {
List<Method> stateRepresentations = new ArrayList<>();
Class<?> clazz = testClass;
RandomProvider randomProvider = new RandomProvider();
Map<Class<?>, ParameterGenerator<?>> parameterGeneratorsMap = new HashMap<>();

while (clazz != null) {
readTestStructureFromClass(clazz, namedGens, groupConfigs, actorGenerators, validationFunctions, stateRepresentations, randomProvider);
readTestStructureFromClass(clazz, namedGens, groupConfigs, actorGenerators, parameterGeneratorsMap, validationFunctions, stateRepresentations, randomProvider);
clazz = clazz.getSuperclass();
}
if (stateRepresentations.size() > 1) {
Expand All @@ -67,13 +70,16 @@ public static CTestStructure getFromTestClass(Class<?> testClass) {
if (!stateRepresentations.isEmpty())
stateRepresentation = stateRepresentations.get(0);
// Create StressCTest class configuration
return new CTestStructure(actorGenerators, new ArrayList<>(groupConfigs.values()), validationFunctions, stateRepresentation, randomProvider);
List<ParameterGenerator<?>> parameterGenerators = new ArrayList<>(parameterGeneratorsMap.values());

return new CTestStructure(actorGenerators, parameterGenerators, new ArrayList<>(groupConfigs.values()), validationFunctions, stateRepresentation, randomProvider);
}

@SuppressWarnings("removal")
private static void readTestStructureFromClass(Class<?> clazz, Map<String, ParameterGenerator<?>> namedGens,
Map<String, OperationGroup> groupConfigs,
List<ActorGenerator> actorGenerators,
Map<Class<?>, ParameterGenerator<?>> parameterGeneratorsMap,
List<Method> validationFunctions,
List<Method> stateRepresentations,
RandomProvider randomProvider) {
Expand Down Expand Up @@ -107,7 +113,10 @@ private static void readTestStructureFromClass(Class<?> clazz, Map<String, Param
int nParameters = m.getParameterCount() - (isSuspendableMethod ? 1 : 0);
for (int i = 0; i < nParameters; i++) {
String nameInOperation = opAnn.params().length > 0 ? opAnn.params()[i] : null;
gens.add(getOrCreateGenerator(m, m.getParameters()[i], nameInOperation, namedGens, defaultGens, randomProvider));
Parameter parameter = m.getParameters()[i];
ParameterGenerator<?> parameterGenerator = getOrCreateGenerator(m, parameter, nameInOperation, namedGens, defaultGens, randomProvider);
parameterGeneratorsMap.putIfAbsent(parameter.getType(), parameterGenerator);
gens.add(parameterGenerator);
}
// Get list of handled exceptions if they are presented
List<Class<? extends Throwable>> handledExceptions = Arrays.asList(opAnn.handleExceptionsAsResult());
Expand Down Expand Up @@ -184,7 +193,7 @@ private static ParameterGenerator<?> getOrCreateGenerator(Method m,
+ m.getName() + "\" should be specified.");
}
// If the @Param annotation is presented check it's correctness firstly
if (!paramAnn.name().isEmpty() && !(paramAnn.gen() == ParameterGenerator.Dummy.class))
if (!paramAnn.name().isEmpty() && !(paramAnn.gen() == DummyParameterGenerator.class))
throw new IllegalStateException("@Param should have either name or gen with optionally configuration");
// If @Param annotation specifies generator's name then return the specified generator
if (!paramAnn.name().isEmpty())
Expand Down
2 changes: 2 additions & 0 deletions src/jvm/main/org/jetbrains/kotlinx/lincheck/LinChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class LinChecker (private val testClass: Class<*>, options: Options<*, *>?) {
reporter.logFailedIteration(minimizedFailedIteration)
return minimizedFailedIteration
}
// Reset the parameter generator ranges to start with the same initial bounds on each scenario generation.
testStructure.parameterGenerators.forEach { it.reset() }
}
return null
}
Expand Down
Loading

0 comments on commit 18e78a3

Please sign in to comment.