From 33b73f2e2e3e328b8db3c40db5d09c5f3aeb8460 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 20 Nov 2018 15:46:49 +0100 Subject: [PATCH] =?UTF-8?q?promise=20=E2=80=94=20consolidate=20error=20han?= =?UTF-8?q?dling=20for=20blocking=20get?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concurrency/InternalPromiseUtil.java | 11 ++++------- .../util/concurrency/AsyncPromiseTest2.kt | 11 ++++++++--- .../src/org/jetbrains/concurrency/AsyncPromise.kt | 15 +++++++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/platform/core-api/src/org/jetbrains/concurrency/InternalPromiseUtil.java b/platform/core-api/src/org/jetbrains/concurrency/InternalPromiseUtil.java index 963c640a50ef..3f871f8258c5 100644 --- a/platform/core-api/src/org/jetbrains/concurrency/InternalPromiseUtil.java +++ b/platform/core-api/src/org/jetbrains/concurrency/InternalPromiseUtil.java @@ -2,11 +2,12 @@ package org.jetbrains.concurrency; import com.intellij.openapi.util.NotNullLazyValue; -import com.intellij.util.ExceptionUtil; +import com.intellij.util.ExceptionUtilRt; import com.intellij.util.ThreeState; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -99,7 +100,7 @@ public class InternalPromiseUtil { return null; } - ExceptionUtil.rethrowUnchecked(error); + ExceptionUtilRt.rethrowUnchecked(error); if (error instanceof ExecutionException) { throw ((ExecutionException)error); } @@ -115,11 +116,7 @@ public class InternalPromiseUtil { if (o == null || getClass() != o.getClass()) return false; PromiseValue value = (PromiseValue)o; - - if (result != null ? !result.equals(value.result) : value.result != null) return false; - if (error != null ? !error.equals(value.error) : value.error != null) return false; - - return true; + return Objects.equals(result, value.result) && Objects.equals(error, value.error); } @Override diff --git a/platform/platform-tests/testSrc/com/intellij/util/concurrency/AsyncPromiseTest2.kt b/platform/platform-tests/testSrc/com/intellij/util/concurrency/AsyncPromiseTest2.kt index d91715521c14..a88e7bba0c39 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/concurrency/AsyncPromiseTest2.kt +++ b/platform/platform-tests/testSrc/com/intellij/util/concurrency/AsyncPromiseTest2.kt @@ -21,7 +21,10 @@ private fun isMessageError(exception: Exception): Boolean { } private fun log(message: String) { - if (PRINT) println(message) + @Suppress("ConstantConditionIf") + if (PRINT) { + println(message) + } } private fun promise(state: AsyncPromiseTest2.State, `when`: When): AsyncPromise { @@ -140,10 +143,12 @@ class AsyncPromiseTest2 { fun testRejectBeforeHandlerSet() { val promise = promise(State.REJECT, When.BEFORE) try { - assert(null == promise.blockingGet(100)) + assertThat(promise.blockingGet(100)).isNull() } catch (exception: Exception) { - if (!isMessageError(exception)) throw exception + if (!isMessageError(exception)) { + throw exception + } } } diff --git a/platform/projectModel-api/src/org/jetbrains/concurrency/AsyncPromise.kt b/platform/projectModel-api/src/org/jetbrains/concurrency/AsyncPromise.kt index d258a0114658..d70632c5e94f 100644 --- a/platform/projectModel-api/src/org/jetbrains/concurrency/AsyncPromise.kt +++ b/platform/projectModel-api/src/org/jetbrains/concurrency/AsyncPromise.kt @@ -23,11 +23,14 @@ open class AsyncPromise : CancellablePromise, InternalPromiseUtil.Completa override fun isDone() = f.isDone override fun get() = nullizeCancelled { f.get() } - override fun get(timeout: Long, unit: TimeUnit?) = nullizeCancelled { f.get(timeout, unit) } + override fun get(timeout: Long, unit: TimeUnit) = nullizeCancelled { f.get(timeout, unit) } + + // because of the contract: get() should return null for canceled promise + private inline fun nullizeCancelled(value: () -> T?): T? { + if (isCancelled) { + return null + } - // because of the unorthodox contract: get() should return null for canceled promise - private fun nullizeCancelled(value: () -> T?): T? { - if (isCancelled) return null return try { value() } @@ -85,6 +88,10 @@ open class AsyncPromise : CancellablePromise, InternalPromiseUtil.Completa return get(timeout.toLong(), timeUnit) } catch (e: ExecutionException) { + if (e.cause === InternalPromiseUtil.OBSOLETE_ERROR) { + return null + } + ExceptionUtilRt.rethrowUnchecked(e.cause) throw e }