Skip to content
Open
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
16 changes: 16 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;

Check failure on line 30 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (11)

package java.util.function does not exist

Check failure on line 30 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (11)

package java.util.function does not exist

Check failure on line 30 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (17)

package java.util.function does not exist

Check failure on line 30 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (17)

package java.util.function does not exist
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -554,6 +555,21 @@
}
}

/**
* Immediately supply a value from a {@link Supplier} with this context as the
* {@link #current} context.
* @param supplier {@link Supplier} to use to produce the value.
* @return result of supplier.
*/
public <V> V supply(Supplier<V> supplier) {

Check failure on line 564 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (11)

cannot find symbol

Check failure on line 564 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (11)

cannot find symbol

Check failure on line 564 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (17)

cannot find symbol

Check failure on line 564 in api/src/context/java/io/grpc/Context.java

View workflow job for this annotation

GitHub Actions / tests (17)

cannot find symbol
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
41 changes: 41 additions & 0 deletions api/src/test/java/io/grpc/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 +554,46 @@ public Object call() {
current.detach(Context.ROOT);
}

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

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

assertSame(ret, base.supply(supplier));
assertSame(base, observed);
assertSame(current, Context.current());

assertSame(ret, current.supply(supplier));
assertSame(current, observed);
assertSame(current, Context.current());

final TestError err = new TestError();
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestError caught = assertThrows(TestError.class, ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel strongly I can, but I was keeping the style of the tests around it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertThrows() was not available back then, worth using in new code

base.supply(new Supplier<Object>() {
@Override
public Object get() {
throw err;
}
});
fail("Excepted exception");
} catch (TestError ex) {
assertSame(err, ex);
}
assertSame(current, Context.current());

current.detach(Context.ROOT);
}

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