| 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.internal.operators.flowable; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
import org.reactivestreams.*; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.exceptions.*; |
| 21 |
import io.reactivex.rxjava3.functions.*; |
| 22 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 23 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableMapNotification.MapNotificationSubscriber; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 25 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 26 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 27 |
import io.reactivex.rxjava3.testsupport.*; |
| 28 |
|
| 29 |
public class FlowableMapNotificationTest extends RxJavaTest { |
| 30 |
@Test |
| 31 |
public void just() { |
| 32 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 33 |
Flowable.just(1) |
| 34 |
.flatMap( |
| 35 |
new Function<Integer, Flowable<Object>>() { |
| 36 |
@Override |
| 37 |
public Flowable<Object> apply(Integer item) { |
| 38 |
return Flowable.just((Object)(item + 1)); |
| 39 |
} |
| 40 |
}, |
| 41 |
new Function<Throwable, Flowable<Object>>() { |
| 42 |
@Override |
| 43 |
public Flowable<Object> apply(Throwable e) { |
| 44 |
return Flowable.error(e); |
| 45 |
} |
| 46 |
}, |
| 47 |
new Supplier<Flowable<Object>>() { |
| 48 |
@Override |
| 49 |
public Flowable<Object> get() { |
| 50 |
return Flowable.never(); |
| 51 |
} |
| 52 |
} |
| 53 |
).subscribe(ts); |
| 54 |
|
| 55 |
ts.assertNoErrors(); |
| 56 |
ts.assertNotComplete(); |
| 57 |
ts.assertValue(2); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void backpressure() { |
| 62 |
TestSubscriber<Object> ts = TestSubscriber.create(0L); |
| 63 |
|
| 64 |
new FlowableMapNotification<>(Flowable.range(1, 3), |
| 65 |
new Function<Integer, Integer>() { |
| 66 |
@Override |
| 67 |
public Integer apply(Integer item) { |
| 68 |
return item + 1; |
| 69 |
} |
| 70 |
}, |
| 71 |
new Function<Throwable, Integer>() { |
| 72 |
@Override |
| 73 |
public Integer apply(Throwable e) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
}, |
| 77 |
new Supplier<Integer>() { |
| 78 |
@Override |
| 79 |
public Integer get() { |
| 80 |
return 5; |
| 81 |
} |
| 82 |
} |
| 83 |
).subscribe(ts); |
| 84 |
|
| 85 |
ts.assertNoValues(); |
| 86 |
ts.assertNoErrors(); |
| 87 |
ts.assertNotComplete(); |
| 88 |
|
| 89 |
ts.request(3); |
| 90 |
|
| 91 |
ts.assertValues(2, 3, 4); |
| 92 |
ts.assertNoErrors(); |
| 93 |
ts.assertNotComplete(); |
| 94 |
|
| 95 |
ts.request(1); |
| 96 |
|
| 97 |
ts.assertValues(2, 3, 4, 5); |
| 98 |
ts.assertNoErrors(); |
| 99 |
ts.assertComplete(); |
| 100 |
} |
| 101 |
|
| 102 |
@Test |
| 103 |
public void noBackpressure() { |
| 104 |
TestSubscriber<Object> ts = TestSubscriber.create(0L); |
| 105 |
|
| 106 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 107 |
|
| 108 |
new FlowableMapNotification<>(pp, |
| 109 |
new Function<Integer, Integer>() { |
| 110 |
@Override |
| 111 |
public Integer apply(Integer item) { |
| 112 |
return item + 1; |
| 113 |
} |
| 114 |
}, |
| 115 |
new Function<Throwable, Integer>() { |
| 116 |
@Override |
| 117 |
public Integer apply(Throwable e) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
}, |
| 121 |
new Supplier<Integer>() { |
| 122 |
@Override |
| 123 |
public Integer get() { |
| 124 |
return 5; |
| 125 |
} |
| 126 |
} |
| 127 |
).subscribe(ts); |
| 128 |
|
| 129 |
ts.assertNoValues(); |
| 130 |
ts.assertNoErrors(); |
| 131 |
ts.assertNotComplete(); |
| 132 |
|
| 133 |
pp.onNext(1); |
| 134 |
pp.onNext(2); |
| 135 |
pp.onNext(3); |
| 136 |
pp.onComplete(); |
| 137 |
|
| 138 |
ts.assertNoValues(); |
| 139 |
ts.assertNoErrors(); |
| 140 |
ts.assertNotComplete(); |
| 141 |
|
| 142 |
ts.request(1); |
| 143 |
|
| 144 |
ts.assertValue(0); |
| 145 |
ts.assertNoErrors(); |
| 146 |
ts.assertComplete(); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void dispose() { |
| 152 |
TestHelper.checkDisposed(new Flowable<Integer>() { |
| 153 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 154 |
@Override |
| 155 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 156 |
MapNotificationSubscriber mn = new MapNotificationSubscriber( |
| 157 |
subscriber, |
| 158 |
Functions.justFunction(Flowable.just(1)), |
| 159 |
Functions.justFunction(Flowable.just(2)), |
| 160 |
Functions.justSupplier(Flowable.just(3)) |
| 161 |
); |
| 162 |
mn.onSubscribe(new BooleanSubscription()); |
| 163 |
} |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void doubleOnSubscribe() { |
| 169 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Integer>>() { |
| 170 |
@Override |
| 171 |
public Flowable<Integer> apply(Flowable<Object> f) throws Exception { |
| 172 |
return f.flatMap( |
| 173 |
Functions.justFunction(Flowable.just(1)), |
| 174 |
Functions.justFunction(Flowable.just(2)), |
| 175 |
Functions.justSupplier(Flowable.just(3)) |
| 176 |
); |
| 177 |
} |
| 178 |
}); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void onErrorCrash() { |
| 183 |
TestSubscriberEx<Integer> ts = Flowable.<Integer>error(new TestException("Outer")) |
| 184 |
.flatMap(Functions.justFunction(Flowable.just(1)), |
| 185 |
new Function<Throwable, Publisher<Integer>>() { |
| 186 |
@Override |
| 187 |
public Publisher<Integer> apply(Throwable t) throws Exception { |
| 188 |
throw new TestException("Inner"); |
| 189 |
} |
| 190 |
}, |
| 191 |
Functions.justSupplier(Flowable.just(3))) |
| 192 |
.to(TestHelper.<Integer>testConsumer()) |
| 193 |
.assertFailure(CompositeException.class); |
| 194 |
|
| 195 |
TestHelper.assertError(ts, 0, TestException.class, "Outer"); |
| 196 |
TestHelper.assertError(ts, 1, TestException.class, "Inner"); |
| 197 |
} |
| 198 |
} |