| 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 static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.Subscription; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.observers.DisposableCompletableObserver; |
| 27 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 28 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 29 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 30 |
import io.reactivex.rxjava3.subscribers.*; |
| 31 |
import io.reactivex.rxjava3.testsupport.*; |
| 32 |
|
| 33 |
public class FlowableIgnoreElementsTest extends RxJavaTest { |
| 34 |
|
| 35 |
@Test |
| 36 |
public void withEmptyFlowable() { |
| 37 |
assertTrue(Flowable.empty().ignoreElements().toFlowable().isEmpty().blockingGet()); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void withNonEmptyFlowable() { |
| 42 |
assertTrue(Flowable.just(1, 2, 3).ignoreElements().toFlowable().isEmpty().blockingGet()); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void upstreamIsProcessedButIgnoredFlowable() { |
| 47 |
final int num = 10; |
| 48 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 49 |
long count = Flowable.range(1, num) |
| 50 |
.doOnNext(new Consumer<Integer>() { |
| 51 |
@Override |
| 52 |
public void accept(Integer t) { |
| 53 |
upstreamCount.incrementAndGet(); |
| 54 |
} |
| 55 |
}) |
| 56 |
.ignoreElements() |
| 57 |
.toFlowable() |
| 58 |
.count().blockingGet(); |
| 59 |
assertEquals(num, upstreamCount.get()); |
| 60 |
assertEquals(0, count); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void completedOkFlowable() { |
| 65 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 66 |
Flowable.range(1, 10).ignoreElements().toFlowable().subscribe(ts); |
| 67 |
ts.assertNoErrors(); |
| 68 |
ts.assertNoValues(); |
| 69 |
ts.assertTerminated(); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void errorReceivedFlowable() { |
| 74 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 75 |
TestException ex = new TestException("boo"); |
| 76 |
Flowable.error(ex).ignoreElements().toFlowable().subscribe(ts); |
| 77 |
ts.assertNoValues(); |
| 78 |
ts.assertTerminated(); |
| 79 |
ts.assertError(TestException.class); |
| 80 |
ts.assertErrorMessage("boo"); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void unsubscribesFromUpstreamFlowable() { |
| 85 |
final AtomicBoolean unsub = new AtomicBoolean(); |
| 86 |
Flowable.range(1, 10).concatWith(Flowable.<Integer>never()) |
| 87 |
.doOnCancel(new Action() { |
| 88 |
@Override |
| 89 |
public void run() { |
| 90 |
unsub.set(true); |
| 91 |
}}) |
| 92 |
.ignoreElements() |
| 93 |
.toFlowable() |
| 94 |
.subscribe().dispose(); |
| 95 |
|
| 96 |
assertTrue(unsub.get()); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void doesNotHangAndProcessesAllUsingBackpressureFlowable() { |
| 101 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 102 |
final AtomicInteger count = new AtomicInteger(0); |
| 103 |
int num = 10; |
| 104 |
Flowable.range(1, num) |
| 105 |
// |
| 106 |
.doOnNext(new Consumer<Integer>() { |
| 107 |
@Override |
| 108 |
public void accept(Integer t) { |
| 109 |
upstreamCount.incrementAndGet(); |
| 110 |
} |
| 111 |
}) |
| 112 |
// |
| 113 |
.ignoreElements() |
| 114 |
.<Integer>toFlowable() |
| 115 |
// |
| 116 |
.doOnNext(new Consumer<Integer>() { |
| 117 |
|
| 118 |
@Override |
| 119 |
public void accept(Integer t) { |
| 120 |
upstreamCount.incrementAndGet(); |
| 121 |
} |
| 122 |
}) |
| 123 |
// |
| 124 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 125 |
|
| 126 |
@Override |
| 127 |
public void onStart() { |
| 128 |
request(1); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void onComplete() { |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onError(Throwable e) { |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void onNext(Integer t) { |
| 142 |
count.incrementAndGet(); |
| 143 |
} |
| 144 |
}); |
| 145 |
assertEquals(num, upstreamCount.get()); |
| 146 |
assertEquals(0, count.get()); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void withEmpty() { |
| 151 |
Flowable.empty().ignoreElements().blockingAwait(); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void withNonEmpty() { |
| 156 |
Flowable.just(1, 2, 3).ignoreElements().blockingAwait(); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void upstreamIsProcessedButIgnored() { |
| 161 |
final int num = 10; |
| 162 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 163 |
Flowable.range(1, num) |
| 164 |
.doOnNext(new Consumer<Integer>() { |
| 165 |
@Override |
| 166 |
public void accept(Integer t) { |
| 167 |
upstreamCount.incrementAndGet(); |
| 168 |
} |
| 169 |
}) |
| 170 |
.ignoreElements() |
| 171 |
.blockingAwait(); |
| 172 |
assertEquals(num, upstreamCount.get()); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void completedOk() { |
| 177 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 178 |
Flowable.range(1, 10).ignoreElements().subscribe(to); |
| 179 |
to.assertNoErrors(); |
| 180 |
to.assertNoValues(); |
| 181 |
to.assertTerminated(); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void errorReceived() { |
| 186 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 187 |
TestException ex = new TestException("boo"); |
| 188 |
Flowable.error(ex).ignoreElements().subscribe(to); |
| 189 |
to.assertNoValues(); |
| 190 |
to.assertTerminated(); |
| 191 |
to.assertError(TestException.class); |
| 192 |
to.assertErrorMessage("boo"); |
| 193 |
} |
| 194 |
|
| 195 |
@Test |
| 196 |
public void unsubscribesFromUpstream() { |
| 197 |
final AtomicBoolean unsub = new AtomicBoolean(); |
| 198 |
Flowable.range(1, 10).concatWith(Flowable.<Integer>never()) |
| 199 |
.doOnCancel(new Action() { |
| 200 |
@Override |
| 201 |
public void run() { |
| 202 |
unsub.set(true); |
| 203 |
}}) |
| 204 |
.ignoreElements() |
| 205 |
.subscribe().dispose(); |
| 206 |
|
| 207 |
assertTrue(unsub.get()); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void doesNotHangAndProcessesAllUsingBackpressure() { |
| 212 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 213 |
final AtomicInteger count = new AtomicInteger(0); |
| 214 |
int num = 10; |
| 215 |
Flowable.range(1, num) |
| 216 |
// |
| 217 |
.doOnNext(new Consumer<Integer>() { |
| 218 |
@Override |
| 219 |
public void accept(Integer t) { |
| 220 |
upstreamCount.incrementAndGet(); |
| 221 |
} |
| 222 |
}) |
| 223 |
// |
| 224 |
.ignoreElements() |
| 225 |
// |
| 226 |
.subscribe(new DisposableCompletableObserver() { |
| 227 |
@Override |
| 228 |
public void onComplete() { |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public void onError(Throwable e) { |
| 233 |
} |
| 234 |
}); |
| 235 |
assertEquals(num, upstreamCount.get()); |
| 236 |
assertEquals(0, count.get()); |
| 237 |
} |
| 238 |
|
| 239 |
@Test |
| 240 |
public void cancel() { |
| 241 |
|
| 242 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 243 |
|
| 244 |
TestSubscriber<Integer> ts = pp.ignoreElements().<Integer>toFlowable().test(); |
| 245 |
|
| 246 |
assertTrue(pp.hasSubscribers()); |
| 247 |
|
| 248 |
ts.cancel(); |
| 249 |
|
| 250 |
assertFalse(pp.hasSubscribers()); |
| 251 |
} |
| 252 |
|
| 253 |
@Test |
| 254 |
public void fused() { |
| 255 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 256 |
|
| 257 |
Flowable.just(1).hide().ignoreElements().<Integer>toFlowable() |
| 258 |
.subscribe(ts); |
| 259 |
|
| 260 |
ts.assertFuseable() |
| 261 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 262 |
.assertResult(); |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void fusedAPICalls() { |
| 267 |
Flowable.just(1).hide().ignoreElements().<Integer>toFlowable() |
| 268 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 269 |
|
| 270 |
@Override |
| 271 |
public void onSubscribe(Subscription s) { |
| 272 |
@SuppressWarnings("unchecked") |
| 273 |
QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s; |
| 274 |
|
| 275 |
try { |
| 276 |
assertNull(qs.poll()); |
| 277 |
} catch (Throwable ex) { |
| 278 |
throw new AssertionError(ex); |
| 279 |
} |
| 280 |
|
| 281 |
assertTrue(qs.isEmpty()); |
| 282 |
|
| 283 |
qs.clear(); |
| 284 |
|
| 285 |
assertTrue(qs.isEmpty()); |
| 286 |
|
| 287 |
try { |
| 288 |
assertNull(qs.poll()); |
| 289 |
} catch (Throwable ex) { |
| 290 |
throw new AssertionError(ex); |
| 291 |
} |
| 292 |
|
| 293 |
try { |
| 294 |
qs.offer(1); |
| 295 |
fail("Should have thrown!"); |
| 296 |
} catch (UnsupportedOperationException ex) { |
| 297 |
// expected |
| 298 |
} |
| 299 |
|
| 300 |
try { |
| 301 |
qs.offer(1, 2); |
| 302 |
fail("Should have thrown!"); |
| 303 |
} catch (UnsupportedOperationException ex) { |
| 304 |
// expected |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void onNext(Integer t) { |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public void onError(Throwable t) { |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public void onComplete() { |
| 318 |
} |
| 319 |
}); |
| 320 |
} |
| 321 |
|
| 322 |
@Test |
| 323 |
public void dispose() { |
| 324 |
TestHelper.checkDisposed(Flowable.just(1).ignoreElements()); |
| 325 |
|
| 326 |
TestHelper.checkDisposed(Flowable.just(1).ignoreElements().toFlowable()); |
| 327 |
} |
| 328 |
|
| 329 |
@Test |
| 330 |
public void doubleOnSubscribe() { |
| 331 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 332 |
@Override |
| 333 |
public Flowable<Object> apply(Flowable<Object> f) |
| 334 |
throws Exception { |
| 335 |
return f.ignoreElements().toFlowable(); |
| 336 |
} |
| 337 |
}); |
| 338 |
|
| 339 |
TestHelper.checkDoubleOnSubscribeFlowableToCompletable(new Function<Flowable<Object>, Completable>() { |
| 340 |
@Override |
| 341 |
public Completable apply(Flowable<Object> f) |
| 342 |
throws Exception { |
| 343 |
return f.ignoreElements(); |
| 344 |
} |
| 345 |
}); |
| 346 |
} |
| 347 |
} |