diff options
author | Terry Wilson <tmwilson@google.com> | 2023-06-26 13:32:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-26 13:32:05 -0700 |
commit | f3399e3d0e52c4c20409de3b4435fea12bec12c6 (patch) | |
tree | 23eeacecdeaa695cdd99e9be45266bb150efadf0 | |
parent | ac8908ec7f7a237124c2031eed7b14804909a1ce (diff) | |
download | grpc-grpc-java-f3399e3d0e52c4c20409de3b4435fea12bec12c6.tar.gz |
android-interop-testing: Use java.util.concurrent (#10312)
Instead of the deprecated Android AsyncTask, let's use the standard
Java concurrency library with a Callable and an ExecutorService.
-rw-r--r-- | android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/InteropInstrumentationTest.java | 31 | ||||
-rw-r--r-- | android-interop-testing/src/androidTest/java/io/grpc/android/integrationtest/UdsChannelInteropTest.java | 40 | ||||
-rw-r--r-- | android-interop-testing/src/main/java/io/grpc/android/integrationtest/TestCallable.java (renamed from android-interop-testing/src/main/java/io/grpc/android/integrationtest/InteropTask.java) | 51 | ||||
-rw-r--r-- | android-interop-testing/src/main/java/io/grpc/android/integrationtest/TesterActivity.java | 33 |
4 files changed, 67 insertions, 88 deletions
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 5d76f1c0a..5b06c91fe 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 androidx.test.platform.app.InstrumentationRegistry; 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 class InteropInstrumentationTest { } private void runTest(String testCase) throws Exception { - final SettableFuture<String> 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 62206138f..f002dd291 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 class UdsChannelInteropTest { server = NettyServerBuilder.forPort(0) .maxInboundMessageSize(16 * 1024 * 1024) - .addService(new TestServiceImpl(executor)) + .addService(new TestServiceImpl(serverExecutor)) .build(); server.start(); @@ -112,23 +113,16 @@ public class UdsChannelInteropTest { } private void runTest(String testCase) throws Exception { - final SettableFuture<String> 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 index 8beaea3ec..56ea75bb6 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<Void, Void, String> { - 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<String> { + private final ManagedChannel channel; + private final String testCase; + private static final String LOG_TAG = "GrpcInteropTask"; static final String SUCCESS_MESSAGE = "Success!"; - private final WeakReference<Listener> listenerReference; - private final String testCase; - private final Tester tester; - - InteropTask( - Listener listener, - ManagedChannel channel, - String testCase) { - this.listenerReference = new WeakReference<Listener>(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 @@ final class InteropTask extends AsyncTask<Void, Void, String> { } } - 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 @@ final class InteropTask extends AsyncTask<Void, Void, String> { } } - @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 8b782ecea..fb5b35c42 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.IOException; 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<Button> buttons; @@ -51,6 +54,8 @@ public class TesterActivity extends AppCompatActivity private UdsTcpEndpointConnector endpointConnector; + private ExecutorService executor; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -72,6 +77,8 @@ public class TesterActivity extends AppCompatActivity ProviderInstaller.installIfNeededAsync(this, this); // Disable buttons until the security provider installing finishes. enableButtons(false); + + executor = Executors.newSingleThreadExecutor(); } /** Click handler for unix domain socket. */ @@ -110,16 +117,6 @@ public class TesterActivity extends AppCompatActivity } } - @Override - public void onComplete(String result) { - if (endpointConnector != null) { - endpointConnector.shutDown(); - endpointConnector = null; - } - resultText.setText(result); - enableButtons(true); - } - private void startTest(String testCase) { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow( hostEdit.getWindowToken(), 0); @@ -162,7 +159,19 @@ public class TesterActivity extends AppCompatActivity } // Start Test. - new InteropTask(TesterActivity.this, channel, testCase).execute(); + String result = null; + try { + result = executor.submit(new TestCallable(channel, testCase)).get(); + } catch (ExecutionException | InterruptedException e) { + result = e.getMessage(); + } finally { + if (endpointConnector != null) { + endpointConnector.shutDown(); + endpointConnector = null; + } + resultText.setText(result); + enableButtons(true); + } } @Override |