=str Fix CompletionStage#toCompletableFuture may throw UnsupportedOperationException.

This commit is contained in:
kerr 2022-11-08 11:44:34 +08:00
parent 99cc4e794d
commit e8a1c3f148

View file

@ -21,6 +21,7 @@ import pekko.stream.impl.Stages.DefaultAttributes
import pekko.stream.stage.{ GraphStage, GraphStageLogic, OutHandler }
import java.util.Optional
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import scala.concurrent.Future
import scala.util.{ Failure, Success, Try }
@ -123,20 +124,21 @@ import scala.util.{ Failure, Success, Try }
}
def onPull(): Unit = {
val future = f.apply(state).toCompletableFuture
if (future.isDone && !future.isCompletedExceptionally) {
handle(future.getNow(null))
} else {
future.handle((r, ex) => {
if (ex != null) {
asyncHandler(Failure(ex))
} else {
asyncHandler(Success(r))
}
null
})
f.apply(state) match {
case cf: CompletableFuture[Optional[Pair[S, E]] @unchecked] if cf.isDone && !cf.isCompletedExceptionally =>
handle(cf.join())
case future =>
future.handle((r, ex) => {
if (ex != null) {
asyncHandler(Failure(ex))
} else {
asyncHandler(Success(r))
}
null
})
}
}
setHandler(out, this)
}
}