mirror of https://github.com/grpc/grpc-java.git
context: add the four-value withValues(). (#2506)
This commit is contained in:
parent
677f05d7fb
commit
97b926391b
|
|
@ -330,6 +330,15 @@ public class Context {
|
||||||
return new Context(this, new Object[][]{{k1, v1}, {k2, v2}, {k3, v3}});
|
return new Context(this, new Object[][]{{k1, v1}, {k2, v2}, {k3, v3}});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Create a new context which propagates the values of this context but does not cascade its
|
||||||
* cancellation.
|
* cancellation.
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ public class ContextTest {
|
||||||
private static final Context.Key<String> FOOD = Context.keyWithDefault("food", "lasagna");
|
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<String> COLOR = Context.key("color");
|
||||||
private static final Context.Key<Object> FAVORITE = Context.key("favorite");
|
private static final Context.Key<Object> FAVORITE = Context.key("favorite");
|
||||||
|
private static final Context.Key<Integer> LUCKY = Context.key("lucky");
|
||||||
|
|
||||||
private Context listenerNotifedContext;
|
private Context listenerNotifedContext;
|
||||||
private CountDownLatch deadlineLatch = new CountDownLatch(1);
|
private CountDownLatch deadlineLatch = new CountDownLatch(1);
|
||||||
|
|
@ -246,6 +247,23 @@ public class ContextTest {
|
||||||
base.attach();
|
base.attach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
public void cancelReturnsFalseIfAlreadyCancelled() {
|
public void cancelReturnsFalseIfAlreadyCancelled() {
|
||||||
Context.CancellableContext base = Context.current().withCancellation();
|
Context.CancellableContext base = Context.current().withCancellation();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue