Skip to content

Commit

Permalink
feature: 增加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
BadKid90s committed Dec 28, 2024
1 parent ede7389 commit 30ea4fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions command-engine/src/test/java/com/sove/command/CommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand All @@ -31,22 +33,28 @@ public void commandTest() {

}


@Test
public void sshjExecutorTest() {
SshProperties properties = new SshProperties("192.168.1.109", 22, "test", "123456");
SshProperties properties = new SshProperties("192.168.1.109", 22, "root", "123456");
SshjExecutor sshExecutor = new SshjExecutor(properties);
ResultParser<String> parser = ResultParserBuilder.build();

Integer sshNum = this.getSshNum(sshExecutor);

// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(100);
ExecutorService executor = Executors.newFixedThreadPool(10);

long start = System.currentTimeMillis();

List<Integer> list = new CopyOnWriteArrayList<>();
// 提交多个任务
for (int i = 0; i < 10000; i++) {
final int threadId = i;
executor.submit(() -> {
String command = "echo Hello from thread " + threadId;
String msg = sshExecutor.exec(() -> command, parser);
System.out.printf("resultMsg: " + msg);
Integer msg = this.getSshNum(sshExecutor);
System.out.println("resultMsg: " + msg + "threadId :" + threadId);

list.add(msg);
});
}

Expand All @@ -61,7 +69,19 @@ public void sshjExecutorTest() {
executor.shutdownNow(); // 当前线程被中断,强制关闭
Thread.currentThread().interrupt(); // 恢复中断状态
}
System.out.println("success! ");
long end = System.currentTimeMillis() - start;

Integer maxSshNum = list.stream()
.max(Integer::compareTo)
.orElse(0); // 如果列表为空,返回 null;
System.out.printf("耗时:%sms, 开始ssh客户端数量:%s, 最大ssh客户端数量:%s%n", end, sshNum, maxSshNum);
}

private Integer getSshNum(SshjExecutor sshExecutor) {
String command = "lsof -i :22 | wc -l";
return sshExecutor.exec(() -> command, (str ->
Integer.valueOf(str.replace("\n", ""))
));
}

}
2 changes: 1 addition & 1 deletion command-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sove.cloud</groupId>
<groupId>com.sove</groupId>
<artifactId>shell-command</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Expand Down

0 comments on commit 30ea4fc

Please sign in to comment.