use WebSocketClient instead of NetworkFuzzer in the primitive echo tests

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-11-26 15:37:36 +11:00
parent 629e2d69a4
commit e4502865b7
2 changed files with 48 additions and 34 deletions

View File

@ -19,19 +19,21 @@
package org.eclipse.jetty.websocket.javax.tests.server; package org.eclipse.jetty.websocket.javax.tests.server;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.websocket.OnError; import javax.websocket.OnError;
import javax.websocket.OnMessage; import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.toolchain.test.Hex; import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.websocket.core.CloseStatus; import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.eclipse.jetty.websocket.javax.tests.Fuzzer;
import org.eclipse.jetty.websocket.javax.tests.LocalServer; import org.eclipse.jetty.websocket.javax.tests.LocalServer;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
@ -41,6 +43,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test various {@link javax.websocket.Decoder.Binary Decoder.Binary} / {@link javax.websocket.Encoder.Binary Encoder.Binary} echo behavior of Java Primitives * Test various {@link javax.websocket.Decoder.Binary Decoder.Binary} / {@link javax.websocket.Encoder.Binary Encoder.Binary} echo behavior of Java Primitives
*/ */
@ -86,7 +92,7 @@ public class PrimitivesBinaryEchoTest
private static void addCase(List<Arguments> data, Class<?> endpointClass, String sendHex, String expectHex) private static void addCase(List<Arguments> data, Class<?> endpointClass, String sendHex, String expectHex)
{ {
data.add(Arguments.of(endpointClass.getSimpleName(), endpointClass, sendHex, expectHex)); data.add(Arguments.of(endpointClass, sendHex, expectHex));
} }
public static Stream<Arguments> data() public static Stream<Arguments> data()
@ -105,6 +111,7 @@ public class PrimitivesBinaryEchoTest
} }
private static LocalServer server; private static LocalServer server;
private static JavaxWebSocketClientContainer client;
@BeforeAll @BeforeAll
public static void startServer() throws Exception public static void startServer() throws Exception
@ -113,32 +120,32 @@ public class PrimitivesBinaryEchoTest
server.start(); server.start();
server.getServerContainer().addEndpoint(ByteBufferEchoSocket.class); server.getServerContainer().addEndpoint(ByteBufferEchoSocket.class);
server.getServerContainer().addEndpoint(ByteArrayEchoSocket.class); server.getServerContainer().addEndpoint(ByteArrayEchoSocket.class);
client = new JavaxWebSocketClientContainer();
client.start();
} }
@AfterAll @AfterAll
public static void stopServer() throws Exception public static void stopServer() throws Exception
{ {
client.stop();
server.stop(); server.stop();
} }
@ParameterizedTest(name = "{0}: {2}") @ParameterizedTest(name = "{0}: {2}")
@MethodSource("data") @MethodSource("data")
public void testPrimitiveEcho(String endpointClassname, Class<?> endpointClass, String sendHex, String expectHex) throws Exception public void testPrimitiveEcho(Class<?> endpointClass, String sendHex, String expectHex) throws Exception
{ {
String requestPath = endpointClass.getAnnotation(ServerEndpoint.class).value(); String requestPath = endpointClass.getAnnotation(ServerEndpoint.class).value();
EventSocket clientSocket = new EventSocket();
List<Frame> send = new ArrayList<>(); URI uri = server.getWsUri().resolve(requestPath);
send.add(new Frame(OpCode.BINARY).setPayload(Hex.asByteBuffer(sendHex))); try (Session session = client.connectToServer(clientSocket, uri))
send.add(CloseStatus.toFrame(CloseStatus.NORMAL));
List<Frame> expect = new ArrayList<>();
expect.add(new Frame(OpCode.BINARY).setPayload(Hex.asByteBuffer(expectHex)));
expect.add(CloseStatus.toFrame(CloseStatus.NORMAL));
try (Fuzzer session = server.newNetworkFuzzer(requestPath))
{ {
session.sendBulk(send); session.getBasicRemote().sendBinary(Hex.asByteBuffer(sendHex));
session.expect(expect); assertThat(clientSocket.binaryMessages.poll(5, TimeUnit.SECONDS), is(Hex.asByteBuffer(expectHex)));
} }
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientSocket.closeReason.getCloseCode().getCode(), is(CloseStatus.NORMAL));
} }
} }

