本文整理汇总了Java中io.grpc.Context类的典型用法代码示例。如果您正苦于以下问题:Java Context类的具体用法?Java Context怎么用?Java Context使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Context类属于io.grpc包,在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: interceptorShouldFreezeContext
import io.grpc.Context; //导入依赖的package包/类
@Test
public void interceptorShouldFreezeContext() {
TestService svc = new TestService();
// Plumbing
serverRule.getServiceRegistry().addService(ServerInterceptors.interceptForward(svc,
new AmbientContextServerInterceptor("ctx-"),
new AmbientContextFreezeServerInterceptor()));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
Metadata.Key<String> key = Metadata.Key.of("ctx-k", Metadata.ASCII_STRING_MARSHALLER);
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(key, "value");
stub.sayHello(HelloRequest.newBuilder().setName("World").build());
});
assertThat(svc.frozen).isTrue();
}
示例2: apply
import io.grpc.Context; //导入依赖的package包/类
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
// Reset the gRPC context between test executions
Context prev = Context.ROOT.attach();
try {
base.evaluate();
if (Context.current() != Context.ROOT) {
Assert.fail("Test is leaking context state between tests! Ensure proper " +
"attach()/detach() pairing.");
}
} finally {
Context.ROOT.detach(prev);
}
}
};
}
示例3: ruleSetsContextToRoot
import io.grpc.Context; //导入依赖的package包/类
@Test
public void ruleSetsContextToRoot() {
Context.current().withValue(Context.key("foo"), "bar").run(() -> {
assertThat(Context.current()).isNotEqualTo(Context.ROOT);
try {
GrpcContextRule rule = new GrpcContextRule();
rule.apply(new Statement() {
@Override
public void evaluate() {
assertThat(Context.current()).isEqualTo(Context.ROOT);
}
}, Description.createTestDescription(GrpcContextRuleTest.class, "ruleSetsContextToRoot"))
.evaluate();
} catch (Throwable throwable) {
fail(throwable.getMessage());
}
});
}
示例4: ruleFailsIfContextLeaks
import io.grpc.Context; //导入依赖的package包/类
@Test
public void ruleFailsIfContextLeaks() {
Context.current().withValue(Context.key("foo"), "bar").run(() -> {
assertThat(Context.current()).isNotEqualTo(Context.ROOT);
assertThatThrownBy(() -> {
GrpcContextRule rule = new GrpcContextRule();
rule.apply(new Statement() {
@Override
public void evaluate() {
// Leak context
Context.current().withValue(Context.key("cheese"), "baz").attach();
}
}, Description.createTestDescription(GrpcContextRuleTest.class, "ruleSetsContextToRoot"))
.evaluate();
}).isInstanceOf(AssertionError.class).hasMessageContaining("Test is leaking context");
});
}
示例5: run
import io.grpc.Context; //导入依赖的package包/类
@Override
public void run() {
Context origContext =
Context.current().withValue(ContextUtils.CONTEXT_SPAN_KEY, span).attach();
try {
runnable.run();
} catch (Throwable t) {
setErrorStatus(span, t);
Throwables.propagateIfPossible(t);
throw new RuntimeException("unexpected", t);
} finally {
Context.current().detach(origContext);
if (endSpan) {
span.end();
}
}
}
示例6: call
import io.grpc.Context; //导入依赖的package包/类
@Override
public V call() throws Exception {
Context origContext =
Context.current().withValue(ContextUtils.CONTEXT_SPAN_KEY, span).attach();
try {
return callable.call();
} catch (Exception e) {
setErrorStatus(span, e);
throw e;
} catch (Throwable t) {
setErrorStatus(span, t);
Throwables.propagateIfPossible(t);
throw new RuntimeException("unexpected", t);
} finally {
Context.current().detach(origContext);
if (endSpan) {
span.end();
}
}
}
示例7: record_CurrentContextSet
import io.grpc.Context; //导入依赖的package包/类
@Test
public void record_CurrentContextSet() {
View view =
View.create(
VIEW_NAME,
"description",
MEASURE_DOUBLE,
Sum.create(),
Arrays.asList(KEY),
Cumulative.create());
viewManager.registerView(view);
Context orig =
Context.current()
.withValue(ContextUtils.TAG_CONTEXT_KEY, new SimpleTagContext(Tag.create(KEY, VALUE)))
.attach();
try {
statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0).record();
} finally {
Context.current().detach(orig);
}
ViewData viewData = viewManager.getView(VIEW_NAME);
// record() should have used the given TagContext.
assertThat(viewData.getAggregationMap().keySet()).containsExactly(Arrays.asList(VALUE));
}
示例8: testWithTagContextUsingWrap
import io.grpc.Context; //导入依赖的package包/类
@Test
public void testWithTagContextUsingWrap() {
Runnable runnable;
Scope scopedTags = CurrentTagContextUtils.withTagContext(tagContext);
try {
assertThat(CurrentTagContextUtils.getCurrentTagContext()).isSameAs(tagContext);
runnable =
Context.current()
.wrap(
new Runnable() {
@Override
public void run() {
assertThat(CurrentTagContextUtils.getCurrentTagContext())
.isSameAs(tagContext);
}
});
} finally {
scopedTags.close();
}
assertThat(tagContextToList(CurrentTagContextUtils.getCurrentTagContext())).isEmpty();
// When we run the runnable we will have the TagContext in the current Context.
runnable.run();
}
示例9: start_Runnable
import io.grpc.Context; //导入依赖的package包/类
@Test(timeout = 60000)
public void start_Runnable() throws Exception {
final Context context = Context.current().withValue(KEY, "myvalue");
previousContext = context.attach();
final AtomicBoolean tested = new AtomicBoolean(false);
Runnable runnable =
new Runnable() {
@Override
public void run() {
assertThat(Context.current()).isSameAs(context);
assertThat(KEY.get()).isEqualTo("myvalue");
tested.set(true);
}
};
Thread thread = new Thread(runnable);
thread.start();
thread.join();
assertThat(tested.get()).isTrue();
}
示例10: start_Subclass
import io.grpc.Context; //导入依赖的package包/类
@Test(timeout = 60000)
public void start_Subclass() throws Exception {
final Context context = Context.current().withValue(KEY, "myvalue");
previousContext = context.attach();
final AtomicBoolean tested = new AtomicBoolean(false);
class MyThread extends Thread {
@Override
public void run() {
assertThat(Context.current()).isSameAs(context);
assertThat(KEY.get()).isEqualTo("myvalue");
tested.set(true);
}
}
Thread thread = new MyThread();
thread.start();
thread.join();
assertThat(tested.get()).isTrue();
}
示例11: execute
import io.grpc.Context; //导入依赖的package包/类
@Test(timeout = 60000)
public void execute() throws Exception {
final Thread callerThread = Thread.currentThread();
final Context context = Context.current().withValue(KEY, "myvalue");
previousContext = context.attach();
final Semaphore tested = new Semaphore(0);
executor.execute(
new Runnable() {
@Override
public void run() {
assertThat(Thread.currentThread()).isNotSameAs(callerThread);
assertThat(Context.current()).isSameAs(context);
assertThat(KEY.get()).isEqualTo("myvalue");
tested.release();
}
});
tested.acquire();
}
示例12: submit_Callable
import io.grpc.Context; //导入依赖的package包/类
@Test(timeout = 60000)
public void submit_Callable() throws Exception {
final Thread callerThread = Thread.currentThread();
final Context context = Context.current().withValue(KEY, "myvalue");
previousContext = context.attach();
final AtomicBoolean tested = new AtomicBoolean(false);
executor
.submit(
new Callable<Void>() {
@Override
public Void call() throws Exception {
assertThat(Thread.currentThread()).isNotSameAs(callerThread);
assertThat(Context.current()).isSameAs(context);
assertThat(KEY.get()).isEqualTo("myvalue");
tested.set(true);
return null;
}
})
.get();
assertThat(tested.get()).isTrue();
}
示例13: submit_Runnable
import io.grpc.Context; //导入依赖的package包/类
@Test(timeout = 60000)
public void submit_Runnable() throws Exception {
final Thread callerThread = Thread.currentThread();
final Context context = Context.current().withValue(KEY, "myvalue");
previousContext = context.attach();
final AtomicBoolean tested = new AtomicBoolean(false);
executor
.submit(
new Runnable() {
@Override
public void run() {
assertThat(Thread.currentThread()).isNotSameAs(callerThread);
assertThat(Context.current()).isSameAs(context);
assertThat(KEY.get()).isEqualTo("myvalue");
tested.set(true);
}
})
.get();
assertThat(tested.get()).isTrue();
}
示例14: submit_RunnableWithResult
import io.grpc.Context; //导入依赖的package包/类
@Test(timeout = 60000)
public void submit_RunnableWithResult() throws Exception {
final Thread callerThread = Thread.currentThread();
final Context context = Context.current().withValue(KEY, "myvalue");
previousContext = context.attach();
final AtomicBoolean tested = new AtomicBoolean(false);
Object result = new Object();
Future<Object> future =
executor.submit(
new Runnable() {
@Override
public void run() {
assertThat(Thread.currentThread()).isNotSameAs(callerThread);
assertThat(Context.current()).isNotSameAs(Context.ROOT);
assertThat(Context.current()).isSameAs(context);
assertThat(KEY.get()).isEqualTo("myvalue");
tested.set(true);
}
},
result);
assertThat(future.get()).isSameAs(result);
assertThat(tested.get()).isTrue();
}
示例15: doesPropagateContext
import io.grpc.Context; //导入依赖的package包/类
@Test
public void doesPropagateContext() throws Exception {
final Context oldContext = Context.current();
final Context newContext = oldContext.withValues(KEY_1, VAL_1, KEY_2, VAL_2);
newContext.attach();
final TestSubscriber<Object> subscriber = new TestSubscriber<Object>();
Observable.create(subscriber1 -> {
subscriber1.onNext(KEY_1.get());
subscriber1.onNext(KEY_2.get());
subscriber1.onCompleted();
}).subscribeOn(Schedulers.computation()).subscribe(subscriber);
newContext.detach(oldContext);
subscriber.awaitTerminalEvent();
subscriber.assertValues(VAL_1, VAL_2);
}
开发者ID:bmcstdio,项目名称:rxjava-grpc-context-hook,代码行数:20,代码来源:GrpcContextPropagatingOnScheduleActionTests.java