Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observable/ObservableZipTests.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.observable;
15
16 import static org.junit.Assert.assertSame;
17
18 import java.util.*;
19
20 import org.junit.*;
21
22 import io.reactivex.rxjava3.core.Observable;
23 import io.reactivex.rxjava3.core.RxJavaTest;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.observable.ObservableCovarianceTest.*;
26 import io.reactivex.rxjava3.observable.ObservableEventStream.Event;
27 import io.reactivex.rxjava3.observables.GroupedObservable;
28
29 public class ObservableZipTests extends RxJavaTest {
30
31 @Test
32 public void zipObservableOfObservables() throws Exception {
33 ObservableEventStream.getEventStream("HTTP-ClusterB", 20)
34 .groupBy(new Function<Event, String>() {
35 @Override
36 public String apply(Event e) {
37 return e.instanceId;
38 }
39 })
40 // now we have streams of cluster+instanceId
41 .flatMap(new Function<GroupedObservable<String, Event>, Observable<HashMap<String, String>>>() {
42 @Override
43 public Observable<HashMap<String, String>> apply(final GroupedObservable<String, Event> ge) {
44 return ge.scan(new HashMap<>(), new BiFunction<HashMap<String, String>, Event, HashMap<String, String>>() {
45 @Override
46 public HashMap<String, String> apply(HashMap<String, String> accum,
47 Event perInstanceEvent) {
48 synchronized (accum) {
49 accum.put("instance", ge.getKey());
50 }
51 return accum;
52 }
53 });
54 }
55 })
56 .take(10)
57 .blockingForEach(new Consumer<Object>() {
58 @Override
59 public void accept(Object pv) {
60 synchronized (pv) {
61 System.out.println(pv);
62 }
63 }
64 });
65
66 System.out.println("**** finished");
67
68 Thread.sleep(200); // make sure the event streams receive their interrupt
69 }
70
71 /**
72 * This won't compile if super/extends isn't done correctly on generics.
73 */
74 @Test
75 public void covarianceOfZip() {
76 Observable<HorrorMovie> horrors = Observable.just(new HorrorMovie());
77 Observable<CoolRating> ratings = Observable.just(new CoolRating());
78
79 Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).blockingForEach(action);
80 Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).blockingForEach(action);
81 Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).blockingForEach(extendedAction);
82 Observable.<Media, Rating, Result> zip(horrors, ratings, combine).blockingForEach(action);
83 Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).blockingForEach(action);
84
85 Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine);
86 }
87
88 /**
89 * Occasionally zip may be invoked with 0 observables. Test that we don't block indefinitely instead
90 * of immediately invoking zip with 0 argument.
91 *
92 * We now expect an NoSuchElementException since last() requires at least one value and nothing will be emitted.
93 */
94 @Test(expected = NoSuchElementException.class)
95 public void nonBlockingObservable() {
96
97 final Object invoked = new Object();
98
99 Collection<Observable<Object>> observables = Collections.emptyList();
100
101 Observable<Object> result = Observable.zip(observables, new Function<Object[], Object>() {
102 @Override
103 public Object apply(Object[] args) {
104 System.out.println("received: " + args);
105 Assert.assertEquals("No argument should have been passed", 0, args.length);
106 return invoked;
107 }
108 });
109
110 assertSame(invoked, result.blockingLast());
111 }
112
113 BiFunction<Media, Rating, ExtendedResult> combine = new BiFunction<Media, Rating, ExtendedResult>() {
114 @Override
115 public ExtendedResult apply(Media m, Rating r) {
116 return new ExtendedResult();
117 }
118 };
119
120 Consumer<Result> action = new Consumer<Result>() {
121 @Override
122 public void accept(Result t1) {
123 System.out.println("Result: " + t1);
124 }
125 };
126
127 Consumer<ExtendedResult> extendedAction = new Consumer<ExtendedResult>() {
128 @Override
129 public void accept(ExtendedResult t1) {
130 System.out.println("Result: " + t1);
131 }
132 };
133
134 @Test
135 public void zipWithDelayError() {
136 Observable.just(1)
137 .zipWith(Observable.just(2), new BiFunction<Integer, Integer, Integer>() {
138 @Override
139 public Integer apply(Integer a, Integer b) throws Exception {
140 return a + b;
141 }
142 }, true)
143 .test()
144 .assertResult(3);
145 }
146
147 @Test
148 public void zipWithDelayErrorBufferSize() {
149 Observable.just(1)
150 .zipWith(Observable.just(2), new BiFunction<Integer, Integer, Integer>() {
151 @Override
152 public Integer apply(Integer a, Integer b) throws Exception {
153 return a + b;
154 }
155 }, true, 16)
156 .test()
157 .assertResult(3);
158 }
159
160 }