Skip to content

Commit

Permalink
Refactored decode(Memento) and embedded Memento in the decoder instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor17 committed Apr 13, 2022
1 parent 178cb65 commit 345cfe2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.apache.maven.surefire.api.event.Event;
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento;
import org.apache.maven.surefire.extensions.CloseableDaemonThread;
import org.apache.maven.surefire.extensions.EventHandler;
import org.apache.maven.surefire.extensions.util.CountdownCloseable;
Expand Down Expand Up @@ -67,10 +66,9 @@ public void run()
CountdownCloseable c = countdownCloseable;
EventDecoder eventDecoder = decoder )
{
Memento memento = eventDecoder.new Memento();
do
{
Event event = eventDecoder.decode( memento );
Event event = eventDecoder.decode();
if ( event != null && !disabled )
{
eventHandler.handleEvent( event );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public class EventDecoder extends AbstractStreamDecoder<Event, ForkedProcessEven

private final OutputStream debugSink;

private Memento memento;

public EventDecoder( @Nonnull ReadableByteChannel channel,
@Nonnull ForkNodeArguments arguments )
{
Expand All @@ -148,8 +150,15 @@ public EventDecoder( @Nonnull ReadableByteChannel channel,
}

@Override
public Event decode( @Nonnull Memento memento ) throws IOException
public Event decode() throws IOException
{
if ( memento == null )
{
// do not create memento in constructor because the constructor is called in another thread
// memento is the thread confinement object
memento = new Memento();
}

try
{
ForkedProcessEventType eventType = readMessageType( memento );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ protected AbstractStreamDecoder( @Nonnull ReadableByteChannel channel,
logger = arguments.getConsoleLogger();
}

public abstract M decode( @Nonnull Memento memento ) throws MalformedChannelException, IOException;
/**
* Decoding and returns a message {@code M} and waiting, if necessary, for the next
* message received from the channel.
*
* @return message {@code M}, or null if could not decode a message due to a frame error
* @throws MalformedChannelException the channel error
* @throws IOException stream I/O exception
*/
public abstract M decode() throws MalformedChannelException, IOException;

@Nonnull
protected abstract byte[] getEncodedMagicNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

/**
*
* No supported message type.
*/
public class MalformedChannelException extends Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.maven.surefire.api.booter.Command;
import org.apache.maven.surefire.api.booter.MasterProcessChannelDecoder;
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento;
import org.apache.maven.surefire.api.stream.MalformedChannelException;
import org.apache.maven.surefire.booter.stream.CommandDecoder;

Expand All @@ -40,7 +39,6 @@
public class CommandChannelDecoder implements MasterProcessChannelDecoder
{
private final CommandDecoder decoder;
private Memento memento;

public CommandChannelDecoder( @Nonnull ReadableByteChannel channel,
@Nonnull ForkNodeArguments arguments )
Expand All @@ -53,18 +51,11 @@ public CommandChannelDecoder( @Nonnull ReadableByteChannel channel,
@SuppressWarnings( "checkstyle:innerassignment" )
public Command decode() throws IOException
{
if ( memento == null )
{
// do not create memento in constructor because the constructor is called in another thread
// memento is the thread confinement object
memento = decoder.new Memento();
}

do
{
try
{
Command command = decoder.decode( memento );
Command command = decoder.decode();
if ( command != null )
{
return command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class CommandDecoder extends AbstractStreamDecoder<Command, MasterProcess

private final ForkNodeArguments arguments;
private final OutputStream debugSink;
private Memento memento;

public CommandDecoder( @Nonnull ReadableByteChannel channel,
@Nonnull ForkNodeArguments arguments )
Expand All @@ -80,8 +81,15 @@ public CommandDecoder( @Nonnull ReadableByteChannel channel,
}

@Override
public Command decode( @Nonnull Memento memento ) throws IOException, MalformedChannelException
public Command decode() throws IOException, MalformedChannelException
{
if ( memento == null )
{
// do not create memento in constructor because the constructor is called in another thread
// memento is the thread confinement object
memento = new Memento();
}

try
{
MasterProcessCommand commandType = readMessageType( memento );
Expand Down

0 comments on commit 345cfe2

Please sign in to comment.