| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 27 |
import io.reactivex.rxjava3.testsupport.*; |
| 28 |
|
| 29 |
public class ObservableIgnoreElementsTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void withEmptyObservable() { |
| 33 |
assertTrue(Observable.empty().ignoreElements().toObservable().isEmpty().blockingGet()); |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
public void withNonEmptyObservable() { |
| 38 |
assertTrue(Observable.just(1, 2, 3).ignoreElements().toObservable().isEmpty().blockingGet()); |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
public void upstreamIsProcessedButIgnoredObservable() { |
| 43 |
final int num = 10; |
| 44 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 45 |
long count = Observable.range(1, num) |
| 46 |
.doOnNext(new Consumer<Integer>() { |
| 47 |
@Override |
| 48 |
public void accept(Integer t) { |
| 49 |
upstreamCount.incrementAndGet(); |
| 50 |
} |
| 51 |
}) |
| 52 |
.ignoreElements() |
| 53 |
.toObservable() |
| 54 |
.count().blockingGet(); |
| 55 |
assertEquals(num, upstreamCount.get()); |
| 56 |
assertEquals(0, count); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void completedOkObservable() { |
| 61 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 62 |
Observable.range(1, 10).ignoreElements().toObservable().subscribe(to); |
| 63 |
to.assertNoErrors(); |
| 64 |
to.assertNoValues(); |
| 65 |
to.assertTerminated(); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void errorReceivedObservable() { |
| 70 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 71 |
TestException ex = new TestException("boo"); |
| 72 |
Observable.error(ex).ignoreElements().toObservable().subscribe(to); |
| 73 |
to.assertNoValues(); |
| 74 |
to.assertTerminated(); |
| 75 |
to.assertError(TestException.class); |
| 76 |
to.assertErrorMessage("boo"); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void unsubscribesFromUpstreamObservable() { |
| 81 |
final AtomicBoolean unsub = new AtomicBoolean(); |
| 82 |
Observable.range(1, 10).concatWith(Observable.<Integer>never()) |
| 83 |
.doOnDispose(new Action() { |
| 84 |
@Override |
| 85 |
public void run() { |
| 86 |
unsub.set(true); |
| 87 |
}}) |
| 88 |
.ignoreElements() |
| 89 |
.toObservable() |
| 90 |
.subscribe() |
| 91 |
.dispose(); |
| 92 |
assertTrue(unsub.get()); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void withEmpty() { |
| 97 |
Observable.empty().ignoreElements().blockingAwait(); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void withNonEmpty() { |
| 102 |
Observable.just(1, 2, 3).ignoreElements().blockingAwait(); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void upstreamIsProcessedButIgnored() { |
| 107 |
final int num = 10; |
| 108 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 109 |
Observable.range(1, num) |
| 110 |
.doOnNext(new Consumer<Integer>() { |
| 111 |
@Override |
| 112 |
public void accept(Integer t) { |
| 113 |
upstreamCount.incrementAndGet(); |
| 114 |
} |
| 115 |
}) |
| 116 |
.ignoreElements() |
| 117 |
.blockingAwait(); |
| 118 |
assertEquals(num, upstreamCount.get()); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void completedOk() { |
| 123 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 124 |
Observable.range(1, 10).ignoreElements().subscribe(to); |
| 125 |
to.assertNoErrors(); |
| 126 |
to.assertNoValues(); |
| 127 |
to.assertTerminated(); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void errorReceived() { |
| 132 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 133 |
TestException ex = new TestException("boo"); |
| 134 |
Observable.error(ex).ignoreElements().subscribe(to); |
| 135 |
to.assertNoValues(); |
| 136 |
to.assertTerminated(); |
| 137 |
to.assertError(TestException.class); |
| 138 |
to.assertErrorMessage("boo"); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void unsubscribesFromUpstream() { |
| 143 |
final AtomicBoolean unsub = new AtomicBoolean(); |
| 144 |
Observable.range(1, 10).concatWith(Observable.<Integer>never()) |
| 145 |
.doOnDispose(new Action() { |
| 146 |
@Override |
| 147 |
public void run() { |
| 148 |
unsub.set(true); |
| 149 |
}}) |
| 150 |
.ignoreElements() |
| 151 |
.subscribe() |
| 152 |
.dispose(); |
| 153 |
assertTrue(unsub.get()); |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void dispose() { |
| 158 |
|
| 159 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 160 |
|
| 161 |
TestObserver<Integer> to = ps.ignoreElements().<Integer>toObservable().test(); |
| 162 |
|
| 163 |
assertTrue(ps.hasObservers()); |
| 164 |
|
| 165 |
to.dispose(); |
| 166 |
|
| 167 |
assertFalse(ps.hasObservers()); |
| 168 |
|
| 169 |
TestHelper.checkDisposed(ps.ignoreElements().<Integer>toObservable()); |
| 170 |
} |
| 171 |
|
| 172 |
@Test |
| 173 |
public void checkDispose() { |
| 174 |
TestHelper.checkDisposed(Observable.just(1).ignoreElements()); |
| 175 |
|
| 176 |
TestHelper.checkDisposed(Observable.just(1).ignoreElements().toObservable()); |
| 177 |
} |
| 178 |
} |