Issue #3884 - Updating tests and fixing merge

+ Test cases have been updated based on PR review
+ Fixing merge from `jetty-9.4.x` that caused a duplicate
  JettyListenerEventDriver.onContinuationFrame() method

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-08-05 12:52:20 -05:00
parent 1ee771e991
commit b8f29630ed
5 changed files with 33 additions and 29 deletions

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.tests; package org.eclipse.jetty.websocket.tests;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -51,6 +52,7 @@ public class CloseTrackingEndpoint extends WebSocketAdapter
public CountDownLatch errorLatch = new CountDownLatch(1); public CountDownLatch errorLatch = new CountDownLatch(1);
public LinkedBlockingQueue<String> messageQueue = new LinkedBlockingQueue<>(); public LinkedBlockingQueue<String> messageQueue = new LinkedBlockingQueue<>();
public LinkedBlockingQueue<ByteBuffer> binaryMessageQueue = new LinkedBlockingQueue<>();
public AtomicReference<Throwable> error = new AtomicReference<>(); public AtomicReference<Throwable> error = new AtomicReference<>();
public void assertReceivedCloseEvent(int clientTimeoutMs, Matcher<Integer> statusCodeMatcher) public void assertReceivedCloseEvent(int clientTimeoutMs, Matcher<Integer> statusCodeMatcher)
@ -110,6 +112,13 @@ public class CloseTrackingEndpoint extends WebSocketAdapter
messageQueue.offer(message); messageQueue.offer(message);
} }
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len)
{
LOG.debug("onWebSocketBinary({},offset,len)", payload, offset, len);
binaryMessageQueue.offer(ByteBuffer.wrap(payload, offset, len));
}
public EndPoint getEndPoint() throws Exception public EndPoint getEndPoint() throws Exception
{ {
Session session = getSession(); Session session = getSession();

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.tests; package org.eclipse.jetty.websocket.tests;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
@ -33,4 +34,10 @@ public class EchoSocket
{ {
session.getRemote().sendString(msg); session.getRemote().sendString(msg);
} }
@OnWebSocketMessage
public void onBinaryMessage(Session session, byte[] data, int offset, int len) throws IOException
{
session.getRemote().sendBytes(ByteBuffer.wrap(data, offset, len));
}
} }

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.tests.client;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.EnumSet; import java.util.EnumSet;
@ -232,13 +233,23 @@ public class WebSocketClientTest
remote.sendPartialString(" ", false); remote.sendPartialString(" ", false);
remote.sendPartialString("World", true); remote.sendPartialString("World", true);
remote.sendPartialBytes(BufferUtil.toBuffer("It's a big enough umbrella, "), false); String[] parts = {
remote.sendPartialBytes(BufferUtil.toBuffer("but it's always me that "), false); "The difference between the right word ",
remote.sendPartialBytes(BufferUtil.toBuffer("ends up getting wet."), true); "and the almost right word is the difference ",
"between lightning and a lightning bug."
};
remote.sendPartialBytes(BufferUtil.toBuffer(parts[0]), false);
remote.sendPartialBytes(BufferUtil.toBuffer(parts[1]), false);
remote.sendPartialBytes(BufferUtil.toBuffer(parts[2]), true);
// wait for response from server // wait for response from server
String received = cliSock.messageQueue.poll(5, TimeUnit.SECONDS); String received = cliSock.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Message", received, containsString("Hello World")); assertThat("Message", received, containsString("Hello World"));
ByteBuffer bufReceived = cliSock.binaryMessageQueue.poll(5, TimeUnit.SECONDS);
received = BufferUtil.toUTF8String(bufReceived.slice());
assertThat("Message", received, containsString(parts[0] + parts[1] + parts[2]));
} }
} }

View File

@ -40,9 +40,6 @@ import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.OpCode; import org.eclipse.jetty.websocket.common.OpCode;
import org.eclipse.jetty.websocket.common.WebSocketSession; import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet; import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.eclipse.jetty.websocket.tests.CloseTrackingEndpoint; import org.eclipse.jetty.websocket.tests.CloseTrackingEndpoint;
@ -57,7 +54,7 @@ import static org.hamcrest.Matchers.is;
public class FrameListenerTest public class FrameListenerTest
{ {
private Server server; private Server server;
private FrameCreator frameCreator; private FrameEndpoint serverEndpoint;
private WebSocketClient client; private WebSocketClient client;
@BeforeEach @BeforeEach
@ -77,8 +74,8 @@ public class FrameListenerTest
public void configure(WebSocketServletFactory factory) public void configure(WebSocketServletFactory factory)
{ {
factory.getPolicy().setIdleTimeout(SECONDS.toMillis(2)); factory.getPolicy().setIdleTimeout(SECONDS.toMillis(2));
frameCreator = new FrameCreator(); serverEndpoint = new FrameEndpoint();
factory.setCreator(frameCreator); factory.setCreator((req, resp) -> serverEndpoint);
} }
}); });
context.addServlet(closeEndpoint, "/ws"); context.addServlet(closeEndpoint, "/ws");
@ -138,8 +135,6 @@ public class FrameListenerTest
clientRemote.sendPartialString(" ", false); clientRemote.sendPartialString(" ", false);
clientRemote.sendPartialString("world", true); clientRemote.sendPartialString("world", true);
FrameEndpoint serverEndpoint = frameCreator.frameEndpoint;
String event = serverEndpoint.frameEvents.poll(5, SECONDS); String event = serverEndpoint.frameEvents.poll(5, SECONDS);
assertThat("Event", event, is("FRAME[TEXT,fin=false,payload=hello,len=5]")); assertThat("Event", event, is("FRAME[TEXT,fin=false,payload=hello,len=5]"));
event = serverEndpoint.frameEvents.poll(5, SECONDS); event = serverEndpoint.frameEvents.poll(5, SECONDS);
@ -153,18 +148,6 @@ public class FrameListenerTest
} }
} }
public static class FrameCreator implements WebSocketCreator
{
public FrameEndpoint frameEndpoint;
@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
{
frameEndpoint = new FrameEndpoint();
return frameEndpoint;
}
}
public static class FrameEndpoint implements WebSocketFrameListener public static class FrameEndpoint implements WebSocketFrameListener
{ {
public Session session; public Session session;

View File

@ -250,12 +250,6 @@ public class JettyListenerEventDriver extends AbstractEventDriver
return; return;
} }
super.onContinuationFrame(buffer, fin);
}
@Override
public void onContinuationFrame(ByteBuffer buffer, boolean fin) throws IOException
{
if (listener instanceof WebSocketListener) if (listener instanceof WebSocketListener)
{ {
super.onContinuationFrame(buffer, fin); super.onContinuationFrame(buffer, fin);