/
githubmirror
/
RxJava
Обзор
Документация
Войти
/
githubmirror
/
RxJava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
4.x
src/test/java/io/reactivex/rxjava4/flowable/FlowableZipTests.java
114 строк
4 KB
David Karnok
4.x: Streamable + takeUntil + groupBy + refactor + helpers (#8215)
05 июл 2026, 01:45
Не верифицирован
05 июл 2026, 01:45
580b39c
Код
Авторство
О чём код?
/* * Copyright (c) 2016-present, RxJava Contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See * the License for the specific language governing permissions and limitations under the License. */ package io.reactivex.rxjava4.flowable; import static org.junit.jupiter.api.Assertions.*; import java.util.*; import java.util.concurrent.Flow.Publisher; import org.junit.jupiter.api.Test; import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.core.config.StandardBufferedConfig; import io.reactivex.rxjava4.flowable.FlowableCovarianceTest.*; import io.reactivex.rxjava4.flowable.FlowableEventStream.Event; import io.reactivex.rxjava4.functions.*; public class FlowableZipTests extends RxJavaTest { @Test public void zipObservableOfObservables() { FlowableEventStream.getEventStream("HTTP-ClusterB", 20) .groupBy(Event::instanceId) // now we have streams of cluster+instanceId .flatMap((Function<GroupedFlowable<String, Event>, Publisher<HashMap<String, String>>>) ge -> ge.scan(new HashMap<>(), (accum, _) -> { synchronized (accum) { accum.put("instance", ge.getKey()); } return accum; })) .take(10) .blockingForEach(v -> { synchronized (v) { System.out.println(v); } }); System.out.println("**** finished"); } /** * This won't compile if super/extends isn't done correctly on generics. */ @Test public void covarianceOfZip() { Flowable<HorrorMovie> horrors = Flowable.just(new HorrorMovie()); Flowable<CoolRating> ratings = Flowable.just(new CoolRating()); Flowable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).blockingForEach(action); Flowable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).blockingForEach(action); Flowable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).blockingForEach(extendedAction); Flowable.<Media, Rating, Result> zip(horrors, ratings, combine).blockingForEach(action); Flowable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).blockingForEach(action); Flowable.<Movie, CoolRating, Result> zip(horrors, ratings, combine); } /** * Occasionally zip may be invoked with 0 observables. Test that we don't block indefinitely instead * of immediately invoking zip with 0 argument. * * We now expect an NoSuchElementException since last() requires at least one value and nothing will be emitted. */ @Test public void nonBlockingObservable() { assertThrows(NoSuchElementException.class, () -> { final Object invoked = new Object(); Collection<Flowable<Object>> observables = Collections.emptyList(); Flowable<Object> result = Flowable.zip(observables, args -> { System.out.println("received: " + args); assertEquals(0, args.length, "No argument should have been passed"); return invoked; }); assertSame(invoked, result.blockingLast()); }); } BiFunction<Media, Rating, ExtendedResult> combine = (_, _) -> new ExtendedResult(); Consumer<Result> action = t1 -> System.out.println("Result: " + t1); Consumer<ExtendedResult> extendedAction = t1 -> System.out.println("Result: " + t1); @Test public void zipWithDelayError() { Flowable.just(1) .zipWith(Flowable.just(2), Integer::sum, new StandardBufferedConfig(true)) .test() .assertResult(3); } @Test public void zipWithDelayErrorBufferSize() { Flowable.just(1) .zipWith(Flowable.just(2), Integer::sum, new StandardBufferedConfig(true, 16)) .test() .assertResult(3); } }