|
20 | 20 | import static org.mockito.ArgumentMatchers.any; |
21 | 21 | import static org.mockito.ArgumentMatchers.anyInt; |
22 | 22 | import static org.mockito.ArgumentMatchers.isNull; |
23 | | -import static org.mockito.Mockito.when; |
| 23 | +import static org.mockito.Mockito.doThrow; |
24 | 24 | import static org.robolectric.Shadows.shadowOf; |
25 | 25 |
|
26 | 26 | import android.os.IBinder; |
27 | 27 | import android.os.Looper; |
28 | 28 | import android.os.Parcel; |
| 29 | +import android.os.RemoteException; |
29 | 30 | import com.google.common.collect.ImmutableList; |
30 | 31 | import io.grpc.Attributes; |
| 32 | +import io.grpc.ServerStreamTracer; |
31 | 33 | import io.grpc.Status; |
32 | 34 | import io.grpc.internal.FixedObjectPool; |
33 | 35 | import io.grpc.internal.MockServerTransportListener; |
| 36 | +import io.grpc.internal.ObjectPool; |
| 37 | +import java.util.List; |
34 | 38 | import java.util.concurrent.ScheduledExecutorService; |
35 | 39 | import org.junit.Before; |
36 | 40 | import org.junit.Rule; |
@@ -60,26 +64,74 @@ public final class BinderServerTransportTest { |
60 | 64 |
|
61 | 65 | @Before |
62 | 66 | public void setUp() throws Exception { |
63 | | - transport = |
64 | | - new BinderServerTransport( |
65 | | - new FixedObjectPool<>(executorService), |
66 | | - Attributes.EMPTY, |
67 | | - ImmutableList.of(), |
68 | | - OneWayBinderProxy.IDENTITY_DECORATOR, |
69 | | - mockBinder); |
70 | 67 | transportListener = new MockServerTransportListener(transport); |
71 | 68 | } |
72 | 69 |
|
| 70 | + // Provide defaults so that we can "include only relevant details in tests." |
| 71 | + BinderServerTransportBuilder newBinderServerTransportBuilder() { |
| 72 | + return new BinderServerTransportBuilder() |
| 73 | + .setExecutorServicePool(new FixedObjectPool<>(executorService)) |
| 74 | + .setAttributes(Attributes.EMPTY) |
| 75 | + .setStreamTracerFactories(ImmutableList.of()) |
| 76 | + .setBinderDecorator(OneWayBinderProxy.IDENTITY_DECORATOR) |
| 77 | + .setCallbackBinder(mockBinder); |
| 78 | + } |
| 79 | + |
73 | 80 | @Test |
74 | | - public void testSetupTransactionFailureCausesMultipleShutdowns_b153460678() throws Exception { |
| 81 | + public void testSetupTransactionFailureReportsMultipleTerminations_b153460678() throws Exception { |
75 | 82 | // Make the binder fail the setup transaction. |
76 | | - when(mockBinder.transact(anyInt(), any(Parcel.class), isNull(), anyInt())).thenReturn(false); |
| 83 | + doThrow(new RemoteException()) |
| 84 | + .when(mockBinder) |
| 85 | + .transact(anyInt(), any(Parcel.class), isNull(), anyInt()); |
| 86 | + transport = newBinderServerTransportBuilder().setCallbackBinder(mockBinder).build(); |
| 87 | + shadowOf(Looper.getMainLooper()).idle(); |
77 | 88 | transport.start(transportListener); |
78 | 89 |
|
79 | | - // Now shut it down. |
| 90 | + // Now shut it down externally *before* executing Runnables scheduled on the executor. |
80 | 91 | transport.shutdownNow(Status.UNKNOWN.withDescription("reasons")); |
81 | 92 | shadowOf(Looper.getMainLooper()).idle(); |
82 | 93 |
|
83 | 94 | assertThat(transportListener.isTerminated()).isTrue(); |
84 | 95 | } |
| 96 | + |
| 97 | + static class BinderServerTransportBuilder { |
| 98 | + ObjectPool<ScheduledExecutorService> executorServicePool; |
| 99 | + Attributes attributes; |
| 100 | + List<ServerStreamTracer.Factory> streamTracerFactories; |
| 101 | + OneWayBinderProxy.Decorator binderDecorator; |
| 102 | + IBinder callbackBinder; |
| 103 | + |
| 104 | + public BinderServerTransport build() { |
| 105 | + return new BinderServerTransport( |
| 106 | + executorServicePool, attributes, streamTracerFactories, binderDecorator, callbackBinder); |
| 107 | + } |
| 108 | + |
| 109 | + public BinderServerTransportBuilder setExecutorServicePool( |
| 110 | + ObjectPool<ScheduledExecutorService> executorServicePool) { |
| 111 | + this.executorServicePool = executorServicePool; |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public BinderServerTransportBuilder setAttributes(Attributes attributes) { |
| 116 | + this.attributes = attributes; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public BinderServerTransportBuilder setStreamTracerFactories( |
| 121 | + List<ServerStreamTracer.Factory> streamTracerFactories) { |
| 122 | + this.streamTracerFactories = streamTracerFactories; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public BinderServerTransportBuilder setBinderDecorator( |
| 127 | + OneWayBinderProxy.Decorator binderDecorator) { |
| 128 | + this.binderDecorator = binderDecorator; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + public BinderServerTransportBuilder setCallbackBinder(IBinder callbackBinder) { |
| 133 | + this.callbackBinder = callbackBinder; |
| 134 | + return this; |
| 135 | + } |
| 136 | + } |
85 | 137 | } |
0 commit comments