Skip to content

Commit

Permalink
Fix JUnitXMLReporter not logging from JGroups threads
Browse files Browse the repository at this point in the history
* Added a test configuration with the STDOUT appender to be utilized.
  This way our custom reporter captures the output from the JGroups
  threads in the output file.
* Update the files to use a log extension.
* Added color to the console output.
  • Loading branch information
jabolina committed Mar 11, 2024
1 parent 7844b83 commit 66f8b3a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
21 changes: 21 additions & 0 deletions conf/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<!-- follow=true doesn't work on Windows with jline/jansi -->
<Console name="STDOUT" target="SYSTEM_OUT" follow="false">
<PatternLayout pattern="%-7d{HH:mm:ss,SSS} [%p] [%t] (%c{1.}): %m%n"/>
<!--<PatternLayout pattern="%r [%p] %c{1}: %m%n"/>-->
</Console>
</appenders>
<loggers>
<root level="${env:LOG_LEVEL:-WARN}">
<appender-ref ref="STDOUT"/>
</root>

<!-- Utilize the STDOUT appender to find the JUnitXMLReporter. -->
<!-- Additivity false, otherwise we write to the root logger. -->
<logger name="org.jgroups" level="${env:LOG_LEVEL:-WARN}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
</loggers>
</configuration>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
-Djgroups.tunnel.gossip_router_hosts=${jgroups.tunnel.gossip_router_hosts}
-Dtests.tmp.dir=${tmp.dir}
-Dlog4j.configuration=file:${conf.dir}/log4j.properties
-Dlog4j.configurationFile=${conf.dir}/log4j2.xml
-Dlog4j.configurationFile=${conf.dir}/log4j2-test.xml
-Djava.net.preferIPv4Stack=${jgroups.useIPv4}
-Djava.net.preferIPv6Addresses=${jgroups.useIPv6}
-Dorg.jgrops.test.trace=${trace}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
Expand All @@ -40,15 +41,19 @@
public class JUnitXMLReporter implements IResultListener {
protected String output_dir=null;

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_RED = "\u001B[31m";

protected static final String XML_DEF="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
protected static final String CDATA="![CDATA[";
protected static final String LT="&lt;";
protected static final String GT="&gt;";
protected static final String SYSTEM_OUT="system-out";
protected static final String SYSTEM_ERR="system-err";
protected static final String TESTS="tests.data";
protected static final String STDOUT="stdout.txt";
protected static final String STDERR="stderr.txt";
protected static final String STDOUT="stdout.log";
protected static final String STDERR="stderr.log";

protected PrintStream old_stdout=System.out;
protected PrintStream old_stderr=System.err;
Expand All @@ -70,11 +75,9 @@ public void onStart(ITestContext context) {
deleteContents(dir);

try {
System.setOut(new MyOutput(1));
System.setErr(new MyOutput(2));
}
catch(FileNotFoundException e) {
}
System.setOut(new MyOutput(stdout::get));
System.setErr(new MyOutput(stderr::get));
} catch(FileNotFoundException ignore) {}
}

/** Invoked after all test classes in this test have been run */
Expand All @@ -88,6 +91,7 @@ public void onFinish(ITestContext context) {
error(e.toString());
}
finally {
closeStreams();
System.setOut(old_stdout);
System.setErr(old_stderr);
}
Expand All @@ -104,7 +108,7 @@ public void onTestStart(ITestResult result) {
public void onTestSuccess(ITestResult tr) {
if (stdout.get() != null)
stdout.get().println("\n\n------------- SUCCESS: " + getMethodName(tr) + " -----------");
onTestCompleted(tr, "OK: ", old_stdout);
onTestCompleted(tr, ANSI_GREEN + "OK" + ANSI_RESET + ": ", old_stdout);
}


Expand All @@ -116,7 +120,7 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
public void onTestFailure(ITestResult tr) {
if (stdout.get() != null)
stdout.get().println("\n\n------------- FAILURE: " + getMethodName(tr) + " -----------");
onTestCompleted(tr, "FAIL: ",old_stderr);
onTestCompleted(tr, ANSI_RED + "FAIL" + ANSI_RESET + ": ",old_stderr);
}

/** Invoked each time a test method is skipped */
Expand All @@ -136,7 +140,6 @@ public void onConfigurationSuccess(ITestResult tr) {
if (tr.getMethod().isAfterClassConfiguration() || tr.getMethod().isAfterMethodConfiguration())
stdout.get().println("\n=======================\n");
}
closeStreams();
}

public void onConfigurationFailure(ITestResult tr) {
Expand Down Expand Up @@ -551,13 +554,12 @@ protected static class MyOutput extends PrintStream {
TMPFILE_NAME = System.getProperty("java.io.tmpdir") + "/" + "tmp.txt";
}
}
final int type;

public MyOutput(int type) throws FileNotFoundException {
private final Supplier<PrintStream> acquire;

public MyOutput(Supplier<PrintStream> acquire) throws FileNotFoundException {
super(TMPFILE_NAME); // dummy name
this.type=type;
if(type != 1 && type != 2)
throw new IllegalArgumentException("index has to be 1 or 2");
this.acquire = acquire;
}

public void write(final byte[] b) {
Expand Down Expand Up @@ -601,7 +603,7 @@ public void println(Object obj) {
}

protected synchronized void append(String x, boolean newline) {
PrintStream tmp=type == 1? stdout.get() : stderr.get();
PrintStream tmp = acquire.get();
if(tmp == null)
return;
if(newline)
Expand Down

0 comments on commit 66f8b3a

Please sign in to comment.