@Override public StepVerifier.Assertions hasOperatorErrorMatching(Predicate<Throwable> matcher) { //noinspection ConstantConditions satisfies(() -> matcher != null, () -> "Require non-null matcher"); hasOneOperatorErrorWithError(); return satisfies( () -> matcher.test(hookRecorder.operatorErrors.peek().getT1().orElse(null)), () -> String.format( "Expected operator error matching the given predicate, did not match: <%s>.", hookRecorder.operatorErrors.peek())); }
@Override protected void onAccept(Tuple2<RedisURI, StatefulRedisConnection<String, String>> value) { if (this.closed) { value.getT2().closeAsync(); return; } synchronized (this.connections) { this.connections.put(value.getT1(), value.getT2()); } }
/** * For testing purposes. * * @param candidate the registry to use, as a plain {@link Object} to avoid leaking dependency */ FluxMetricsFuseable(Flux<? extends T> flux, @Nullable MeterRegistry candidate) { super(flux); Tuple2<String, List<Tag>> nameAndTags = resolveNameAndTags(flux); this.name = nameAndTags.getT1(); this.tags = nameAndTags.getT2(); this.registryCandidate = candidate; }
/** * For testing purposes. * * @param registryCandidate the registry to use, as a plain {@link Object} to avoid leaking dependency */ MonoMetricsFuseable(Mono<? extends T> mono, @Nullable MeterRegistry registryCandidate) { super(mono); Tuple2<String, List<Tag>> nameAndTags = FluxMetrics.resolveNameAndTags(mono); this.name = nameAndTags.getT1(); this.tags = nameAndTags.getT2(); this.registryCandidate = registryCandidate; }
@GetMapping(path = "/applications", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<ServerSentEvent<Application>> applicationsStream() { return Flux.from(eventPublisher) .flatMap(event -> registry.getInstance(event.getInstance())) .map(this::getApplicationForInstance) .flatMap(group -> toApplication(group.getT1(), group.getT2())) .map(application -> ServerSentEvent.builder(application).build()) .mergeWith(ping()); }
@Test public void fnAnyDelegate() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Function<Tuple2, Tuple2<Object, Object>> invert = t2 -> new Tuple2<>(t2.getT2(), t2.getT1()); Tuple2<Object, Object> tuple = Tuples.fnAny(invert).apply(source); assertThat(tuple.getT1()).isEqualTo(2); assertThat(tuple.getT2()).isEqualTo(1); assertThat(tuple) .isExactlyInstanceOf(Tuple2.class) .containsExactly(2, 1); }
@Test public void fn2() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Tuple2<Object, Object> tuple = Tuples.fn2().apply(source); assertThat(tuple.getT1()).isEqualTo(1); assertThat(tuple.getT2()).isEqualTo(2); assertThat(tuple) .isInstanceOf(Tuple8.class) .containsExactly(1, 2, 3, 4, 5, 6, 7, 8); }
protected Mono<Application> toApplication(String name, Flux<Instance> instances) { return instances.collectList().map(instanceList -> { Application group = new Application(name); group.setInstances(instanceList); group.setBuildVersion(getBuildVersion(instanceList)); Tuple2<String, Instant> status = getStatus(instanceList); group.setStatus(status.getT1()); group.setStatusTimestamp(status.getT2()); return group; }); }
@Test public void promiseDelays() throws Exception { Tuple2<Long, String> h = Mono.delay(Duration.ofMillis(3000)) .log("time1") .map(d -> "Spring wins") .or(Mono.delay(Duration.ofMillis(2000)).log("time2").map(d -> "Spring Reactive")) .flatMap(t -> Mono.just(t+ " world")) .elapsed() .block(); assertThat("Alternate mono not seen", h.getT2(), is("Spring Reactive world")); System.out.println(h.getT1()); }
@Test public void whenMonoJust() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void whenDelayJustMono() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void contextForLibraryReactivePutNoContext() { Mono<String> put = doPut("www.example.com", Mono.just("Walter")) .filter(t -> t.getT1() < 300) .map(Tuple2::getT2); StepVerifier.create(put) .expectNext("PUT <Walter> sent to www.example.com") .verifyComplete(); } }
@Test public void whenMonoCallable() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.fromCallable(() -> 1), Mono.fromCallable(() -> 2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void aMonoCanBeTimestamped(){ StepVerifier.withVirtualTime(this::scenario_aMonoCanBeTimestamped, 0) .thenAwait(Duration.ofSeconds(2)) .thenRequest(1) .expectNextMatches(t -> t.getT1() == 2000 && t.getT2().equals("test")) .verifyComplete(); } }
@Test public void aFluxCanBeTimestamped(){ StepVerifier.withVirtualTime(this::scenario_aFluxCanBeTimestamped, 0) .thenAwait(Duration.ofSeconds(2)) .thenRequest(1) .expectNextMatches(t -> t.getT1() == 2000 && t.getT2().equals("test")) .verifyComplete(); } }
@Test public void aFluxCanBeBenchmarked(){ StepVerifier.withVirtualTime(this::scenario_aFluxCanBeBenchmarked,0) .thenAwait(Duration.ofSeconds(2)) .thenRequest(1) .expectNextMatches(t -> t.getT1() == 2000 && t.getT2().equals("test")) .verifyComplete(); }
@Test public void aFluxCanBeBenchmarked(){ StepVerifier.withVirtualTime(this::scenario_aFluxCanBeBenchmarked,0) .thenAwait(Duration.ofSeconds(2)) .thenRequest(1) .expectNextMatches(t -> t.getT1() == 2000 && t.getT2().equals("test")) .verifyComplete(); }
@Test public void mapT2() { Tuple2<Integer, String> base = Tuples.of(100, "Foo"); Tuple2<?,?> mapped = base.mapT2(String::length); assertThat(mapped).isNotSameAs(base) .hasSize(2) .containsExactly(base.getT1(), 3); }
@Test public void testMonoAndFunction() { StepVerifier.create(Mono.just("source") .zipWhen(t -> handle(t))) .expectNextMatches(pair -> pair.getT1().equals("source") && pair.getT2() == 6) .expectComplete() .verify(); }
@Test public void contextForLibraryReactivePut() { Mono<String> put = doPut("www.example.com", Mono.just("Walter")) .subscriberContext(Context.of(HTTP_CORRELATION_ID, "2-j3r9afaf92j-afkaf")) .filter(t -> t.getT1() < 300) .map(Tuple2::getT2); StepVerifier.create(put) .expectNext("PUT <Walter> sent to www.example.com with header X-Correlation-ID = 2-j3r9afaf92j-afkaf") .verifyComplete(); }