Add websocket-undertow smoke test

Closes gh-73
This commit is contained in:
Andy Wilkinson
2022-09-13 15:54:44 +01:00
parent a54be44ecc
commit ea0ac3b2e5
10 changed files with 228 additions and 0 deletions

View File

@@ -82,6 +82,7 @@ groups:
- websocket
- websocket-jetty
- websocket-stomp
- websocket-undertow
- name: integration
smoke_tests:
- integration

View File

@@ -115,4 +115,5 @@ include "webmvc-tomcat-tls"
include "websocket"
include "websocket-jetty"
include "websocket-stomp"
include "websocket-undertow"

View File

@@ -0,0 +1 @@
Tests WebSocket support with Undertow.

View File

@@ -0,0 +1,26 @@
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.aot.smoke-test'
id 'org.graalvm.buildtools.native'
}
dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation(project(":aot-smoke-test-third-party-hints"))
implementation("org.springframework.boot:spring-boot-starter-websocket")
implementation("org.springframework.boot:spring-boot-starter-undertow")
modules {
module("org.springframework.boot:spring-boot-starter-tomcat") {
replacedBy("org.springframework.boot:spring-boot-starter-undertow", "Use Undertow instead of Tomcat")
}
}
testImplementation("org.springframework.boot:spring-boot-starter-test")
aotSmokeTestImplementation(project(":aot-smoke-test-support"))
aotSmokeTestImplementation("org.awaitility:awaitility:4.2.0")
}
aotSmokeTest {
webApplication = true
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.websocket.jetty;
import java.time.Duration;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
import org.springframework.aot.smoketest.support.junit.AotSmokeTest;
import static org.assertj.core.api.Assertions.assertThat;
@AotSmokeTest
class WebSocketJettyApplicationAotTests {
@Test
void clientShouldSendMessage(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10))
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("Client: Sent 'Hello Websocket'"));
}
@Test
void serverShouldReceiveMessage(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10))
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("Server: Received 'Hello Websocket'"));
}
@Test
void serverShouldReply(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10))
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("Server: Sent 'Hello Websocket'"));
}
@Test
void clientShouldReceiveReply(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10))
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("Client: Received 'Hello Websocket'"));
}
}

View File

@@ -0,0 +1,49 @@
package com.example.websocket.undertow;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
class CLR implements CommandLineRunner {
private final WebSocketClient webSocketClient;
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
CLR(WebSocketClient webSocketClient, ServletWebServerApplicationContext servletWebServerApplicationContext) {
this.webSocketClient = webSocketClient;
this.servletWebServerApplicationContext = servletWebServerApplicationContext;
}
@Override
public void run(String... args) throws Exception {
URI uri = URI.create("ws://localhost:%d/echo".formatted(getServerPort()));
WebSocketSession session = this.webSocketClient.execute(new MyWebsocketHandler(), null, uri).get(5,
TimeUnit.SECONDS);
TextMessage message = new TextMessage("Hello Websocket");
session.sendMessage(message);
System.out.printf("Client: Sent '%s'%n", message.getPayload());
}
private int getServerPort() {
return this.servletWebServerApplicationContext.getWebServer().getPort();
}
private static class MyWebsocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
System.out.printf("Client: Received '%s'%n", message.getPayload());
}
}
}

View File

@@ -0,0 +1,33 @@
package com.example.websocket.undertow;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class EchoWebSocketHandler extends TextWebSocketHandler {
public EchoWebSocketHandler() {
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
System.out.printf("Opened new session in instance %s%n", this);
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.printf("Server: Received '%s' in %s%n", message.getPayload(), this);
session.sendMessage(message);
System.out.printf("Server: Sent '%s' in %s%n", message.getPayload(), this);
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
System.out.printf("Closing connection in %s%n", this, exception);
session.close(CloseStatus.SERVER_ERROR);
}
}

View File

@@ -0,0 +1,31 @@
package com.example.websocket.undertow;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
private final EchoWebSocketHandler echoWebSocketHandler;
public WebSocketConfig(EchoWebSocketHandler echoWebSocketHandler) {
this.echoWebSocketHandler = echoWebSocketHandler;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(this.echoWebSocketHandler, "/echo");
}
@Bean
public WebSocketClient webSocketClient() {
return new StandardWebSocketClient();
}
}

View File

@@ -0,0 +1,16 @@
package com.example.websocket.undertow;
import org.springframework.aot.smoketest.thirdpartyhints.UndertowRuntimeHints;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportRuntimeHints;
@SpringBootApplication
@ImportRuntimeHints(UndertowRuntimeHints.class)
public class WebSocketUndertowApplication {
public static void main(String[] args) {
SpringApplication.run(WebSocketUndertowApplication.class, args);
}
}

View File

@@ -0,0 +1,14 @@
package com.example.websocket.undertow;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WebSocketUndertowApplicationTests {
@Test
void contextLoads() {
}
}