Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-2025] Updated abstractions which helps associating systemProperties() with a test context #475

Merged
merged 1 commit into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -72,7 +72,7 @@ public final class ForkClient

/**
* <em>testSetStartedAt</em> is set to non-zero after received
* {@link MasterProcessChannelEncoder#testSetStarting(ReportEntry, boolean)}.
* {@link MasterProcessChannelEncoder#testSetStarting(TestSetReportEntry, boolean)}.
*/
private final AtomicLong testSetStartedAt = new AtomicLong( START_TIME_ZERO );

Expand Down
Expand Up @@ -36,7 +36,7 @@
import org.apache.maven.surefire.api.report.RunMode;
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.util.internal.ObjectUtils;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;
import org.apache.maven.surefire.booter.spi.EventChannelEncoder;
import org.apache.maven.surefire.extensions.EventHandler;
Expand Down Expand Up @@ -79,6 +79,7 @@
import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOutln;
import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newChannel;
import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.rules.ExpectedException.none;
Expand All @@ -104,48 +105,6 @@ public static class DecoderOperationsTest
@Rule
public final ExpectedException rule = none();

@Test
public void shouldHaveSystemProperty() throws Exception
{
final Stream out = Stream.newStream();
WritableBufferedByteChannel wChannel = newBufferedChannel( out );
EventChannelEncoder encoder = new EventChannelEncoder( wChannel );
Map<String, String> props = ObjectUtils.systemProps();
encoder.systemProperties( props );
wChannel.close();

ForkedProcessEventNotifier notifier = new ForkedProcessEventNotifier();
PropertyEventAssertionListener listener = new PropertyEventAssertionListener();
notifier.setSystemPropertiesListener( listener );

ReadableByteChannel channel = newChannel( new ByteArrayInputStream( out.toByteArray() ) );

EH eventHandler = new EH();
CountdownCloseable countdown = new CountdownCloseable( mock( Closeable.class ), 0 );
ConsoleLoggerMock logger = new ConsoleLoggerMock( false, false, false, false );
ForkNodeArgumentsMock arguments = new ForkNodeArgumentsMock( logger, new File( "" ) );
try ( EventConsumerThread t = new EventConsumerThread( "t", channel, eventHandler, countdown, arguments ) )
{
t.start();
for ( int i = 0; i < props.size(); i++ )
{
notifier.notifyEvent( eventHandler.pullEvent() );
}
}

assertThat( logger.error ).isEmpty();
assertThat( logger.warning ).isEmpty();
assertThat( logger.info ).isEmpty();
assertThat( logger.debug ).isEmpty();

assertThat( logger.isCalled() )
.isFalse();
assertThat( arguments.isCalled() )
.isFalse();
assertThat( listener.counter.get() )
.isEqualTo( props.size() );
}

@Test
public void shouldSendByeEvent() throws Exception
{
Expand Down Expand Up @@ -799,44 +758,6 @@ public void testStdErrStream() throws Exception
.isTrue();
}

@Test
public void shouldCountSameNumberOfSystemProperties() throws Exception
{
final Stream out = Stream.newStream();
WritableBufferedByteChannel wChannel = newBufferedChannel( out );
EventChannelEncoder encoder = new EventChannelEncoder( wChannel );
encoder.systemProperties( ObjectUtils.systemProps() );
wChannel.close();

ReadableByteChannel channel = newChannel( new ByteArrayInputStream( out.toByteArray() ) );

ForkedProcessEventNotifier notifier = new ForkedProcessEventNotifier();
PropertyEventAssertionListener listener = new PropertyEventAssertionListener();
notifier.setSystemPropertiesListener( listener );

EH eventHandler = new EH();
CountdownCloseable countdown = new CountdownCloseable( mock( Closeable.class ), 0 );
ConsoleLoggerMock logger = new ConsoleLoggerMock( false, false, false, false );
ForkNodeArgumentsMock arguments = new ForkNodeArgumentsMock( logger, new File( "" ) );
try ( EventConsumerThread t = new EventConsumerThread( "t", channel, eventHandler, countdown, arguments ) )
{
t.start();
notifier.notifyEvent( eventHandler.pullEvent() );
}

assertThat( logger.error ).isEmpty();
assertThat( logger.warning ).isEmpty();
assertThat( logger.info ).isEmpty();
assertThat( logger.debug ).isEmpty();

assertThat( logger.isCalled() )
.isFalse();
assertThat( arguments.isCalled() )
.isFalse();
assertThat( listener.called.get() )
.isTrue();
}

