fix: 单元测试
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
@@ -50,10 +51,32 @@ public class ApplicationTest {
|
||||
}
|
||||
|
||||
|
||||
@org.testng.annotations.Test
|
||||
@Test
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user