Skip to content

Commit

Permalink
Simplify java assertions and add messages to all (async-profiler#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
krk authored Oct 17, 2024
1 parent 5a90a82 commit 62dca46
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 27 deletions.
87 changes: 62 additions & 25 deletions test/one/profiler/test/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,86 @@

package one.profiler.test;

import java.util.function.BiPredicate;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Assert {
private static final Logger log = Logger.getLogger(Assert.class.getName());
enum Comparison {
GT(">", (a, b) -> a > b),
GTE(">=", (a, b) -> a >= b),
LT("<", (a, b) -> a < b),
LTE("<=", (a, b) -> a <= b),
EQ("==", Double::equals),
NE("!=", (a, b) -> !Double.isNaN(a) && !Double.isNaN(b) && !a.equals(b));

public final String operator;
public final BiPredicate<Double, Double> comparator;

public static void isGreater(double value, double threshold) {
isGreater(value, threshold, null);
Comparison(String operator, BiPredicate<Double, Double> comparator) {
this.operator = operator;
this.comparator = comparator;
}
}

public static void isGreater(double value, double threshold, String message) {
boolean asserted = value <= threshold;
log.log(Level.FINE, "isGreater (asserted: " + asserted + ") " + (message == null ? "" : message) + ": " + value + " > " + threshold);
private static final Logger log = Logger.getLogger(Assert.class.getName());

private static void assertComparison(Comparison comparison, double left, double right, @SuppressWarnings("unused") String message) {
boolean asserted = !comparison.comparator.test(left, right);

// message parameter will be part of the source code line.
String assertionMessage = String.format("%s %s %s\n%s", left, comparison.operator, right, SourceCode.tryGet(2));
log.log(Level.FINE, String.format("isAsserted %s: %s", asserted, assertionMessage));
if (asserted) {
throw new AssertionError(
"Expected " + value + " > " + threshold + (message != null ? (": " + message) : ""));
throw new AssertionError("Expected " + assertionMessage);
}
}

public static void isGreaterOrEqual(double value, double threshold) {
if (value < threshold) {
throw new AssertionError("Expected " + value + " >= " + threshold);
}
public static void isEqual(double left, double right) {
assertComparison(Comparison.EQ, left, right, null);
}

public static void isLess(double value, double threshold) {
isLess(value, threshold, null);
public static void isEqual(double left, double right, String message) {
assertComparison(Comparison.EQ, left, right, message);
}

public static void isLess(double value, double threshold, String message) {
boolean asserted = value >= threshold;
log.log(Level.FINE, "isLess (asserted: " + asserted + ")" + (message == null ? "" : message) + ": " + value + " < " + threshold);
public static void isNotEqual(double left, double right) {
assertComparison(Comparison.NE, left, right, null);
}

if (asserted) {
throw new AssertionError(
"Expected " + value + " < " + threshold + (message != null ? (": " + message) : ""));
}
public static void isNotEqual(double left, double right, String message) {
assertComparison(Comparison.NE, left, right, message);
}

public static void isLessOrEqual(double value, double threshold) {
if (value > threshold) {
throw new AssertionError("Expected " + value + " <= " + threshold);
}
public static void isGreater(double left, double right) {
assertComparison(Comparison.GT, left, right, null);
}

public static void isGreater(double left, double right, String message) {
assertComparison(Comparison.GT, left, right, message);
}

public static void isGreaterOrEqual(double left, double right) {
assertComparison(Comparison.GTE, left, right, null);
}

public static void isGreaterOrEqual(double left, double right, String message) {
assertComparison(Comparison.GTE, left, right, message);
}

public static void isLess(double left, double right) {
assertComparison(Comparison.LT, left, right, null);
}

public static void isLess(double left, double right, String message) {
assertComparison(Comparison.LT, left, right, message);
}

public static void isLessOrEqual(double left, double right) {
assertComparison(Comparison.LTE, left, right, null);
}

public static void isLessOrEqual(double left, double right, String message) {
assertComparison(Comparison.LTE, left, right, message);
}
}
57 changes: 57 additions & 0 deletions test/one/profiler/test/SourceCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/

package one.profiler.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SourceCode {
public static String tryGet(int ignoreFrames) {
return tryGet(new Exception(), ignoreFrames + 1);
}

public static String tryGet(Throwable e, int ignoreFrames) {
StackTraceElement[] stackTrace = e.getStackTrace();

if (stackTrace.length > ignoreFrames) {
StackTraceElement element = stackTrace[ignoreFrames];
String className = element.getClassName();
String filePath = getFilePath(className);

return getSourceCodeAt(filePath, element.getLineNumber());
}
return "No stack trace available";
}

private static String getFilePath(String className) {
int dollar = className.lastIndexOf('$');
if (dollar >= 0) {
className = className.substring(0, dollar);
}
return "test/" + className.replace('.', '/') + ".java";
}

private static String getSourceCodeAt(String filePath, int lineNumber) {
String result = "\t> " + filePath + ":" + lineNumber;

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
int currentLine = 1;

while ((line = reader.readLine()) != null) {
if (currentLine == lineNumber) {
return result + "\n\t> " + line.trim();
}
currentLine++;
}
} catch (IOException ex) {
return "Error reading source file: " + ex.getMessage();
}

return result;
}
}
3 changes: 1 addition & 2 deletions test/test/lock/LockTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class LockTests {

@Test(mainClass = DatagramTest.class, debugNonSafepoints = true) // Fails on Alpine
@Test(mainClass = DatagramTest.class, debugNonSafepoints = true)
public void datagramSocketLock(TestProcess p) throws Exception {
Output out = p.profile("-e cpu -d 3 -o collapsed --cstack dwarf");
assert out.ratio("(PlatformEvent::.ark|PlatformEvent::.npark)") > 0.1
Expand All @@ -26,7 +26,6 @@ public void datagramSocketLock(TestProcess p) throws Exception {
@Test(mainClass = RaceToLock.class, inputs = "1000000", output = true)
public void raceToLocks(TestProcess p) throws Exception {
int interval = Integer.parseInt(p.inputs()[0]);

Output out = p.profile("--lock " + interval + " --threads -o collapsed");
Output stdout = p.readFile(TestProcess.STDOUT);

Expand Down

0 comments on commit 62dca46

Please sign in to comment.