reactor.util.context.Context.of()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(232)

本文整理了Java中reactor.util.context.Context.of()方法的一些代码示例,展示了Context.of()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.of()方法的具体详情如下:
包路径:reactor.util.context.Context
类名称:Context
方法名:of

Context.of介绍

[英]Create a Context pre-initialized with one key-value pair.
[中]创建一个使用一个键值对预初始化的上下文。

代码示例

代码示例来源:origin: spring-projects/spring-security

/**
 * Creates a Reactor {@link Context} that contains the {@code Mono<SecurityContext>}
 * that can be merged into another {@link Context}
 * @param securityContext the {@code Mono<SecurityContext>} to set in the returned
 * Reactor {@link Context}
 * @return a Reactor {@link Context} that contains the {@code Mono<SecurityContext>}
 */
public static Context withSecurityContext(Mono<? extends SecurityContext> securityContext) {
  return Context.of(SECURITY_CONTEXT_KEY, securityContext);
}

代码示例来源:origin: lettuce-io/lettuce-core

/**
 * Creates a Reactor {@link Context} that contains the {@code Mono<TraceContextProvider>}. that can be merged into another
 * {@link Context}.
 *
 * @param supplier the {@link TraceContextProvider} to set in the returned Reactor {@link Context}.
 * @return a Reactor {@link Context} that contains the {@code Mono<TraceContextProvider>}.
 */
static Context withTraceContextProvider(TraceContextProvider supplier) {
  return Context.of(TraceContextProvider.class, supplier);
}

代码示例来源:origin: spring-projects/spring-security

private Context serverWebExchange() {
  return Context.of(ServerWebExchange.class, this.serverWebExchange);
}

代码示例来源:origin: spring-projects/spring-security

@Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    return chain.filter(exchange)
        .subscriberContext(Context.of(ServerWebExchange.class, exchange));
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorLocal() {
  BiFunction<Throwable, Object, Throwable> localHook = (e, v) ->
      new IllegalStateException("boom_" + v, e);
  Context c = Context.of(Hooks.KEY_ON_OPERATOR_ERROR, localHook);
  IllegalArgumentException failure = new IllegalArgumentException("foo");
  final Throwable throwable = Operators.onOperatorError(null, failure,
      "foo", c);
  assertThat(throwable).isInstanceOf(IllegalStateException.class)
             .hasMessage("boom_foo")
             .hasCause(failure);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void of2() throws Exception {
  Context c = Context.of(1, 100, 2, 200);
  assertThat(c).isInstanceOf(Context2.class);
  assertThat(c.isEmpty()).as("isEmpty").isFalse();
  assertThat(c.stream()).hasSize(2);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void ofApi() {
  assertThat(Context.of("test", 12))
      .isInstanceOf(Context1.class)
      .hasToString("Context1{test=12}");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void discardTryOnNextPredicateFail() {
  List<Object> discarded = new ArrayList<>();
  CoreSubscriber<Integer> actual = new AssertSubscriber<>(
      Context.of(Hooks.KEY_ON_DISCARD, (Consumer<?>) discarded::add));
  FilterSubscriber<Integer> subscriber =
      new FilterSubscriber<>(actual, i -> { throw new IllegalStateException("boom"); });
  subscriber.onSubscribe(Operators.emptySubscription());
  subscriber.tryOnNext(1);
  assertThat(discarded).containsExactly(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void notContainsAllOfMap() throws Exception {
  Map<String, String> expected = new HashMap<>();
  expected.put("foo", "bar");
  expected.put("other", "stuff");
  assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")),
      e -> e.containsAllOf(expected))
      .withMessage("Expected Context Context2{foo=bar, foobar=baz} to contain all " +
          "of {other=stuff, foo=bar}");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void putAllOf() {
  Context m = Context.of("A", 1, "B", 2, "C", 3);
  Context put = c.putAll(m);
  assertThat(put).isInstanceOf(ContextN.class);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactlyInAnyOrder(1, 2, 3, 4, "A", "B", "C");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void notHasSize() throws Exception {
  assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz"))
                    .subscriberContext(Context.of("fails", true)),
      e -> e.hasSize(2))
      .withMessageStartingWith("Expected Context of size 2, got 3 for Context Context3{");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void putAllOf() {
  Context m = Context.of("A", 1, "B", 2, "C", 3);
  Context put = c.putAll(m);
  assertThat(put).isInstanceOf(Context5.class)
          .hasToString("Context5{1=A, 2=B, A=1, B=2, C=3}");
}

代码示例来源:origin: reactor/reactor-core

@Test
  public void equalsIgnoresContext() {
    Signal<String> next1 = Signal.next("foo");
    Signal<String> next2 = Signal.next("foo", Context.of("bar", "baz"));

    assertThat(next1.getContext().isEmpty()).as("next1 context empty").isTrue();
    assertThat(next2.getContext().isEmpty()).as("next2 context not empty").isFalse();
    assertThat(next1).isEqualTo(next2);
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
public void monoSubscriberContextPutsAll() {
  StepVerifier.create(
      Mono.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.of("foo", "bar", 1, "baz"))
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("foo") && c.hasKey(1) && c.hasKey("initial"))
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void completeWithContextCreatesNewInstances() {
  Context context = Context.of("foo", "bar");
  assertThat(Signal.complete(context))
      .isNotSameAs(Signal.complete(context))
      .isNotSameAs(Signal.complete())
      .isEqualTo(Signal.complete())
      .isEqualTo(Signal.complete(context));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void hasSize() throws Exception {
  assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")),
      e -> e.hasSize(2));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contains() throws Exception {
  assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")),
      e -> e.contains("foo", "bar"));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void fluxSubscriberContextPutsAll() {
  StepVerifier.create(
      Flux.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.of("foo", "bar", 1, "baz"))
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("foo") && c.hasKey(1) && c.hasKey("initial"))
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void expectAccessibleContextWithInitialContext() {
  StepVerifierOptions stepVerifierOptions = StepVerifierOptions.create()
                                 .withInitialContext(Context.of("foo", "bar"));
  StepVerifier.create(Mono.just(1), stepVerifierOptions)
        .expectAccessibleContext()
        .contains("foo", "bar")
        .then()
        .expectNext(1)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
  public void contextNotAccessibleWithEmptySubscriptionOnly() {
    StepVerifier.create(Flux.empty(), StepVerifierOptions.create().withInitialContext(Context.of("a", "b")))
          .expectNoAccessibleContext()
          .verifyComplete();
  }
}

相关文章