Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket test cases #1168

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions 2-advanced/dubbo-samples-triple-websocket/case-configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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
#
# http://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.

services:
nacos:
image: nacos/nacos-server:${nacos-server.version:2.0.0}
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- NACOS_AUTH_ENABLE=true
- JVM_XMS=512m
- JVM_XMX=512m
- JVM_XMN=256m

tri-websocket-server:
type: app
basedir: .
mainClass: org.apache.dubbo.tri.websocket.demo.WebSocketDemoApplication
systemProps:
- nacos.address=nacos
- nacos.port=8848
waitPortsBeforeRun:
- nacos:8848
checkPorts:
- 50052
checkLog: "Current Spring Boot Application is await..."
depends_on:
- nacos

test:
type: test
basedir: .
tests:
- "**/*Test.class"
systemProps:
- nacos.address=nacos
- nacos.port=8848
- dubbo.address=tri-websocket-server
waitPortsBeforeRun:
- nacos:8848
- tri-websocket-server:50052
depends_on:
- nacos
- tri-websocket-server
25 changes: 25 additions & 0 deletions 2-advanced/dubbo-samples-triple-websocket/case-versions.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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
#
# http://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.
#


# Supported component versions of the test case

# Spring app
dubbo.version=3.3.*
spring.version=4.*, 5.*
java.version= [>= 8]
140 changes: 140 additions & 0 deletions 2-advanced/dubbo-samples-triple-websocket/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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

http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>23</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-samples-triple-websocket</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<dubbo.version>3.3.1-SNAPSHOT</dubbo.version>
<spring-boot.version>3.3.3</spring-boot.version>
<java-websocket.version>1.5.6</java-websocket.version>
<jarkata.version>6.1.0</jarkata.version>
<tomcat.version>10.1.28</tomcat.version>
<junit.version>4.13.2</junit.version>

