| 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.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.Publisher; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.flowable.FlowableCovarianceTest.*; |
| 25 |
import io.reactivex.rxjava3.flowable.FlowableEventStream.Event; |
| 26 |
import io.reactivex.rxjava3.flowables.GroupedFlowable; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
|
| 29 |
public class FlowableZipTests extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void zipObservableOfObservables() { |
| 33 |
FlowableEventStream.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<GroupedFlowable<String, Event>, Publisher<HashMap<String, String>>>() { |
| 42 |
@Override |
| 43 |
public Publisher<HashMap<String, String>> apply(final GroupedFlowable<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<HashMap<String, String>>() { |
| 58 |
@Override |
| 59 |
public void accept(HashMap<String, String> v) { |
| 60 |
synchronized (v) { |
| 61 |
System.out.println(v); |
| 62 |
} |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
System.out.println("**** finished"); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* This won't compile if super/extends isn't done correctly on generics. |
| 71 |
*/ |
| 72 |
@Test |
| 73 |
public void covarianceOfZip() { |
| 74 |
Flowable<HorrorMovie> horrors = Flowable.just(new HorrorMovie()); |
| 75 |
Flowable<CoolRating> ratings = Flowable.just(new CoolRating()); |
| 76 |
|
| 77 |
Flowable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).blockingForEach(action); |
| 78 |
Flowable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).blockingForEach(action); |
| 79 |
Flowable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).blockingForEach(extendedAction); |
| 80 |
Flowable.<Media, Rating, Result> zip(horrors, ratings, combine).blockingForEach(action); |
| 81 |
Flowable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).blockingForEach(action); |
| 82 |
|
| 83 |
Flowable.<Movie, CoolRating, Result> zip(horrors, ratings, combine); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Occasionally zip may be invoked with 0 observables. Test that we don't block indefinitely instead |
| 88 |
* of immediately invoking zip with 0 argument. |
| 89 |
* |
| 90 |
* We now expect an NoSuchElementException since last() requires at least one value and nothing will be emitted. |
| 91 |
*/ |
| 92 |
@Test(expected = NoSuchElementException.class) |
| 93 |
public void nonBlockingObservable() { |
| 94 |
|
| 95 |
final Object invoked = new Object(); |
| 96 |
|
| 97 |
Collection<Flowable<Object>> observables = Collections.emptyList(); |
| 98 |
|
| 99 |
Flowable<Object> result = Flowable.zip(observables, new Function<Object[], Object>() { |
| 100 |
@Override |
| 101 |
public Object apply(Object[] args) { |
| 102 |
System.out.println("received: " + args); |
| 103 |
assertEquals("No argument should have been passed", 0, args.length); |
| 104 |
return invoked; |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
assertSame(invoked, result.blockingLast()); |
| 109 |
} |
| 110 |
|
| 111 |
BiFunction<Media, Rating, ExtendedResult> combine = new BiFunction<Media, Rating, ExtendedResult>() { |
| 112 |
@Override |
| 113 |
public ExtendedResult apply(Media m, Rating r) { |
| 114 |
return new ExtendedResult(); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
Consumer<Result> action = new Consumer<Result>() { |
| 119 |
@Override |
| 120 |
public void accept(Result t1) { |
| 121 |
System.out.println("Result: " + t1); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
Consumer<ExtendedResult> extendedAction = new Consumer<ExtendedResult>() { |
| 126 |
@Override |
| 127 |
public void accept(ExtendedResult t1) { |
| 128 |
System.out.println("Result: " + t1); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
@Test |
| 133 |
public void zipWithDelayError() { |
| 134 |
Flowable.just(1) |
| 135 |
.zipWith(Flowable.just(2), new BiFunction<Integer, Integer, Integer>() { |
| 136 |
@Override |
| 137 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 138 |
return a + b; |
| 139 |
} |
| 140 |
}, true) |
| 141 |
.test() |
| 142 |
.assertResult(3); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void zipWithDelayErrorBufferSize() { |
| 147 |
Flowable.just(1) |
| 148 |
.zipWith(Flowable.just(2), new BiFunction<Integer, Integer, Integer>() { |
| 149 |
@Override |
| 150 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 151 |
return a + b; |
| 152 |
} |
| 153 |
}, true, 16) |
| 154 |
.test() |
| 155 |
.assertResult(3); |
| 156 |
} |
| 157 |
} |