Skip to content
Merged
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 @@ -20,17 +20,21 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doThrow;
import static org.robolectric.Shadows.shadowOf;

import android.os.IBinder;
import android.os.Looper;
import android.os.Parcel;
import android.os.RemoteException;
import com.google.common.collect.ImmutableList;
import io.grpc.Attributes;
import io.grpc.ServerStreamTracer;
import io.grpc.Status;
import io.grpc.internal.FixedObjectPool;
import io.grpc.internal.MockServerTransportListener;
import io.grpc.internal.ObjectPool;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -60,26 +64,74 @@ public final class BinderServerTransportTest {

@Before
public void setUp() throws Exception {
transport =
new BinderServerTransport(
new FixedObjectPool<>(executorService),
Attributes.EMPTY,
ImmutableList.of(),
OneWayBinderProxy.IDENTITY_DECORATOR,
mockBinder);
transportListener = new MockServerTransportListener(transport);
}

// Provide defaults so that we can "include only relevant details in tests."
BinderServerTransportBuilder newBinderServerTransportBuilder() {
return new BinderServerTransportBuilder()
.setExecutorServicePool(new FixedObjectPool<>(executorService))
.setAttributes(Attributes.EMPTY)
.setStreamTracerFactories(ImmutableList.of())
.setBinderDecorator(OneWayBinderProxy.IDENTITY_DECORATOR)
.setCallbackBinder(mockBinder);
}

@Test
public void testSetupTransactionFailureCausesMultipleShutdowns_b153460678() throws Exception {
public void testSetupTransactionFailureReportsMultipleTerminations_b153460678() throws Exception {
// Make the binder fail the setup transaction.
when(mockBinder.transact(anyInt(), any(Parcel.class), isNull(), anyInt())).thenReturn(false);
doThrow(new RemoteException())
.when(mockBinder)
.transact(anyInt(), any(Parcel.class), isNull(), anyInt());
transport = newBinderServerTransportBuilder().setCallbackBinder(mockBinder).build();
shadowOf(Looper.getMainLooper()).idle();
transport.start(transportListener);

// Now shut it down.
// Now shut it down externally *before* executing Runnables scheduled on the executor.
transport.shutdownNow(Status.UNKNOWN.withDescription("reasons"));
shadowOf(Looper.getMainLooper()).idle();

assertThat(transportListener.isTerminated()).isTrue();
}

static class BinderServerTransportBuilder {
ObjectPool<ScheduledExecutorService> executorServicePool;
Attributes attributes;
List<ServerStreamTracer.Factory> streamTracerFactories;
OneWayBinderProxy.Decorator binderDecorator;
IBinder callbackBinder;

public BinderServerTransport build() {
return new BinderServerTransport(
executorServicePool, attributes, streamTracerFactories, binderDecorator, callbackBinder);
}

public BinderServerTransportBuilder setExecutorServicePool(
ObjectPool<ScheduledExecutorService> executorServicePool) {
this.executorServicePool = executorServicePool;
return this;
}

public BinderServerTransportBuilder setAttributes(Attributes attributes) {
this.attributes = attributes;
return this;
}

public BinderServerTransportBuilder setStreamTracerFactories(
List<ServerStreamTracer.Factory> streamTracerFactories) {
this.streamTracerFactories = streamTracerFactories;
return this;
}

public BinderServerTransportBuilder setBinderDecorator(
OneWayBinderProxy.Decorator binderDecorator) {
this.binderDecorator = binderDecorator;
return this;
}

public BinderServerTransportBuilder setCallbackBinder(IBinder callbackBinder) {
this.callbackBinder = callbackBinder;
return this;
}
}
}
Loading