promise — consolidate error handling for blocking get

This commit is contained in:
Vladimir Krivosheev
2018-11-20 17:14:42 +01:00
parent 5319bc3bfb
commit 33b73f2e2e
3 changed files with 23 additions and 14 deletions
@@ -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
@@ -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<String> {
@@ -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
}
}
}
@@ -23,11 +23,14 @@ open class AsyncPromise<T> : CancellablePromise<T>, 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<T> : CancellablePromise<T>, InternalPromiseUtil.Completa
return get(timeout.toLong(), timeUnit)
}
catch (e: ExecutionException) {
if (e.cause === InternalPromiseUtil.OBSOLETE_ERROR) {
return null
}
ExceptionUtilRt.rethrowUnchecked(e.cause)
throw e
}