fix: 单元测试

This commit is contained in:
2024-07-11 09:33:48 +08:00
parent 600076bc59
commit 3b4fdf64dd

View File

@@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.*;
/** /**
* @Author: lyd * @Author: lyd
@@ -50,10 +51,32 @@ public class ApplicationTest {
} }
@org.testng.annotations.Test @Test
void demo1() { void demo1() {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000); // 假设这是一个耗时的操作
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted", e);
}
return "Result";
});
try {
String result = future.get(2, TimeUnit.SECONDS); // 设置超时时间为2秒
System.out.println(result);
} catch (CompletionException e) {
Throwable cause = e.getCause();
if (cause instanceof TimeoutException) {
System.out.println("Operation timed out");
} else {
throw e; // rethrow other exceptions
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
} }