<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-3-autoconfigure</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jarkata.version}</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>${java-websocket.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.dubbo.tri.websocket.demo;

import org.apache.dubbo.common.stream.StreamObserver;

public interface DemoService {

String sayHello(String name);

String sayHelloError(String name);

void greetServerStream(String request, StreamObserver<String> response);

void greetServerStreamError(String request, StreamObserver<String> response);

void greetServerStreamDirectError(String request, StreamObserver<String> response);

StreamObserver<String> greetBiStream(StreamObserver<String> response);

StreamObserver<String> greetBiStreamError(StreamObserver<String> response);

StreamObserver<String> greetBiStreamDirectError(StreamObserver<String> response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.dubbo.tri.websocket.demo;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@DubboService
public class DemoServiceImpl implements DemoService {

private static final Logger LOGGER = LoggerFactory.getLogger(DemoServiceImpl.class);

@Override
public String sayHello(String name) {
return "Hello, " + name;
}

@Override
public String sayHelloError(String name) {
throw new RuntimeException("test error: " + name);
}

@Override
public void greetServerStream(String request, StreamObserver<String> response) {
for (int i = 0; i < 10; i++) {
response.onNext("Hello, " + request + i);
}
response.onCompleted();
}

@Override
public void greetServerStreamError(String request, StreamObserver<String> response) {
response.onError(new RuntimeException("test error: " + request));
}

@Override
public void greetServerStreamDirectError(String request, StreamObserver<String> response) {
throw new RuntimeException("test direct error: " + request);
}

@Override
public StreamObserver<String> greetBiStream(StreamObserver<String> response) {
return new StreamObserver<String>() {
@Override
public void onNext(String data) {
LOGGER.info(data);
response.onNext("Hello, " + data);
}

@Override
public void onError(Throwable throwable) {
LOGGER.error("GreetBiStream on error", throwable);
}

@Override
public void onCompleted() {
LOGGER.info("onCompleted");
response.onCompleted();
}
};
}

@Override
public StreamObserver<String> greetBiStreamError(StreamObserver<String> response) {
return new StreamObserver<String>() {
@Override
public void onNext(String data) {
response.onError(new RuntimeException("test error: " + data));
}

@Override
public void onError(Throwable throwable) {
LOGGER.error("GreetBiStream on error", throwable);
}

@Override
public void onCompleted() {
LOGGER.info("onCompleted");
response.onCompleted();
}
};
}

@Override
public StreamObserver<String> greetBiStreamDirectError(StreamObserver<String> response) {
return new StreamObserver<String>() {
@Override
public void onNext(String data) {
throw new RuntimeException("test direct error: " + data);
}

@Override
public void onError(Throwable throwable) {
LOGGER.error("GreetBiStream on error", throwable);
}

@Override
public void onCompleted() {
LOGGER.info("onCompleted");
response.onCompleted();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.dubbo.tri.websocket.demo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class WebSocketDemoApplication {

public static void main(String[] args) {
SpringApplication.run(WebSocketDemoApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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
#
# http://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.
server:
port: 8080
dubbo:
application:
name: dubbo-springboot-triple-websocket
qos-port: 22222
registry:
address: nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
protocol:
name: tri
port: 50052
triple:
websocket:
enabled: true
servlet:
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.dubbo.tri.websocket.demo.test;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class HelloClient extends WebSocketClient {

private static final Logger LOGGER = LoggerFactory.getLogger(HelloClient.class);

private final List<String> responses = new ArrayList<>();

private int closeCode;

private String closeMessage;

public HelloClient(URI serverURI) {
super(serverURI);
}

@Override
public void onOpen(ServerHandshake handshakedata) {
LOGGER.info("new connection opened:{}", handshakedata);
}

@Override
public void onMessage(String message) {}

@Override
public void onMessage(ByteBuffer message) {
String response = new String(message.array());
responses.add(response);
}

@Override
public void onClose(int code, String reason, boolean remote) {
LOGGER.info("closed with exit code {}, additional info: {}, remote close:{}", code, reason, remote);
closeCode = code;
closeMessage = reason;
}

@Override
public void onError(Exception e) {
LOGGER.error("WebSocket on error", e);
}

public List<String> getResponses() {
return responses;
}

public int getCloseCode() {
return closeCode;
}

public String getCloseMessage() {
return closeMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.dubbo.tri.websocket.demo.test;

import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
public class WebSocketWithNettyTest {

private final String nettyAddress = "localhost:50052";

@Test
public void testHelloWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/sayHello"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(1, responses.size());
Assertions.assertEquals("\"Hello, {\\\"world\\\":1}\"", responses.get(0));
helloClient.close();
}

@Test
public void testHelloErrorWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/sayHelloError"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test error: {\"world\":1}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testServerStreamWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetServerStream"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(10, responses.size());
for (int i = 0; i < 10; i++) {
Assertions.assertEquals("\"Hello, {\\\"world\\\":1}" + i + "\"", responses.get(i));
}
helloClient.close();
}

@Test
public void testServerStreamErrorWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetServerStreamError"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test error: {\"world\":1}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testServerStreamDirectErrorWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI("ws://" + nettyAddress
+ "/org.apache.dubbo.tri.websocket.demo.DemoService/greetServerStreamDirectError"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test direct error: {\"world\":1}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testBiStreamWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetBiStream"));
helloClient.connectBlocking();
for (int i = 0; i < 10; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(10, responses.size());
for (int i = 0; i < 10; i++) {
Assertions.assertEquals("\"Hello, {\\\"world\\\":" + i + "}\"", responses.get(i));
}

for (int i = 10; i < 20; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
Assertions.assertEquals(20, responses.size());
for (int i = 10; i < 20; i++) {
Assertions.assertEquals("\"Hello, {\\\"world\\\":" + i + "}\"", responses.get(i));
}

helloClient.close();
}

@Test
public void testBiStreamErrorWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetBiStreamError"));
helloClient.connectBlocking();
for (int i = 0; i < 10; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test error: {\"world\":0}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testBiStreamDirectErrorWithNetty() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + nettyAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetBiStreamDirectError"));
helloClient.connectBlocking();
for (int i = 0; i < 10; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test direct error: {\"world\":0}", helloClient.getCloseMessage());
helloClient.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.dubbo.tri.websocket.demo.test;

import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
public class WebSocketWithTomcatTest {

private final String tomcatAddress = "localhost:8080";

@Test
public void testHelloWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/sayHello"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(1, responses.size());
Assertions.assertEquals("\"Hello, {\\\"world\\\":1}\"", responses.get(0));
helloClient.close();
}

@Test
public void testHelloErrorWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/sayHelloError"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test error: {\"world\":1}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testServerStreamWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetServerStream"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(10, responses.size());
for (int i = 0; i < 10; i++) {
Assertions.assertEquals("\"Hello, {\\\"world\\\":1}" + i + "\"", responses.get(i));
}
helloClient.close();
}

@Test
public void testServerStreamErrorWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetServerStreamError"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test error: {\"world\":1}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testServerStreamDirectErrorWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI("ws://" + tomcatAddress
+ "/org.apache.dubbo.tri.websocket.demo.DemoService/greetServerStreamDirectError"));
helloClient.connectBlocking();
helloClient.send("{\"world\": 1}");
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test direct error: {\"world\":1}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testBiStreamWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(
new URI("ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetBiStream"));
helloClient.connectBlocking();
for (int i = 0; i < 10; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(10, responses.size());
for (int i = 0; i < 10; i++) {
Assertions.assertEquals("\"Hello, {\\\"world\\\":" + i + "}\"", responses.get(i));
}

for (int i = 10; i < 20; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
Assertions.assertEquals(20, responses.size());
for (int i = 10; i < 20; i++) {
Assertions.assertEquals("\"Hello, {\\\"world\\\":" + i + "}\"", responses.get(i));
}

helloClient.close();
}

@Test
public void testBiStreamErrorWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetBiStreamError"));
helloClient.connectBlocking();
for (int i = 0; i < 10; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test error: {\"world\":0}", helloClient.getCloseMessage());
helloClient.close();
}

@Test
public void testBiStreamDirectErrorWithTomcat() throws URISyntaxException, InterruptedException {
HelloClient helloClient = new HelloClient(new URI(
"ws://" + tomcatAddress + "/org.apache.dubbo.tri.websocket.demo.DemoService/greetBiStreamDirectError"));
helloClient.connectBlocking();
for (int i = 0; i < 10; i++) {
helloClient.send("{\"world\": " + i + "}");
}
TimeUnit.SECONDS.sleep(1);
List<String> responses = helloClient.getResponses();
Assertions.assertEquals(0, responses.size());
Assertions.assertEquals(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code(), helloClient.getCloseCode());
Assertions.assertEquals("test direct error: {\"world\":0}", helloClient.getCloseMessage());
helloClient.close();
}
}
1 change: 1 addition & 0 deletions 2-advanced/pom.xml
Original file line number Diff line number Diff line change
@@ -71,5 +71,6 @@
<module>dubbo-samples-triple-servlet</module>
<module>dubbo-samples-gateway</module>
<module>dubbo-samples-multiple-protocols</module>
<module>dubbo-samples-triple-websocket</module>
</modules>
</project>