当前位置: 首页>>代码示例>>Java>>正文


Java Context类代码示例

本文整理汇总了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();
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:22,代码来源:AmbientContextFreezeInterceptorTest.java

示例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);
            }
        }
    };
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:20,代码来源:GrpcContextRule.java

示例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());
        }
    });
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:20,代码来源:GrpcContextRuleTest.java

示例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");
    });
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:19,代码来源:GrpcContextRuleTest.java

示例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();
    }
  }
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:18,代码来源:CurrentSpanUtils.java

示例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();
    }
  }
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:21,代码来源:CurrentSpanUtils.java

示例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));
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:26,代码来源:StatsRecorderImplTest.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:24,代码来源:CurrentTagContextUtilsTest.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:24,代码来源:ThreadInstrumentationIT.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:25,代码来源:ThreadInstrumentationIT.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:22,代码来源:ExecutorInstrumentationIT.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:26,代码来源:ExecutorInstrumentationIT.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:24,代码来源:ExecutorInstrumentationIT.java

示例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();
}
 
开发者ID:census-instrumentation,项目名称:opencensus-java,代码行数:27,代码来源:ExecutorInstrumentationIT.java

示例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


注:本文中的io.grpc.Context类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。