最近需要写一个 mock 工具,其中需要对一系列的动作进行编排和模拟,在使用 Java 的 CompletableFuture 时,发现缺少一下方法,自己 wrap 了下提供几个拓展方法,用法如下
CompletableFutureEx
.runAsync(() -> print("one"))
.thenRunAsync(() -> print("two"))
.thenRunWithRetry(500, i -> {
print("three: retry " + i);
return i > 5;
})
.thenWaitMills(1000)
.thenRunAsync(() -> print("four"))
.thenWaitSignal(signal)
.thenRunAsync(() -> print("five"));
完整代码在: https://gist.github.com/haomega/cfae14f47054e36d7c3ae4ac3e7cf90b
还有更好的方法或工具么,目前 cancel 貌似不太方便,不能简单的 cancel 掉一个完整的链路 如果是其他语言( golang )应该怎么写