Skip to content

Commit

Permalink
test: skip SpanTest on JDK17 and higher (#1472)
Browse files Browse the repository at this point in the history
The trick that the SpanTest class does to change a private static final field
is no longer possible in JDK12 and higher. This test therefore needs to be
skipped when the tests are running on Java12 or higher.

Fixes the build error in #1462
  • Loading branch information
olavloite committed Oct 4, 2021
1 parent f9de85a commit bbe7603
Showing 1 changed file with 22 additions and 3 deletions.
Expand Up @@ -40,6 +40,7 @@
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -99,8 +100,23 @@ public class SpanTest {
.withDescription("Non-retryable test exception.")
.asRuntimeException();

private static int getVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}

@BeforeClass
public static void startStaticServer() throws Exception {
Assume.assumeTrue("This test is only supported on JDK11 and lower", getVersion() < 12);

mockSpanner = new MockSpannerServiceImpl();
mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions.
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
Expand All @@ -120,7 +136,8 @@ public static void startStaticServer() throws Exception {
.start();
channelProvider = LocalChannelProvider.create(uniqueName);

// Use a little bit reflection to set the test tracer.
// Use a little reflection to set the test tracer.
// This is not possible in Java 12 and later.
java.lang.reflect.Field field = Tracing.class.getDeclaredField("traceComponent");
field.setAccessible(true);
java.lang.reflect.Field modifiersField =
Expand All @@ -133,8 +150,10 @@ public static void startStaticServer() throws Exception {

@AfterClass
public static void stopServer() throws InterruptedException {
server.shutdown();
server.awaitTermination();
if (server != null) {
server.shutdown();
server.awaitTermination();
}
}

@Before
Expand Down

0 comments on commit bbe7603

Please sign in to comment.