@Test
public void shouldHandleErrorAfterNullLine()
{
Expand Down Expand Up @@ -999,7 +920,7 @@ public void testReportEntryOperations( @FromDataPoints( "operation" ) String[] o
when( stackTraceWriter.writeTraceToString() ).thenReturn( exceptionStackTrace );
}

ReportEntry reportEntry = mock( ReportEntry.class );
TestSetReportEntry reportEntry = mock( TestSetReportEntry.class );
when( reportEntry.getElapsed() ).thenReturn( elapsed );
when( reportEntry.getGroup() ).thenReturn( "this group" );
when( reportEntry.getMessage() ).thenReturn( reportedMessage );
Expand All @@ -1009,12 +930,14 @@ public void testReportEntryOperations( @FromDataPoints( "operation" ) String[] o
when( reportEntry.getSourceName() ).thenReturn( "pkg.MyTest" );
when( reportEntry.getSourceText() ).thenReturn( "test class display name" );
when( reportEntry.getStackTraceWriter() ).thenReturn( stackTraceWriter );
when( reportEntry.getSystemProperties() ).thenReturn( systemProps() );

final Stream out = Stream.newStream();

EventChannelEncoder encoder = new EventChannelEncoder( newBufferedChannel( out ) );

EventChannelEncoder.class.getMethod( operation[0], ReportEntry.class, boolean.class )
Class<?> reportType = operation[0].startsWith( "testSet" ) ? TestSetReportEntry.class : ReportEntry.class;
EventChannelEncoder.class.getMethod( operation[0], reportType, boolean.class )
.invoke( encoder, reportEntry, trim );

ForkedProcessEventNotifier notifier = new ForkedProcessEventNotifier();
Expand Down
Expand Up @@ -68,7 +68,6 @@ public void testSetStarting( TestSetReportEntry report )
@Override
public void testSetCompleted( TestSetReportEntry report )
{
target.systemProperties( report.getSystemProperties() );
target.testSetCompleted( report, trim );
}

Expand Down
Expand Up @@ -22,8 +22,7 @@
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;

import java.util.Map;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

/**
* An abstraction for physical encoder of events.
Expand All @@ -33,49 +32,159 @@
*/
public interface MasterProcessChannelEncoder
{
/**
* @return {@code true} if the encoder's stream has got an error
*/
boolean checkError();

/**
* Called on JVM exit error.
*/
void onJvmExit();

void systemProperties( Map<String, String> sysProps );

void testSetStarting( ReportEntry reportEntry, boolean trimStackTraces );

void testSetCompleted( ReportEntry reportEntry, boolean trimStackTraces );

/**
* The test set has started.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testSetStarting( TestSetReportEntry reportEntry, boolean trimStackTraces );

/**
* The test set has finished.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testSetCompleted( TestSetReportEntry reportEntry, boolean trimStackTraces );

/**
* The test has started.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testStarting( ReportEntry reportEntry, boolean trimStackTraces );

/**
* The test has succeeded.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testSucceeded( ReportEntry reportEntry, boolean trimStackTraces );

/**
* The test has failed.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testFailed( ReportEntry reportEntry, boolean trimStackTraces );

/**
* The test is skipped.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testSkipped( ReportEntry reportEntry, boolean trimStackTraces );

/**
* The test error.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testError( ReportEntry reportEntry, boolean trimStackTraces );

/**
* The test assumption failure.
*
* @param reportEntry test set report entry
* @param trimStackTraces {@code true} if stack trace trimming
*/
void testAssumptionFailure( ReportEntry reportEntry, boolean trimStackTraces );

/**
* Test output, a line or characters.
*
* @param reportEntry std/out or std/err context
*/
void testOutput( TestOutputReportEntry reportEntry );

/**
* Info log.
*
* @param msg message of info logger
*/
void consoleInfoLog( String msg );

/**
* Error log.
*
* @param msg message of error logger
*/
void consoleErrorLog( String msg );

/**
* Error log.
*
* @param t exception
*/
void consoleErrorLog( Throwable t );

/**
* Error log.
*
* @param msg additional error message
* @param t exception
*/
void consoleErrorLog( String msg, Throwable t );

/**
* Error log.
*
* @param stackTraceWriter printable stack trace
* @param trimStackTraces {@code true} if selected trimmed stack trace to print into encoder channel/stream
*/
void consoleErrorLog( StackTraceWriter stackTraceWriter, boolean trimStackTraces );

/**
* Debug log.
*
* @param msg message of debug logger
*/
void consoleDebugLog( String msg );

/**
* Warning log.
*
* @param msg message of warning logger
*/
void consoleWarningLog( String msg );

/**
* Say BYE on exit.
* ForkBooter will consequently wait for BYE_ACK command which finally drains the (std/in) sink channel.
*/
void bye();

/**
* The provider wants to stop the progress.
*/
void stopOnNextTest();

/**
* The provider acquires a new test set to run.
*/
void acquireNextTest();

/**
* ForkedBooter tear down has failed while waiting for BYE_ACK command.
*
* @param stackTraceWriter printable stack trace
* @param trimStackTraces {@code true} if selected trimmed stack trace to print into encoder channel/stream
*/
void sendExitError( StackTraceWriter stackTraceWriter, boolean trimStackTraces );
}
Expand Up @@ -28,6 +28,7 @@
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestSetReportEntry;
import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;
import org.apache.maven.surefire.booter.stream.EventEncoder;

Expand Down Expand Up @@ -127,8 +128,7 @@ public void onJvmExit()
write( ByteBuffer.wrap( new byte[] {'\n'} ), true );
}

@Override
public void systemProperties( Map<String, String> sysProps )
private void encodeSystemProperties( Map<String, String> sysProps, RunMode rm, Long testRunId )
{
CharsetEncoder encoder = newCharsetEncoder();
ByteBuffer result = null;
Expand All @@ -150,14 +150,15 @@ public void systemProperties( Map<String, String> sysProps )
}

@Override
public void testSetStarting( ReportEntry reportEntry, boolean trimStackTraces )
public void testSetStarting( TestSetReportEntry reportEntry, boolean trimStackTraces )
{
encode( BOOTERCODE_TESTSET_STARTING, runMode, reportEntry, trimStackTraces, true );
}

@Override
public void testSetCompleted( ReportEntry reportEntry, boolean trimStackTraces )
public void testSetCompleted( TestSetReportEntry reportEntry, boolean trimStackTraces )
{
encodeSystemProperties( reportEntry.getSystemProperties(), null, null ); // todo in next commit
encode( BOOTERCODE_TESTSET_COMPLETED, runMode, reportEntry, trimStackTraces, true );
}

Expand Down