[*] map body

This commit is contained in:
acgist
2023-02-12 15:33:35 +08:00
parent e8ae344d11
commit 61c93b2614
34 changed files with 223 additions and 189 deletions

View File

@@ -2,7 +2,7 @@ taoyao:
media:
media-server-list:
- name: media-local-a
enabled: true
enabled: false
host: 192.168.1.110
port: 4443
schema: wss

View File

@@ -9,8 +9,8 @@ server:
key-password: 123456
tomcat:
thread:
max: 128
min-spare: 4
max: 256
min-spare: 8
remoteip:
host-header: X-Forwarded-Host
port-header: X-Forwarded-Port

View File

@@ -32,7 +32,12 @@ public @interface CostedTest {
/**
* @return 超时时间
*/
long timeout() default 1000;
long timeout() default 1000L;
/**
* @return 等待资源释放时间
*/
long waitRelease() default 0L;
/**
* @return 超时时间单位

View File

@@ -49,10 +49,15 @@ public class CostedTestTestExecutionListener implements TestExecutionListener {
});
}
countDownLatch.await(timeout, timeUnit);
executor.shutdown();
}
final long zTime = System.currentTimeMillis();
final long costed = zTime - aTime;
log.info("多线程测试消耗时间:{}", costed);
final long waitRelease = costedTest.waitRelease();
if(waitRelease > 0) {
Thread.sleep(waitRelease);
}
}
}

View File

@@ -1,12 +1,16 @@
package com.acgist.taoyao.signal;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.net.http.WebSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.junit.jupiter.api.Test;
import com.acgist.taoyao.annotation.CostedTest;
import com.acgist.taoyao.annotation.TaoyaoTest;
import com.acgist.taoyao.main.TaoyaoApplication;
@@ -16,34 +20,41 @@ import lombok.extern.slf4j.Slf4j;
@TaoyaoTest(classes = TaoyaoApplication.class)
class SignalTest {
/**
* 防止GC
*/
private List<WebSocket> list = new ArrayList<>();
@Test
void testSignal() throws InterruptedException {
final WebSocket clientA = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientA");
final WebSocket clientB = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientB");
clientA.sendText("""
{"header":{"pid":1000,"v":"1.0.0","id":"1","sn":"clientA"},"body":{}}
{"header":{"signal":"client::heartbeat","v":"1.0.0","id":"1"},"body":{}}
""", true).join();
assertNotNull(clientA);
assertNotNull(clientB);
}
@Test
@CostedTest(thread = 10, count = 100, waitRelease = 5000L)
void testThread() throws InterruptedException {
final int total = 1000;
final int total = 100;
final CountDownLatch count = new CountDownLatch(total);
final WebSocket clientA = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientA", count);
final long aTime = System.currentTimeMillis();
for (int index = 0; index < total; index++) {
clientA.sendText("""
{"header":{"pid":2999,"v":"1.0.0","id":"1","sn":"clientA"},"body":{}}
{"header":{"signal":"client::status","v":"1.0.0","id":"1"},"body":{}}
""", true).join();
}
this.list.add(clientA);
// final ExecutorService executor = Executors.newFixedThreadPool(10);
// for (int index = 0; index < total; index++) {
// executor.execute(() -> {
// synchronized (clientA) {
// clientA.sendText("""
// {"header":{"pid":2999,"v":"1.0.0","id":"1","sn":"clientA"},"body":{}}
// {"header":{"signal":"client::status","v":"1.0.0","id":"1"},"body":{}}
// """, true).join();
// }
// });
@@ -51,8 +62,20 @@ class SignalTest {
count.await();
final long zTime = System.currentTimeMillis();
log.info("执行时间:{}", zTime - aTime);
Thread.sleep(1000);
log.info("当前连接数量:{}", this.list.size());
assertNotNull(clientA);
}
@Test
void testMax() throws InterruptedException {
final int size = 1024;
final CountDownLatch count = new CountDownLatch(size);
for (int index = 0; index < size; index++) {
final WebSocket clientA = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientA", count);
assertNotNull(clientA);
assertTrue(!(clientA.isInputClosed() || clientA.isOutputClosed()));
}
count.await();
}
}

View File

@@ -37,7 +37,7 @@ public class WebSocketClient {
@Override
public void onOpen(WebSocket webSocket) {
webSocket.sendText(String.format("""
{"header":{"pid":2000,"v":"1.0.0","id":"1","sn":"%s"},"body":{"username":"taoyao","password":"taoyao","ip":"127.0.0.1"}}
{"header":{"signal":"client::register","v":"1.0.0","id":"1"},"body":{"username":"taoyao","password":"taoyao","ip":"127.0.0.1","sn":"%s"}}
""", sn), true);
Listener.super.onOpen(webSocket);
}

View File

@@ -1,29 +0,0 @@
package com.acgist.taoyao.signal.protocol;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.acgist.taoyao.annotation.TaoyaoTest;
import com.acgist.taoyao.boot.model.Message;
import com.acgist.taoyao.main.TaoyaoApplication;
import com.acgist.taoyao.signal.protocol.platform.PlatformScriptProtocol;
@TaoyaoTest(classes = TaoyaoApplication.class)
class PlatformScriptProtocolTest {
@Autowired
private PlatformScriptProtocol platformScriptProtocol;
@Test
void testScript() {
assertDoesNotThrow(() -> {
this.platformScriptProtocol.execute("taoyao", Map.of("script", "netstat -ano"), null, Message.success());
Thread.sleep(1000);
});
}
}

View File

@@ -1,27 +0,0 @@
package com.acgist.taoyao.signal.protocol;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.acgist.taoyao.annotation.TaoyaoTest;
import com.acgist.taoyao.boot.model.Message;
import com.acgist.taoyao.main.TaoyaoApplication;
import com.acgist.taoyao.signal.protocol.platform.PlatformShutdownProtocol;
@TaoyaoTest(classes = TaoyaoApplication.class)
class PlatformShutdownProtocolTest {
@Autowired
private PlatformShutdownProtocol platformShutdownProtocol;
@Test
void testShutdown() {
assertDoesNotThrow(() -> {
this.platformShutdownProtocol.execute("taoyao", null, Message.success());
Thread.sleep(1000);
});
}
}