Skip to content

Commit b5f65fe

Browse files
committed
Refactored decode(Memento) and embedded Memento in the decoder instance
1 parent 178cb65 commit b5f65fe

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.apache.maven.surefire.api.event.Event;
2323
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
24-
import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento;
2524
import org.apache.maven.surefire.extensions.CloseableDaemonThread;
2625
import org.apache.maven.surefire.extensions.EventHandler;
2726
import org.apache.maven.surefire.extensions.util.CountdownCloseable;
@@ -67,10 +66,9 @@ public void run()
6766
CountdownCloseable c = countdownCloseable;
6867
EventDecoder eventDecoder = decoder )
6968
{
70-
Memento memento = eventDecoder.new Memento();
7169
do
7270
{
73-
Event event = eventDecoder.decode( memento );
71+
Event event = eventDecoder.decode();
7472
if ( event != null && !disabled )
7573
{
7674
eventHandler.handleEvent( event );

maven-surefire-common/src/main/java/org/apache/maven/surefire/stream/EventDecoder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public class EventDecoder extends AbstractStreamDecoder<Event, ForkedProcessEven
140140

141141
private final OutputStream debugSink;
142142

143+
private Memento memento;
144+
143145
public EventDecoder( @Nonnull ReadableByteChannel channel,
144146
@Nonnull ForkNodeArguments arguments )
145147
{
@@ -148,8 +150,15 @@ public EventDecoder( @Nonnull ReadableByteChannel channel,
148150
}
149151

150152
@Override
151-
public Event decode( @Nonnull Memento memento ) throws IOException
153+
public Event decode() throws IOException
152154
{
155+
if ( memento == null )
156+
{
157+
// do not create memento in constructor because the constructor is called in another thread
158+
// memento is the thread confinement object
159+
memento = new Memento();
160+
}
161+
153162
try
154163
{
155164
ForkedProcessEventType eventType = readMessageType( memento );

surefire-api/src/main/java/org/apache/maven/surefire/api/stream/AbstractStreamDecoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ protected AbstractStreamDecoder( @Nonnull ReadableByteChannel channel,
8989
logger = arguments.getConsoleLogger();
9090
}
9191

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

94102
@Nonnull
95103
protected abstract byte[] getEncodedMagicNumber();

surefire-api/src/main/java/org/apache/maven/surefire/api/stream/MalformedChannelException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
/**
23-
*
23+
* No supported message type.
2424
*/
2525
public class MalformedChannelException extends Exception
2626
{

surefire-api/src/test/java/org/apache/maven/surefire/api/stream/AbstractStreamDecoderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ protected Mock( @Nonnull ReadableByteChannel channel, @Nonnull ForkNodeArguments
664664
}
665665

666666
@Override
667-
public Event decode( @Nonnull Memento memento ) throws MalformedChannelException
667+
public Event decode() throws MalformedChannelException
668668
{
669669
throw new MalformedChannelException();
670670
}

surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/CommandChannelDecoder.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.maven.surefire.api.booter.Command;
2323
import org.apache.maven.surefire.api.booter.MasterProcessChannelDecoder;
2424
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
25-
import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento;
2625
import org.apache.maven.surefire.api.stream.MalformedChannelException;
2726
import org.apache.maven.surefire.booter.stream.CommandDecoder;
2827

@@ -40,7 +39,6 @@
4039
public class CommandChannelDecoder implements MasterProcessChannelDecoder
4140
{
4241
private final CommandDecoder decoder;
43-
private Memento memento;
4442

4543
public CommandChannelDecoder( @Nonnull ReadableByteChannel channel,
4644
@Nonnull ForkNodeArguments arguments )
@@ -53,18 +51,11 @@ public CommandChannelDecoder( @Nonnull ReadableByteChannel channel,
5351
@SuppressWarnings( "checkstyle:innerassignment" )
5452
public Command decode() throws IOException
5553
{
56-
if ( memento == null )
57-
{
58-
// do not create memento in constructor because the constructor is called in another thread
59-
// memento is the thread confinement object
60-
memento = decoder.new Memento();
61-
}
62-
6354
do
6455
{
6556
try
6657
{
67-
Command command = decoder.decode( memento );
58+
Command command = decoder.decode();
6859
if ( command != null )
6960
{
7061
return command;

surefire-booter/src/main/java/org/apache/maven/surefire/booter/stream/CommandDecoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class CommandDecoder extends AbstractStreamDecoder<Command, MasterProcess
7070

7171
private final ForkNodeArguments arguments;
7272
private final OutputStream debugSink;
73+
private Memento memento;
7374

7475
public CommandDecoder( @Nonnull ReadableByteChannel channel,
7576
@Nonnull ForkNodeArguments arguments )
@@ -80,8 +81,15 @@ public CommandDecoder( @Nonnull ReadableByteChannel channel,
8081
}
8182

8283
@Override
83-
public Command decode( @Nonnull Memento memento ) throws IOException, MalformedChannelException
84+
public Command decode() throws IOException, MalformedChannelException
8485
{
86+
if ( memento == null )
87+
{
88+
// do not create memento in constructor because the constructor is called in another thread
89+
// memento is the thread confinement object
90+
memento = new Memento();
91+
}
92+
8593
try
8694
{
8795
MasterProcessCommand commandType = readMessageType( memento );

0 commit comments

Comments
 (0)