Skip to content

Commit

Permalink
Refactor jballerina integration tests to use more Paths instead of St…
Browse files Browse the repository at this point in the history
…rings
  • Loading branch information
Shadow-Devil committed Feb 17, 2025
1 parent 0cc005f commit 47922b4
Show file tree
Hide file tree
Showing 70 changed files with 538 additions and 534 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.ballerina.runtime.internal.scheduling.RuntimeRegistry;
import io.ballerina.runtime.internal.scheduling.Scheduler;
import io.ballerina.runtime.internal.values.FutureValue;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import io.ballerina.runtime.internal.values.FutureValue;
import io.ballerina.runtime.internal.values.ObjectValue;
import io.ballerina.runtime.internal.values.ValueCreator;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.ballerina.runtime.internal.utils.ErrorUtils;
import io.ballerina.runtime.transactions.TransactionLocalContext;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,14 @@ private void prepareFor(DebugInstruction instruction, int threadId) {

private Variable[] computeGlobalScopeVariables(VariablesArguments requestArgs) {
int stackFrameReference = requestArgs.getVariablesReference();
String classQName = PackageUtils.getQualifiedClassName(suspendedContext, INIT_CLASS_NAME);
List<ReferenceType> cls = suspendedContext.getAttachedVm().classesByName(classQName);
String classQName = PackageUtils.getQualifiedClassName(
Objects.requireNonNull(suspendedContext), INIT_CLASS_NAME);
List<ReferenceType> cls = Objects.requireNonNull(suspendedContext).getAttachedVm().classesByName(classQName);
if (cls.size() != 1) {
return new Variable[0];
}
List<CompletableFuture<Variable>> scheduledVariables = new ArrayList<>();
ReferenceType initClassReference = cls.get(0);
ReferenceType initClassReference = cls.getFirst();
for (Field field : initClassReference.allFields()) {
String fieldName = Utils.decodeIdentifier(field.name());
if (!field.isPublic() || !field.isStatic() || fieldName.startsWith(GENERATED_VAR_PREFIX)) {
Expand Down Expand Up @@ -1080,16 +1081,19 @@ private CompletableFuture<Variable[]> fetchLocalVariablesFromMap(VariablesArgume
private CompletableFuture<Variable> computeVariableAsync(String name, Value value, Integer stackFrameRef) {
return CompletableFuture.supplyAsync(() -> {
BVariable variable = VariableFactory.getVariable(suspendedContext, name, value);
if (variable == null) {
return null;
}
if (variable instanceof BSimpleVariable) {
variable.getDapVariable().setVariablesReference(0);
} else if (variable instanceof BCompoundVariable) {
int variableReference = nextVarReference.getAndIncrement();
variable.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, (BCompoundVariable) variable);
updateVariableToStackFrameMap(stackFrameRef, variableReference);
switch (variable) {
case null -> {
return null;
}
case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
case BCompoundVariable bCompoundVariable -> {
int variableReference = nextVarReference.getAndIncrement();
variable.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, bCompoundVariable);
updateVariableToStackFrameMap(stackFrameRef, variableReference);
}
default -> {
}
}
return variable.getDapVariable();
}, variableExecutor);
Expand Down Expand Up @@ -1135,15 +1139,19 @@ private Variable[] createVariableArrayFrom(VariablesArguments args, Map<String,
String name = entry.getKey();
Value value = entry.getValue();
BVariable variable = VariableFactory.getVariable(suspendedContext, name, value);
if (variable == null) {
return null;
} else if (variable instanceof BSimpleVariable) {
variable.getDapVariable().setVariablesReference(0);
} else if (variable instanceof BCompoundVariable) {
int variableReference = nextVarReference.getAndIncrement();
variable.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, (BCompoundVariable) variable);
updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
switch (variable) {
case null -> {
return null;
}
case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
case BCompoundVariable bCompoundVariable -> {
int variableReference = nextVarReference.getAndIncrement();
variable.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, bCompoundVariable);
updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
}
default -> {
}
}
return variable.getDapVariable();
}).filter(Objects::nonNull).toArray(Variable[]::new);
Expand All @@ -1156,15 +1164,19 @@ private Variable[] createVariableArrayFrom(VariablesArguments args, List<Value>
return varMap.stream().map(value -> {
String name = String.format("[%d]", index.getAndIncrement());
BVariable variable = VariableFactory.getVariable(suspendedContext, name, value);
if (variable == null) {
return null;
} else if (variable instanceof BSimpleVariable) {
variable.getDapVariable().setVariablesReference(0);
} else if (variable instanceof BCompoundVariable) {
int variableReference = nextVarReference.getAndIncrement();
variable.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, (BCompoundVariable) variable);
updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
switch (variable) {
case null -> {
return null;
}
case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
case BCompoundVariable bCompoundVariable -> {
int variableReference = nextVarReference.getAndIncrement();
variable.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, bCompoundVariable);
updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
}
default -> {
}
}
return variable.getDapVariable();
}).filter(Objects::nonNull).toArray(Variable[]::new);
Expand All @@ -1178,15 +1190,19 @@ private Variable[] createVariableArrayFrom(VariablesArguments args, List<Value>
*/
private EvaluateResponse constructEvaluateResponse(EvaluateArguments args, BVariable evaluationResult) {
EvaluateResponse response = new EvaluateResponse();
if (evaluationResult == null) {
return response;
} else if (evaluationResult instanceof BSimpleVariable) {
evaluationResult.getDapVariable().setVariablesReference(0);
} else if (evaluationResult instanceof BCompoundVariable) {
int variableReference = nextVarReference.getAndIncrement();
evaluationResult.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, (BCompoundVariable) evaluationResult);
updateVariableToStackFrameMap(args.getFrameId(), variableReference);
switch (evaluationResult) {
case null -> {
return response;
}
case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
case BCompoundVariable bCompoundVariable -> {
int variableReference = nextVarReference.getAndIncrement();
evaluationResult.getDapVariable().setVariablesReference(variableReference);
loadedCompoundVariables.put(variableReference, bCompoundVariable);
updateVariableToStackFrameMap(args.getFrameId(), variableReference);
}
default -> {
}
}

Variable dapVariable = evaluationResult.getDapVariable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Map<String, Value> computeChildVariables() {
@Nullable
private Map<Value, Value> getRecordFields() {
try {
loadAllKeys();
ArrayReference loadedKeys = loadAllKeys();
Map<Value, Value> recordFields = new LinkedHashMap<>();
if (loadedKeys == null) {
return null;
Expand All @@ -105,19 +105,22 @@ private Map<Value, Value> getRecordFields() {
}
}

private void loadAllKeys() {
if (loadedKeys == null) {
try {
Optional<Method> getKeysMethod = VariableUtils.getMethod(jvmValue, METHOD_GET_KEYS,
GETKEYS_METHOD_SIGNATURE_PATTERN);
Value keyArray = ((ObjectReference) jvmValue).invokeMethod(
context.getOwningThread().getThreadReference(), getKeysMethod.get(), Collections.emptyList(),
ObjectReference.INVOKE_SINGLE_THREADED);
loadedKeys = (ArrayReference) keyArray;
} catch (Exception ignored) {
loadedKeys = null;
}
@Nullable
private ArrayReference loadAllKeys() {
if (loadedKeys != null) {
return loadedKeys;
}
try {
Optional<Method> getKeysMethod = VariableUtils.getMethod(jvmValue, METHOD_GET_KEYS,
GETKEYS_METHOD_SIGNATURE_PATTERN);
Value keyArray = ((ObjectReference) jvmValue).invokeMethod(
context.getOwningThread().getThreadReference(), getKeysMethod.get(), Collections.emptyList(),
ObjectReference.INVOKE_SINGLE_THREADED);
loadedKeys = (ArrayReference) keyArray;
} catch (Exception ignored) {
loadedKeys = null;
}
return loadedKeys;
}

@Nullable
Expand All @@ -140,7 +143,7 @@ public int getChildrenCount() {
if (!(jvmValue instanceof ObjectReference)) {
return 0;
}
loadAllKeys();
ArrayReference loadedKeys = loadAllKeys();
return loadedKeys == null ? 0 : loadedKeys.length();
} catch (Exception ignored) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.ballerinalang.test.context;

import java.nio.file.Path;
import java.util.Map;

/**
Expand All @@ -34,7 +35,7 @@ public interface BMain {
* @param balFile bal file path
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String balFile) throws BallerinaTestException;
void runMain(Path balFile) throws BallerinaTestException;

/**
* Run ballerina main function provided .bal file.
Expand All @@ -43,7 +44,7 @@ public interface BMain {
* @param leechers log leechers to check the log if any
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String balFile, LogLeecher[] leechers) throws BallerinaTestException;
void runMain(Path balFile, LogLeecher[] leechers) throws BallerinaTestException;

/**
* Run ballerina main function provided .bal file.
Expand All @@ -53,7 +54,7 @@ public interface BMain {
* @param args arguments to pass
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String balFile, String[] flags, String[] args) throws BallerinaTestException;
void runMain(Path balFile, String[] flags, String[] args) throws BallerinaTestException;

/**
* Run ballerina main function provided .bal file.
Expand All @@ -64,7 +65,7 @@ public interface BMain {
* @param leechers log leechers to check the log if any
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String balFile, String[] flags, String[] args, LogLeecher[] leechers) throws BallerinaTestException;
void runMain(Path balFile, String[] flags, String[] args, LogLeecher[] leechers) throws BallerinaTestException;

/**
* Run ballerina main function provided .bal file.
Expand All @@ -76,7 +77,7 @@ public interface BMain {
* @param clientArgs arguments which program expects
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String balFile, String[] flags, String[] args, Map<String, String> envProperties,
void runMain(Path balFile, String[] flags, String[] args, Map<String, String> envProperties,
String[] clientArgs) throws BallerinaTestException;

/**
Expand All @@ -90,7 +91,7 @@ void runMain(String balFile, String[] flags, String[] args, Map<String, String>
* @param leechers log leechers to check the log if any
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String balFile, String[] flags, String[] args, Map<String, String> envProperties,
void runMain(Path balFile, String[] flags, String[] args, Map<String, String> envProperties,
String[] clientArgs, LogLeecher[] leechers) throws BallerinaTestException;
// ********************* End bal file run methods. *******************

Expand All @@ -103,7 +104,7 @@ void runMain(String balFile, String[] flags, String[] args, Map<String, String>
* @param packagePath package path
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String sourceRoot, String packagePath) throws BallerinaTestException;
void runMain(Path sourceRoot, String packagePath) throws BallerinaTestException;

/**
* Run ballerina main function provided bal package.
Expand All @@ -113,7 +114,7 @@ void runMain(String balFile, String[] flags, String[] args, Map<String, String>
* @param leechers log leechers to check the log if any
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String sourceRoot, String packagePath, LogLeecher[] leechers) throws BallerinaTestException;
void runMain(Path sourceRoot, String packagePath, LogLeecher[] leechers) throws BallerinaTestException;

/**
* Run ballerina main function provided bal package.
Expand All @@ -124,7 +125,7 @@ void runMain(String balFile, String[] flags, String[] args, Map<String, String>
* @param args arguments to parse
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String sourceRoot, String packagePath, String[] flags, String[] args) throws BallerinaTestException;
void runMain(Path sourceRoot, String packagePath, String[] flags, String[] args) throws BallerinaTestException;

/**
* Run ballerina main function provided bal package.
Expand All @@ -136,7 +137,7 @@ void runMain(String balFile, String[] flags, String[] args, Map<String, String>
* @param leechers log leechers to check the log if any
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String sourceRoot, String packagePath, String[] flags, String[] args,
void runMain(Path sourceRoot, String packagePath, String[] flags, String[] args,
LogLeecher[] leechers) throws BallerinaTestException;

/**
Expand All @@ -150,7 +151,7 @@ void runMain(String sourceRoot, String packagePath, String[] flags, String[] arg
* @param clientArgs arguments which program expects
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String sourceRoot, String packagePath, String[] flags, String[] args,
void runMain(Path sourceRoot, String packagePath, String[] flags, String[] args,
Map<String, String> envProperties, String[] clientArgs) throws BallerinaTestException;

/**
Expand All @@ -165,7 +166,7 @@ void runMain(String sourceRoot, String packagePath, String[] flags, String[] arg
* @param leechers log leechers to check the log if any
* @throws BallerinaTestException if any exception is thrown when running the main function
*/
void runMain(String sourceRoot, String packagePath,
void runMain(Path sourceRoot, String packagePath,
String[] flags, String[] args, Map<String, String> envProperties,
String[] clientArgs, LogLeecher[] leechers) throws BallerinaTestException;
// ********************* End bal package run methods. *******************
Expand Down
Loading

0 comments on commit 47922b4

Please sign in to comment.