aboutsummaryrefslogtreecommitdiff
path: root/context
diff options
context:
space:
mode:
authorKun Zhang <zhangkun83@users.noreply.github.com>2016-12-13 14:03:23 -0800
committerGitHub <noreply@github.com>2016-12-13 14:03:23 -0800
commit97b926391b6e6d4bfca3fd15c55fcda0efa6b7b8 (patch)
tree067b3af62509d90afd3fcd09a165bb45815ce282 /context
parent677f05d7fb15c5438bb303372e33f117682fa84f (diff)
downloadgrpc-grpc-java-97b926391b6e6d4bfca3fd15c55fcda0efa6b7b8.tar.gz
context: add the four-value withValues(). (#2506)
Diffstat (limited to 'context')
-rw-r--r--context/src/main/java/io/grpc/Context.java9
-rw-r--r--context/src/test/java/io/grpc/ContextTest.java18
2 files changed, 27 insertions, 0 deletions
diff --git a/context/src/main/java/io/grpc/Context.java b/context/src/main/java/io/grpc/Context.java
index db7aaf7ab..4de295605 100644
--- a/context/src/main/java/io/grpc/Context.java
+++ b/context/src/main/java/io/grpc/Context.java
@@ -331,6 +331,15 @@ public class Context {
}
/**
+ * Create a new context with the given key value set. The new context will cascade cancellation
+ * from its parent.
+ */
+ public <V1, V2, V3, V4> Context withValues(Key<V1> k1, V1 v1, Key<V2> k2, V2 v2,
+ Key<V3> k3, V3 v3, Key<V4> k4, V4 v4) {
+ return new Context(this, new Object[][]{{k1, v1}, {k2, v2}, {k3, v3}, {k4, v4}});
+ }
+
+ /**
* Create a new context which propagates the values of this context but does not cascade its
* cancellation.
*/
diff --git a/context/src/test/java/io/grpc/ContextTest.java b/context/src/test/java/io/grpc/ContextTest.java
index af80d11ee..a05808e84 100644
--- a/context/src/test/java/io/grpc/ContextTest.java
+++ b/context/src/test/java/io/grpc/ContextTest.java
@@ -78,6 +78,7 @@ public class ContextTest {
private static final Context.Key<String> FOOD = Context.keyWithDefault("food", "lasagna");
private static final Context.Key<String> COLOR = Context.key("color");
private static final Context.Key<Object> FAVORITE = Context.key("favorite");
+ private static final Context.Key<Integer> LUCKY = Context.key("lucky");
private Context listenerNotifedContext;
private CountDownLatch deadlineLatch = new CountDownLatch(1);
@@ -247,6 +248,23 @@ public class ContextTest {
}
@Test
+ public void withValuesFour() {
+ Object fav = new Object();
+ Context base = Context.current().withValues(PET, "dog", COLOR, "blue");
+ Context child = base.withValues(PET, "cat", FOOD, "cheese", FAVORITE, fav, LUCKY, 7);
+
+ child.attach();
+
+ assertEquals("cat", PET.get());
+ assertEquals("cheese", FOOD.get());
+ assertEquals("blue", COLOR.get());
+ assertEquals(fav, FAVORITE.get());
+ assertEquals(7, (int) LUCKY.get());
+
+ base.attach();
+ }
+
+ @Test
public void cancelReturnsFalseIfAlreadyCancelled() {
Context.CancellableContext base = Context.current().withCancellation();
assertTrue(base.cancel(null));