Issue #3428 - improve testing & warn if no decoder accepts message

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-05-28 10:21:28 +10:00
parent 9475662451
commit 1690f69f36
5 changed files with 114 additions and 43 deletions

View File

@ -26,14 +26,6 @@ import org.eclipse.jetty.websocket.util.messages.MessageSink;
public class JavaxWebSocketMessageMetadata
{
public static enum Type
{
TEXT,
BINARY,
TEXT_STREAM,
BINARY_STREAM
}
private MethodHandle methodHandle;
private Class<? extends MessageSink> sinkClass;
private List<RegisteredDecoder> registeredDecoders;
@ -91,18 +83,6 @@ public class JavaxWebSocketMessageMetadata
this.sinkClass = sinkClass;
}
public RegisteredDecoder getRegisteredDecoder()
{
if (registeredDecoders == null || registeredDecoders.isEmpty())
return null;
return registeredDecoders.get(0);
}
public void setRegisteredDecoder(RegisteredDecoder registeredDecoder)
{
this.registeredDecoders = List.of(registeredDecoder);
}
public List<RegisteredDecoder> getRegisteredDecoders()
{
return registeredDecoders;

View File

@ -72,5 +72,7 @@ public class DecodedBinaryMessageSink<T> extends AbstractDecodedMessageSink.Basi
}
}
}
_logger.warn("Message lost, willDecode() has returned false for all decoders in the decoder list.");
}
}

View File

@ -48,6 +48,7 @@ public class DecodedTextMessageSink<T> extends AbstractDecodedMessageSink.Basic<
return new StringMessageSink(_coreSession, methodHandle);
}
@SuppressWarnings("Duplicates")
public void onMessage(String wholeMessage)
{
for (Decoder.Text<T> decoder : _decoders)
@ -70,5 +71,7 @@ public class DecodedTextMessageSink<T> extends AbstractDecodedMessageSink.Basic<
}
}
}
_logger.warn("Message lost, willDecode() has returned false for all decoders in the decoder list.");
}
}

View File

@ -49,6 +49,7 @@ public class DecodedTextStreamMessageSink<T> extends AbstractDecodedMessageSink.
return new ReaderMessageSink(_coreSession, methodHandle);
}
@SuppressWarnings("Duplicates")
public void onStreamStart(Reader reader)
{
try

View File

@ -16,13 +16,13 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests;
package org.eclipse.jetty.websocket.javax.tests.coders;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ -30,8 +30,12 @@ import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.eclipse.jetty.websocket.javax.common.decoders.AbstractDecoder;
import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.eclipse.jetty.websocket.javax.tests.WSURI;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
@ -73,7 +77,7 @@ public class DecoderListTest
client.stop();
}
public static Stream<Arguments> getArguments()
public static Stream<Arguments> getTextArguments()
{
return Stream.of(
Arguments.of("=DecodeEquals", "DecodeEquals="),
@ -83,9 +87,20 @@ public class DecoderListTest
);
}
public static Stream<Arguments> getBinaryArguments()
{
return Stream.of(
Arguments.of("=DecodeEquals", "DecodeEquals="),
Arguments.of("+DecodePlus", "DecodePlus+"),
Arguments.of("-DecodeMinus", "DecodeMinus-"),
// No decoder accepts this message and we have no default decoder for this type, so we get no response.
Arguments.of("DecodeNoMatch", null)
);
}
@ParameterizedTest
@MethodSource("getArguments")
public void testDecoderList(String request, String expected) throws Exception
@MethodSource("getTextArguments")
public void testTextDecoderList(String request, String expected) throws Exception
{
EventSocket clientEndpoint = new EventSocket();
Session session = client.connectToServer(clientEndpoint, serverUri);
@ -94,7 +109,19 @@ public class DecoderListTest
assertThat(response, is(expected));
}
@ServerEndpoint(value = "/", decoders = {EqualsDecoder.class, PlusDecoder.class, MinusDecoder.class})
@ParameterizedTest
@MethodSource("getBinaryArguments")
public void testBinaryDecoderList(String request, String expected) throws Exception
{
EventSocket clientEndpoint = new EventSocket();
Session session = client.connectToServer(clientEndpoint, serverUri);
session.getBasicRemote().sendBinary(BufferUtil.toBuffer(request));
ByteBuffer response = clientEndpoint.binaryMessages.poll(3, TimeUnit.SECONDS);
assertThat(BufferUtil.toString(response), is(expected));
}
@ServerEndpoint(value = "/", decoders = {EqualsTextDecoder.class, PlusTextDecoder.class, MinusTextDecoder.class,
EqualsBinaryDecoder.class, PlusBinaryDecoder.class, MinusBinaryDecoder.class})
public static class DecoderListEndpoint
{
@OnMessage
@ -102,33 +129,101 @@ public class DecoderListTest
{
return message;
}
@OnMessage
public ByteBuffer echo(ByteBufferWrapper message)
{
return message.getByteBuffer();
}
}
public static class EqualsDecoder extends PrefixStringDecoder
public static class ByteBufferWrapper
{
public EqualsDecoder()
private final ByteBuffer byteBuffer;
public ByteBufferWrapper(ByteBuffer byteBuffer)
{
this.byteBuffer = byteBuffer;
}
public ByteBuffer getByteBuffer()
{
return byteBuffer;
}
}
public static class EqualsBinaryDecoder extends ByteBufferWrapperDecoder
{
public EqualsBinaryDecoder()
{
super("=");
}
}
public static class PlusDecoder extends PrefixStringDecoder
public static class PlusBinaryDecoder extends ByteBufferWrapperDecoder
{
public PlusDecoder()
public PlusBinaryDecoder()
{
super("+");
}
}
public static class MinusDecoder extends PrefixStringDecoder
public static class MinusBinaryDecoder extends ByteBufferWrapperDecoder
{
public MinusDecoder()
public MinusBinaryDecoder()
{
super("-");
}
}
public static class PrefixStringDecoder implements Decoder.Text<String>
public static class EqualsTextDecoder extends PrefixStringDecoder
{
public EqualsTextDecoder()
{
super("=");
}
}
public static class PlusTextDecoder extends PrefixStringDecoder
{
public PlusTextDecoder()
{
super("+");
}
}
public static class MinusTextDecoder extends PrefixStringDecoder
{
public MinusTextDecoder()
{
super("-");
}
}
public static class ByteBufferWrapperDecoder extends AbstractDecoder implements Decoder.Binary<ByteBufferWrapper>
{
private final String prefix;
public ByteBufferWrapperDecoder(String prefix)
{
this.prefix = prefix;
}
@Override
public ByteBufferWrapper decode(ByteBuffer bytes)
{
String s = BufferUtil.toString(bytes).substring(prefix.length()) + prefix;
return new ByteBufferWrapper(BufferUtil.toBuffer(s));
}
@Override
public boolean willDecode(ByteBuffer bytes)
{
return BufferUtil.toString(bytes).startsWith(prefix);
}
}
public static class PrefixStringDecoder extends AbstractDecoder implements Decoder.Text<String>
{
private final String prefix;
@ -148,15 +243,5 @@ public class DecoderListTest
{
return s.startsWith(prefix);
}
@Override
public void init(EndpointConfig config)
{
}
@Override
public void destroy()
{
}
}
}