diff --git a/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/InteropInstrumentationTest.java b/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/InteropInstrumentationTest.java index 5d76f1c0a6e..5b06c91fe09 100644 --- a/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/InteropInstrumentationTest.java +++ b/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/InteropInstrumentationTest.java @@ -25,9 +25,10 @@ import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.common.GooglePlayServicesRepairableException; import com.google.android.gms.security.ProviderInstaller; -import com.google.common.util.concurrent.SettableFuture; -import io.grpc.android.integrationtest.InteropTask.Listener; import java.io.InputStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; @@ -44,6 +45,7 @@ public class InteropInstrumentationTest { private String serverHostOverride; private boolean useTestCa; private String testCase; + private ExecutorService executor = Executors.newSingleThreadExecutor(); @Before public void setUp() throws Exception { @@ -101,26 +103,21 @@ public void interopTests() throws Exception { } private void runTest(String testCase) throws Exception { - final SettableFuture resultFuture = SettableFuture.create(); - InteropTask.Listener listener = - new Listener() { - @Override - public void onComplete(String result) { - resultFuture.set(result); - } - }; InputStream testCa; if (useTestCa) { testCa = ApplicationProvider.getApplicationContext().getResources().openRawResource(R.raw.ca); } else { testCa = null; } - new InteropTask( - listener, - TesterOkHttpChannelBuilder.build(host, port, serverHostOverride, useTls, testCa), - testCase) - .execute(); - String result = resultFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); - assertEquals(testCase + " failed", InteropTask.SUCCESS_MESSAGE, result); + + String result = null; + try { + result = executor.submit(new TestCallable( + TesterOkHttpChannelBuilder.build(host, port, serverHostOverride, useTls, testCa), + testCase)).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); + assertEquals(testCase + " failed", TestCallable.SUCCESS_MESSAGE, result); + } catch (ExecutionException | InterruptedException e) { + result = e.getMessage(); + } } } diff --git a/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/UdsChannelInteropTest.java b/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/UdsChannelInteropTest.java index 62206138f6d..f002dd291c7 100644 --- a/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/UdsChannelInteropTest.java +++ b/android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/UdsChannelInteropTest.java @@ -16,22 +16,22 @@ package io.grpc.android.integrationtest; -import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import android.net.LocalSocketAddress.Namespace; import androidx.test.InstrumentationRegistry; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.rule.ActivityTestRule; -import com.google.common.util.concurrent.SettableFuture; import io.grpc.Server; import io.grpc.android.UdsChannelBuilder; -import io.grpc.android.integrationtest.InteropTask.Listener; import io.grpc.netty.NettyServerBuilder; import io.grpc.testing.integration.TestServiceImpl; import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -53,7 +53,8 @@ public class UdsChannelInteropTest { private Server server; private UdsTcpEndpointConnector endpointConnector; - private ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); + private ScheduledExecutorService serverExecutor = Executors.newScheduledThreadPool(2); + private ExecutorService testExecutor = Executors.newSingleThreadExecutor(); // Ensures Looper is initialized for tests running on API level 15. Otherwise instantiating an // AsyncTask throws an exception. @@ -69,7 +70,7 @@ public void setUp() throws IOException { server = NettyServerBuilder.forPort(0) .maxInboundMessageSize(16 * 1024 * 1024) - .addService(new TestServiceImpl(executor)) + .addService(new TestServiceImpl(serverExecutor)) .build(); server.start(); @@ -112,23 +113,16 @@ public void interopTests() throws Exception { } private void runTest(String testCase) throws Exception { - final SettableFuture resultFuture = SettableFuture.create(); - InteropTask.Listener listener = - new Listener() { - @Override - public void onComplete(String result) { - resultFuture.set(result); - } - }; - - new InteropTask( - listener, - UdsChannelBuilder.forPath(UDS_PATH, Namespace.ABSTRACT) - .maxInboundMessageSize(16 * 1024 * 1024) - .build(), - testCase) - .execute(); - String result = resultFuture.get(TIMEOUT_SECONDS, SECONDS); - assertEquals(testCase + " failed", InteropTask.SUCCESS_MESSAGE, result); + String result = null; + try { + result = testExecutor.submit(new TestCallable( + UdsChannelBuilder.forPath(UDS_PATH, Namespace.ABSTRACT) + .maxInboundMessageSize(16 * 1024 * 1024) + .build(), + testCase)).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); + assertEquals(testCase + " failed", TestCallable.SUCCESS_MESSAGE, result); + } catch (ExecutionException | InterruptedException e) { + result = e.getMessage(); + } } } diff --git a/android-interop-testing/src/main/java/io/grpc/android/integrationtest/InteropTask.java b/android-interop-testing/src/main/java/io/grpc/android/integrationtest/TestCallable.java similarity index 83% rename from android-interop-testing/src/main/java/io/grpc/android/integrationtest/InteropTask.java rename to android-interop-testing/src/main/java/io/grpc/android/integrationtest/TestCallable.java index 8beaea3ec9c..56ea75bb657 100644 --- a/android-interop-testing/src/main/java/io/grpc/android/integrationtest/InteropTask.java +++ b/android-interop-testing/src/main/java/io/grpc/android/integrationtest/TestCallable.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 The gRPC Authors + * Copyright 2023 The gRPC Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,49 +16,36 @@ package io.grpc.android.integrationtest; -import android.os.AsyncTask; import android.util.Log; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.testing.integration.AbstractInteropTest; import java.io.PrintWriter; import java.io.StringWriter; -import java.lang.ref.WeakReference; +import java.util.concurrent.Callable; import org.junit.AssumptionViolatedException; -/** AsyncTask for interop test cases. */ -final class InteropTask extends AsyncTask { - private static final String LOG_TAG = "GrpcInteropTask"; - - interface Listener { - void onComplete(String result); - } +/** + * Used to run a single test case against a channel in a separate thread. + */ +public class TestCallable implements Callable { + private final ManagedChannel channel; + private final String testCase; + private static final String LOG_TAG = "GrpcInteropTask"; static final String SUCCESS_MESSAGE = "Success!"; - private final WeakReference listenerReference; - private final String testCase; - private final Tester tester; - - InteropTask( - Listener listener, - ManagedChannel channel, - String testCase) { - this.listenerReference = new WeakReference(listener); + public TestCallable(ManagedChannel channel, String testCase) { + this.channel = channel; this.testCase = testCase; - this.tester = new Tester(channel); } @Override - protected void onPreExecute() { + public String call() { + Tester tester = new Tester(channel); tester.setUp(); - } - - @SuppressWarnings("Finally") - @Override - protected String doInBackground(Void... ignored) { try { - runTest(testCase); + runTest(tester, testCase); return SUCCESS_MESSAGE; } catch (Throwable t) { // Print the stack trace to logcat. @@ -78,7 +65,7 @@ protected String doInBackground(Void... ignored) { } } - private void runTest(String testCase) throws Exception { + private void runTest(Tester tester, String testCase) throws Exception { Log.i(LOG_TAG, "Running test case: " + testCase); if ("empty_unary".equals(testCase)) { tester.emptyUnary(); @@ -138,14 +125,6 @@ private void runTest(String testCase) throws Exception { } } - @Override - protected void onPostExecute(String result) { - Listener listener = listenerReference.get(); - if (listener != null) { - listener.onComplete(result); - } - } - private static class Tester extends AbstractInteropTest { private Tester(ManagedChannel channel) { diff --git a/android-interop-testing/src/main/java/io/grpc/android/integrationtest/TesterActivity.java b/android-interop-testing/src/main/java/io/grpc/android/integrationtest/TesterActivity.java index 8b782ecea29..fb5b35c42d5 100644 --- a/android-interop-testing/src/main/java/io/grpc/android/integrationtest/TesterActivity.java +++ b/android-interop-testing/src/main/java/io/grpc/android/integrationtest/TesterActivity.java @@ -36,9 +36,12 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class TesterActivity extends AppCompatActivity - implements ProviderInstaller.ProviderInstallListener, InteropTask.Listener { + implements ProviderInstaller.ProviderInstallListener { private static final String LOG_TAG = "GrpcTesterActivity"; private List