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
21 changes: 21 additions & 0 deletions api/src/context/java/io/grpc/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -554,6 +555,26 @@ public <V> V call(Callable<V> c) throws Exception {
}
}

/**
* Immediately gets a value from a {@link Supplier} with this context as the
* {@link #current} context.
*
* <p>This API is <a href="https://github.com/grpc/grpc-java/pull/12935">experimental</a> and
* subject to change.
*
* @param supplier {@link Supplier} to use to produce the value.
* @see io.grpc.ExperimentalApi
* @return result of supplier.
*/
public <V> V supply(Supplier<V> supplier) {
Context previous = attach();
try {
return supplier.get();
} finally {
detach(previous);
}
}

/**
* Wrap a {@link Runnable} so that it executes with this context as the {@link #current} context.
*/
Expand Down
71 changes: 71 additions & 0 deletions api/src/test/java/io/grpc/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -49,6 +50,7 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand Down Expand Up @@ -553,6 +555,75 @@ public Object call() {
current.detach(Context.ROOT);
}

@Test
public void supply_executesSupplierUnderTargetContextAndRestoresPrevious() {
Context base = Context.current().withValue(PET, "cat");
Context current = Context.current().withValue(PET, "fish");
Context toRestore = current.attach();

final Object ret = new Object();
Supplier<Object> supplier = new Supplier<Object>() {
@Override
public Object get() {
runner.run();
return ret;
}
};

Object result = base.supply(supplier);

assertSame(ret, result);
assertSame(base, observed);
assertSame(current, Context.current());

current.detach(toRestore);
}

@Test
public void supply_whenContextIsAlreadyCurrent_executesAndMaintainsContext() {
Context current = Context.current().withValue(PET, "fish");
Context toRestore = current.attach();

final Object ret = new Object();
Supplier<Object> supplier = new Supplier<Object>() {
@Override
public Object get() {
runner.run();
return ret;
}
};

Object result = current.supply(supplier);

assertSame(ret, result);
assertSame(current, observed);
assertSame(current, Context.current());

current.detach(toRestore);
}

@Test
public void supply_whenSupplierThrows_propagatesExceptionAndRestoresPreviousContext() {
Context base = Context.current().withValue(PET, "cat");
Context current = Context.current().withValue(PET, "fish");
Context toRestore = current.attach();

final TestError err = new TestError();
Supplier<Object> supplier = new Supplier<Object>() {
@Override
public Object get() {
throw err;
}
};

TestError thrown = assertThrows(TestError.class, () -> base.supply(supplier));

assertSame(err, thrown);
assertSame(current, Context.current());

current.detach(toRestore);
}

@Test
public void currentContextExecutor() {
QueuedExecutor queuedExecutor = new QueuedExecutor();
Expand Down
Loading