Skip to content

Commit

Permalink
Don't assert about suppressed exceptions in GraalVM (square#6441)
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse authored Nov 22, 2020
1 parent 5f2732d commit 2f361c2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
16 changes: 16 additions & 0 deletions okhttp-testing-support/src/main/kotlin/okhttp3/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ object TestUtil {
@JvmField
val UNREACHABLE_ADDRESS = InetSocketAddress("198.51.100.1", 8080)

/** See `org.graalvm.nativeimage.ImageInfo`. */
@JvmStatic
private val isGraalVmImage = System.getProperty("org.graalvm.nativeimage.imagecode") != null

@JvmStatic
fun headerEntries(vararg elements: String?): List<Header> {
return List(elements.size / 2) { Header(elements[it * 2]!!, elements[it * 2 + 1]!!) }
Expand Down Expand Up @@ -105,4 +109,16 @@ object TestUtil {
@JvmStatic
val windows: Boolean
get() = System.getProperty("os.name", "?").startsWith("Windows")

/**
* Make assertions about the suppressed exceptions on this. Prefer this over making direct calls
* so tests pass on GraalVM, where suppressed exceptions are silently discarded.
*
* https://github.com/oracle/graal/issues/3008
*/
@JvmStatic
fun Throwable.assertSuppressed(block: (List<@JvmSuppressWildcards Throwable>) -> Unit) {
if (isGraalVmImage) return
block(suppressed.toList())
}
}
10 changes: 6 additions & 4 deletions okhttp/src/test/java/okhttp3/CallKotlinTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import mockwebserver3.MockWebServer
import mockwebserver3.SocketPolicy
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.TestUtil.assertSuppressed
import okhttp3.internal.connection.RealConnection
import okhttp3.internal.connection.RealConnection.Companion.IDLE_CONNECTION_HEALTHY_NS
import okhttp3.internal.http.RecordingProxySelector
Expand Down Expand Up @@ -240,10 +241,11 @@ class CallKotlinTest(
client.newCall(request).execute()
fail("")
} catch (expected: IOException) {
assertThat(expected.suppressed).hasSize(1)
val suppressed = expected.suppressed[0]
assertThat(suppressed).isInstanceOf(IOException::class.java)
assertThat(suppressed).isNotSameAs(expected)
expected.assertSuppressed {
val suppressed = it.single()
assertThat(suppressed).isInstanceOf(IOException::class.java)
assertThat(suppressed).isNotSameAs(expected)
}
}
}
}
7 changes: 6 additions & 1 deletion okhttp/src/test/java/okhttp3/InterceptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import kotlin.Unit;
import mockwebserver3.MockResponse;
import mockwebserver3.MockWebServer;
import mockwebserver3.RecordedRequest;
Expand All @@ -45,6 +46,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static okhttp3.TestUtil.assertSuppressed;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -545,7 +547,10 @@ private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) thro
RecordedResponse recordedResponse = callback.await(server.url("/"));
assertThat(recordedResponse.failure)
.hasMessage("canceled due to java.lang.RuntimeException: boom!");
assertThat(recordedResponse.failure).hasSuppressedException(boom);
assertSuppressed(recordedResponse.failure, throwables -> {
assertThat(throwables).contains(boom);
return Unit.INSTANCE;
});
assertThat(call.isCanceled()).isTrue();

assertThat(executor.takeException()).isEqualTo(boom);
Expand Down
8 changes: 6 additions & 2 deletions okhttp/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import kotlin.Unit;
import mockwebserver3.MockResponse;
import mockwebserver3.MockWebServer;
import mockwebserver3.RecordedRequest;
Expand All @@ -85,7 +86,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.junit.rules.TemporaryFolder;
import org.opentest4j.TestAbortedException;

import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
Expand All @@ -101,6 +101,7 @@
import static mockwebserver3.SocketPolicy.SHUTDOWN_INPUT_AT_END;
import static mockwebserver3.SocketPolicy.SHUTDOWN_OUTPUT_AT_END;
import static mockwebserver3.SocketPolicy.UPGRADE_TO_SSL_AT_END;
import static okhttp3.TestUtil.assertSuppressed;
import static okhttp3.internal.Internal.addHeaderLenient;
import static okhttp3.internal.Util.immutableListOf;
import static okhttp3.internal.Util.userAgent;
Expand Down Expand Up @@ -648,7 +649,10 @@ private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exc
getResponse(newRequest("/foo"));
fail();
} catch (IOException expected) {
assertThat(expected.getSuppressed().length).isEqualTo(1);
assertSuppressed(expected, throwables -> {
assertThat(throwables).hasSize(1);
return Unit.INSTANCE;
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package okhttp3.internal.connection;

import java.io.IOException;
import kotlin.Unit;
import org.junit.jupiter.api.Test;

import static okhttp3.TestUtil.assertSuppressed;
import static org.assertj.core.api.Assertions.assertThat;

public class RouteExceptionTest {
Expand All @@ -39,10 +41,12 @@ public class RouteExceptionTest {

IOException connectionIOException = re.getFirstConnectException();
assertThat(connectionIOException).isSameAs(firstException);
Throwable[] suppressedExceptions = connectionIOException.getSuppressed();
assertThat(suppressedExceptions.length).isEqualTo(2);
assertThat(suppressedExceptions[0]).isSameAs(secondException);
assertThat(suppressedExceptions[1]).isSameAs(thirdException);
assertSuppressed(connectionIOException, suppressedExceptions -> {
assertThat(suppressedExceptions.size()).isEqualTo(2);
assertThat(suppressedExceptions.get(0)).isSameAs(secondException);
assertThat(suppressedExceptions.get(1)).isSameAs(thirdException);
return Unit.INSTANCE;
});

assertThat(re.getLastConnectException()).isSameAs(thirdException);
}
Expand Down

0 comments on commit 2f361c2

Please sign in to comment.