View File

@ -19,18 +19,20 @@
package org.eclipse.jetty.websocket.javax.tests.server; package org.eclipse.jetty.websocket.javax.tests.server;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.websocket.OnError; import javax.websocket.OnError;
import javax.websocket.OnMessage; import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerContainer; import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.websocket.core.CloseStatus; import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.eclipse.jetty.websocket.javax.tests.Fuzzer;
import org.eclipse.jetty.websocket.javax.tests.LocalServer; import org.eclipse.jetty.websocket.javax.tests.LocalServer;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
@ -40,6 +42,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test various {@link javax.websocket.Decoder.Text Decoder.Text} / {@link javax.websocket.Encoder.Text Encoder.Text} echo behavior of Java Primitives * Test various {@link javax.websocket.Decoder.Text Decoder.Text} / {@link javax.websocket.Encoder.Text Encoder.Text} echo behavior of Java Primitives
*/ */
@ -228,7 +234,7 @@ public class PrimitivesTextEchoTest
private static void addCase(List<Arguments> data, Class<?> endpointClass, String sendText, String expectText) private static void addCase(List<Arguments> data, Class<?> endpointClass, String sendText, String expectText)
{ {
data.add(Arguments.of(endpointClass.getSimpleName(), endpointClass, sendText, expectText)); data.add(Arguments.of(endpointClass, sendText, expectText));
} }
public static Stream<Arguments> data() public static Stream<Arguments> data()
@ -341,6 +347,7 @@ public class PrimitivesTextEchoTest
} }
private static LocalServer server; private static LocalServer server;
private static JavaxWebSocketClientContainer client;
@BeforeAll @BeforeAll
public static void startServer() throws Exception public static void startServer() throws Exception
@ -365,32 +372,32 @@ public class PrimitivesTextEchoTest
container.addEndpoint(LongEchoSocket.class); container.addEndpoint(LongEchoSocket.class);
container.addEndpoint(LongObjEchoSocket.class); container.addEndpoint(LongObjEchoSocket.class);
container.addEndpoint(StringEchoSocket.class); container.addEndpoint(StringEchoSocket.class);
client = new JavaxWebSocketClientContainer();
client.start();
} }
@AfterAll @AfterAll
public static void stopServer() throws Exception public static void stopServer() throws Exception
{ {
client.stop();
server.stop(); server.stop();
} }
@ParameterizedTest(name = "{0}: {2}") @ParameterizedTest(name = "{0}: {2}")
@MethodSource("data") @MethodSource("data")
public void testPrimitiveEcho(String endpointClassname, Class<?> endpointClass, String sendText, String expectText) throws Exception public void testPrimitiveEcho(Class<?> endpointClass, String sendText, String expectText) throws Exception
{ {
String requestPath = endpointClass.getAnnotation(ServerEndpoint.class).value(); String requestPath = endpointClass.getAnnotation(ServerEndpoint.class).value();
EventSocket clientSocket = new EventSocket();
List<Frame> send = new ArrayList<>(); URI uri = server.getWsUri().resolve(requestPath);
send.add(new Frame(OpCode.TEXT).setPayload(sendText)); try (Session session = client.connectToServer(clientSocket, uri))
send.add(CloseStatus.toFrame(CloseStatus.NORMAL));
List<Frame> expect = new ArrayList<>();
expect.add(new Frame(OpCode.TEXT).setPayload(expectText));
expect.add(CloseStatus.toFrame(CloseStatus.NORMAL));
try (Fuzzer session = server.newNetworkFuzzer(requestPath))
{ {
session.sendBulk(send); session.getBasicRemote().sendText(sendText);
session.expect(expect); assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is(expectText));
} }
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientSocket.closeReason.getCloseCode().getCode(), is(CloseStatus.NORMAL));
} }
} }