Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -101,26 +103,21 @@ public void interopTests() throws Exception {
}

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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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();

Expand Down Expand Up @@ -112,23 +113,16 @@ public void interopTests() throws Exception {
}

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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Button> buttons;
Expand All @@ -51,6 +54,8 @@ public class TesterActivity extends AppCompatActivity

private UdsTcpEndpointConnector endpointConnector;

private ExecutorService executor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -72,6 +77,8 @@ protected void onCreate(Bundle savedInstanceState) {
ProviderInstaller.installIfNeededAsync(this, this);
// Disable buttons until the security provider installing finishes.
enableButtons(false);

executor = Executors.newSingleThreadExecutor();
}

/** Click handler for unix domain socket. */
Expand Down Expand Up @@ -110,16 +117,6 @@ private void enableButtons(boolean enable) {
}
}

@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);
Expand Down Expand Up @@ -162,7 +159,19 @@ private void startTest(String testCase) {
}

// 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
Expand Down