|
27 | 27 | import com.google.common.util.concurrent.MoreExecutors; |
28 | 28 | import io.grpc.ClientCall; |
29 | 29 | import io.grpc.ClientCall.Listener; |
| 30 | +import io.grpc.Context; |
30 | 31 | import io.grpc.Deadline; |
| 32 | +import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; |
31 | 33 | import io.grpc.ForwardingTestUtil; |
32 | 34 | import io.grpc.Metadata; |
33 | 35 | import io.grpc.Status; |
|
36 | 38 | import java.lang.reflect.Modifier; |
37 | 39 | import java.util.Arrays; |
38 | 40 | import java.util.concurrent.Executor; |
| 41 | +import java.util.concurrent.atomic.AtomicReference; |
39 | 42 | import org.junit.Rule; |
40 | 43 | import org.junit.Test; |
41 | 44 | import org.junit.runner.RunWith; |
@@ -191,6 +194,35 @@ public void setCallThenCancel() { |
191 | 194 | verify(listener).onClose(Status.CANCELLED, null); |
192 | 195 | } |
193 | 196 |
|
| 197 | + @Test |
| 198 | + public void delayedCallsRunUnderContext() throws Exception { |
| 199 | + Context.Key<Object> contextKey = Context.key("foo"); |
| 200 | + Object goldenValue = new Object(); |
| 201 | + DelayedClientCall<String, Integer> delayedClientCall = |
| 202 | + Context.current().withValue(contextKey, goldenValue).call(() -> |
| 203 | + new DelayedClientCall<>(callExecutor, fakeClock.getScheduledExecutorService(), null)); |
| 204 | + AtomicReference<Context> readyContext = new AtomicReference<>(); |
| 205 | + delayedClientCall.start(new ClientCall.Listener<Integer>() { |
| 206 | + @Override public void onReady() { |
| 207 | + readyContext.set(Context.current()); |
| 208 | + } |
| 209 | + }, new Metadata()); |
| 210 | + AtomicReference<Context> startContext = new AtomicReference<>(); |
| 211 | + Runnable r = delayedClientCall.setCall(new SimpleForwardingClientCall<String, Integer>( |
| 212 | + mockRealCall) { |
| 213 | + @Override public void start(Listener<Integer> listener, Metadata metadata) { |
| 214 | + startContext.set(Context.current()); |
| 215 | + listener.onReady(); // Delayed until call finishes draining |
| 216 | + assertThat(readyContext.get()).isNull(); |
| 217 | + super.start(listener, metadata); |
| 218 | + } |
| 219 | + }); |
| 220 | + assertThat(r).isNotNull(); |
| 221 | + r.run(); |
| 222 | + assertThat(contextKey.get(startContext.get())).isEqualTo(goldenValue); |
| 223 | + assertThat(contextKey.get(readyContext.get())).isEqualTo(goldenValue); |
| 224 | + } |
| 225 | + |
194 | 226 | private void callMeMaybe(Runnable r) { |
195 | 227 | if (r != null) { |
196 | 228 | r.run(); |
|
0 commit comments