Skip to content

Commit

Permalink
Merge pull request #43471 from warunalakshitha/profiler_test_fix
Browse files Browse the repository at this point in the history
[Java 21] Fix profiler test
  • Loading branch information
warunalakshitha authored Oct 11, 2024
2 parents 3abeb47 + 3c4094e commit 627c5bb
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.ballerina.runtime.api.types.Parameter;

import java.util.Optional;
import java.util.function.Supplier;

/**
* When this class is used as the first argument of an interop method, Ballerina will inject an instance of
Expand All @@ -47,9 +48,9 @@ public abstract class Environment {
/**
* Yield the current execution and run some operation so other non isolated functions can run in asynchronously.
*
* @param runnable operation to be executed.
* @param supplier operation to be executed.
*/
public abstract void yieldAndRun(Runnable runnable);
public abstract Object yieldAndRun(Supplier<Object> supplier);

/**
* Gets an instance of Ballerina runtime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.ballerina.runtime.internal.scheduling.Strand;

import java.util.Optional;
import java.util.function.Supplier;

/**
* When {@link Environment} is used as the first argument of an interop method, Ballerina will inject an instance
Expand Down Expand Up @@ -69,10 +70,10 @@ public Parameter[] getFunctionPathParameters() {
}

@Override
public void yieldAndRun(Runnable runnable) {
public Object yieldAndRun(Supplier<Object> supplier) {
try {
strand.yield();
runnable.run();
return supplier.get();
} finally {
strand.resume();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public BLockStore() {
*/
@SuppressWarnings("unused")
public void lock(Strand strand, String lockName) {
strand.yield();
getLockFromMap(lockName).lock();
strand.acquiredLockCount++;
strand.resume();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
public class AsyncUtils {

public static Object handleNonIsolatedStrand(Strand strand, Supplier<?> resultSupplier) {
// This check required for non strand Threads.
boolean runnable = strand.isRunnable();
if (runnable) {
strand.yield();

}
Object result = resultSupplier.get();
if (runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public static Strand getStrand() {
if (strand != null) {
return strand;
}
return daemonStrand;
if (daemonStrand == null) {
return null;
}
return new Strand(null, null, daemonStrand.scheduler, daemonStrand, false, null, null);
}
public Object call(Module module, String functionName, Strand parentStrand, Object... args) {
boolean runnable = parentStrand.isRunnable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public void resume() {
checkStrandCancelled();
if (!this.isIsolated && !scheduler.globalNonIsolatedLock.isHeldByCurrentThread()) {
this.scheduler.globalNonIsolatedLock.lock();

}
}

Expand All @@ -103,7 +102,7 @@ public void yield() {
}

public void done() {
if (!isIsolated && scheduler.globalNonIsolatedLock.isHeldByCurrentThread()) {
if (!this.isIsolated && scheduler.globalNonIsolatedLock.isHeldByCurrentThread()) {
scheduler.globalNonIsolatedLock.unlock();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static void sleep(Environment env, BDecimal delaySeconds) {
env.yieldAndRun(() -> {
try {
Thread.sleep(delay);
return null;
} catch (InterruptedException e) {
throw ErrorCreator.createError(StringUtils.fromString("error occurred during sleep"), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ public void testProfilerExecutionWithKillSignal() throws BallerinaTestException
bMainInstance.waitForLeechers(List.of(beforeExecleechers), 20000);
addLogLeechers(afterExecleechers, serverInfoLogReader);
Thread.sleep(5000);
long processId = process.pid();
Runtime.getRuntime().exec("kill -SIGINT " + processId);
ProcessHandle profilerHandle = process.children().findFirst().get().children().findFirst().get();
long profileId = profilerHandle.pid();
long balProcessID = profilerHandle.children().findFirst().get().pid();
Runtime.getRuntime().exec("kill -SIGINT " + balProcessID);
Runtime.getRuntime().exec("kill -SIGINT " + profileId);
bMainInstance.waitForLeechers(List.of(afterExecleechers), 20000);
process.waitFor();
} catch (InterruptedException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void sleep(Environment env, long delayMillis) {
env.yieldAndRun(() -> {
try {
Thread.sleep(delayMillis);
return null;
} catch (InterruptedException e) {
throw ErrorCreator.createError(e);
}
Expand Down

0 comments on commit 627c5bb

Please sign in to comment.