Skip to content

Commit 6966536

Browse files
api: Add a Supplier overload to Context (grpc#12935)
When trying to enter a Context, the call method doesn't work well with non-throwing Callables, such as lambdas. This change adds a Supplier overload to Context which would solve this, since it doesn't declare a checked exception. Fixes grpc#12927
1 parent 96807d8 commit 6966536

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎api/src/context/java/io/grpc/Context.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.TimeUnit;
2828
import java.util.concurrent.TimeoutException;
2929
import java.util.concurrent.atomic.AtomicReference;
30+
import java.util.function.Supplier;
3031
import java.util.logging.Level;
3132
import java.util.logging.Logger;
3233

@@ -554,6 +555,26 @@ public <V> V call(Callable<V> c) throws Exception {
554555
}
555556
}
556557

558+
/**
559+
* Immediately gets a value from a {@link Supplier} with this context as the
560+
* {@link #current} context.
561+
*
562+
* <p>This API is <a href="https://github.com/grpc/grpc-java/pull/12935">experimental</a> and
563+
* subject to change.
564+
*
565+
* @param supplier {@link Supplier} to use to produce the value.
566+
* @see io.grpc.ExperimentalApi
567+
* @return result of supplier.
568+
*/
569+
public <V> V supply(Supplier<V> supplier) {
570+
Context previous = attach();
571+
try {
572+
return supplier.get();
573+
} finally {
574+
detach(previous);
575+
}
576+
}
577+
557578
/**
558579
* Wrap a {@link Runnable} so that it executes with this context as the {@link #current} context.
559580
*/

‎api/src/test/java/io/grpc/ContextTest.java‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.Assert.assertNotSame;
2727
import static org.junit.Assert.assertNull;
2828
import static org.junit.Assert.assertSame;
29+
import static org.junit.Assert.assertThrows;
2930
import static org.junit.Assert.assertTrue;
3031
import static org.junit.Assert.fail;
3132

@@ -49,6 +50,7 @@
4950
import java.util.concurrent.TimeoutException;
5051
import java.util.concurrent.atomic.AtomicBoolean;
5152
import java.util.concurrent.atomic.AtomicReference;
53+
import java.util.function.Supplier;
5254
import java.util.logging.Handler;
5355
import java.util.logging.Level;
5456
import java.util.logging.LogRecord;
@@ -553,6 +555,75 @@ public Object call() {
553555
current.detach(Context.ROOT);
554556
}
555557

558+
@Test
559+
public void supply_executesSupplierUnderTargetContextAndRestoresPrevious() {
560+
Context base = Context.current().withValue(PET, "cat");
561+
Context current = Context.current().withValue(PET, "fish");
562+
Context toRestore = current.attach();
563+
564+
final Object ret = new Object();
565+
Supplier<Object> supplier = new Supplier<Object>() {
566+
@Override
567+
public Object get() {
568+
runner.run();
569+
return ret;
570+
}
571+
};
572+
573+
Object result = base.supply(supplier);
574+
575+
assertSame(ret, result);
576+
assertSame(base, observed);
577+
assertSame(current, Context.current());
578+
579+
current.detach(toRestore);
580+
}
581+
582+
@Test
583+
public void supply_whenContextIsAlreadyCurrent_executesAndMaintainsContext() {
584+
Context current = Context.current().withValue(PET, "fish");
585+
Context toRestore = current.attach();
586+
587+
final Object ret = new Object();
588+
Supplier<Object> supplier = new Supplier<Object>() {
589+
@Override
590+
public Object get() {
591+
runner.run();
592+
return ret;
593+
}
594+
};
595+
596+
Object result = current.supply(supplier);
597+
598+
assertSame(ret, result);
599+
assertSame(current, observed);
600+
assertSame(current, Context.current());
601+
602+
current.detach(toRestore);
603+
}
604+
605+
@Test
606+
public void supply_whenSupplierThrows_propagatesExceptionAndRestoresPreviousContext() {
607+
Context base = Context.current().withValue(PET, "cat");
608+
Context current = Context.current().withValue(PET, "fish");
609+
Context toRestore = current.attach();
610+
611+
final TestError err = new TestError();
612+
Supplier<Object> supplier = new Supplier<Object>() {
613+
@Override
614+
public Object get() {
615+
throw err;
616+
}
617+
};
618+
619+
TestError thrown = assertThrows(TestError.class, () -> base.supply(supplier));
620+
621+
assertSame(err, thrown);
622+
assertSame(current, Context.current());
623+
624+
current.detach(toRestore);
625+
}
626+
556627
@Test
557628
public void currentContextExecutor() {
558629
QueuedExecutor queuedExecutor = new QueuedExecutor();

0 commit comments

Comments
 (0)