Polish reactive WebSocket integration tests
This commit is contained in:
@@ -46,16 +46,15 @@ import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgra
|
|||||||
import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy;
|
import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for WebSocket integration tests involving a server-side
|
* Base class for WebSocket integration tests.
|
||||||
* {@code WebSocketHandler}. Sub-classes to return a Spring configuration class
|
* Sub-classes must implement {@link #getWebConfigClass()} to return Spring
|
||||||
* via {@link #getWebConfigClass()} containing a SimpleUrlHandlerMapping with
|
* config class with handler mappings to {@code WebSocketHandler}'s.
|
||||||
* pattern-to-WebSocketHandler mappings.
|
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||||
public abstract class AbstractWebSocketHandlerIntegrationTests {
|
public abstract class AbstractWebSocketIntegrationTests {
|
||||||
|
|
||||||
protected int port;
|
protected int port;
|
||||||
|
|
||||||
@@ -17,7 +17,6 @@ package org.springframework.web.reactive.socket.server;
|
|||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
@@ -38,11 +37,12 @@ import org.springframework.web.reactive.socket.WebSocketSession;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic WebSocket integration tests.
|
* Integration tests with server-side {@link WebSocketHandler}s.
|
||||||
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||||
public class BasicWebSocketHandlerIntegrationTests extends AbstractWebSocketHandlerIntegrationTests {
|
public class ServerWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,26 +53,22 @@ public class BasicWebSocketHandlerIntegrationTests extends AbstractWebSocketHand
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void echo() throws Exception {
|
public void echo() throws Exception {
|
||||||
Observable<String> messages = Observable.range(1, 10).map(i -> "Interval " + i);
|
int count = 100;
|
||||||
List<String> actual = HttpClient.newClient("localhost", this.port)
|
Observable<String> input = Observable.range(1, count).map(index -> "msg-" + index);
|
||||||
|
Observable<String> output = HttpClient.newClient("localhost", this.port)
|
||||||
.createGet("/echo")
|
.createGet("/echo")
|
||||||
.requestWebSocketUpgrade()
|
.requestWebSocketUpgrade()
|
||||||
.flatMap(WebSocketResponse::getWebSocketConnection)
|
.flatMap(WebSocketResponse::getWebSocketConnection)
|
||||||
.flatMap(conn -> conn.write(messages
|
.flatMap(conn -> conn
|
||||||
.map(TextWebSocketFrame::new)
|
.write(input.map(TextWebSocketFrame::new)).cast(WebSocketFrame.class)
|
||||||
.cast(WebSocketFrame.class))
|
|
||||||
.cast(WebSocketFrame.class)
|
|
||||||
.mergeWith(conn.getInput())
|
.mergeWith(conn.getInput())
|
||||||
)
|
.take(count)
|
||||||
.take(10)
|
.map(frame -> {
|
||||||
.map(frame -> {
|
String text = frame.content().toString(StandardCharsets.UTF_8);
|
||||||
String text = frame.content().toString(StandardCharsets.UTF_8);
|
frame.release();
|
||||||
frame.release();
|
return text;
|
||||||
return text;
|
}));
|
||||||
})
|
assertEquals(input.toList().toBlocking().first(), output.toList().toBlocking().first());
|
||||||
.toList().toBlocking().first();
|
|
||||||
List<String> expected = messages.toList().toBlocking().first();
|
|
||||||
assertEquals(expected, actual);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user