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;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
import org.eclipse.jetty.websocket.javax.tests.Fuzzer;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.eclipse.jetty.websocket.javax.tests.LocalServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
@ -41,6 +43,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
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
*/
@ -86,7 +92,7 @@ public class PrimitivesBinaryEchoTest
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()
@ -105,6 +111,7 @@ public class PrimitivesBinaryEchoTest
}
private static LocalServer server;
private static JavaxWebSocketClientContainer client;
@BeforeAll
public static void startServer() throws Exception
@ -113,32 +120,32 @@ public class PrimitivesBinaryEchoTest
server.start();
server.getServerContainer().addEndpoint(ByteBufferEchoSocket.class);
server.getServerContainer().addEndpoint(ByteArrayEchoSocket.class);
client = new JavaxWebSocketClientContainer();
client.start();
}
@AfterAll
public static void stopServer() throws Exception
{
client.stop();
server.stop();
}
@ParameterizedTest(name = "{0}: {2}")
@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();
List<Frame> send = new ArrayList<>();
send.add(new Frame(OpCode.BINARY).setPayload(Hex.asByteBuffer(sendHex)));
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))
EventSocket clientSocket = new EventSocket();
URI uri = server.getWsUri().resolve(requestPath);
try (Session session = client.connectToServer(clientSocket, uri))
{
session.sendBulk(send);
session.expect(expect);
session.getBasicRemote().sendBinary(Hex.asByteBuffer(sendHex));
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;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
import org.eclipse.jetty.websocket.javax.tests.Fuzzer;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.eclipse.jetty.websocket.javax.tests.LocalServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
@ -40,6 +42,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
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
*/
@ -228,7 +234,7 @@ public class PrimitivesTextEchoTest
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()
@ -341,6 +347,7 @@ public class PrimitivesTextEchoTest
}
private static LocalServer server;
private static JavaxWebSocketClientContainer client;
@BeforeAll
public static void startServer() throws Exception
@ -365,32 +372,32 @@ public class PrimitivesTextEchoTest
container.addEndpoint(LongEchoSocket.class);
container.addEndpoint(LongObjEchoSocket.class);
container.addEndpoint(StringEchoSocket.class);
client = new JavaxWebSocketClientContainer();
client.start();
}
@AfterAll
public static void stopServer() throws Exception
{
client.stop();
server.stop();
}
@ParameterizedTest(name = "{0}: {2}")
@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();
List<Frame> send = new ArrayList<>();
send.add(new Frame(OpCode.TEXT).setPayload(sendText));
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))
EventSocket clientSocket = new EventSocket();
URI uri = server.getWsUri().resolve(requestPath);
try (Session session = client.connectToServer(clientSocket, uri))
{
session.sendBulk(send);
session.expect(expect);
session.getBasicRemote().sendText(sendText);
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is(expectText));
}
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientSocket.closeReason.getCloseCode().getCode(), is(CloseStatus.NORMAL));
}
}