| 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 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.mockito.InOrder; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.Observable; |
| 26 |
import io.reactivex.rxjava3.core.Observer; |
| 27 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 28 |
import io.reactivex.rxjava3.disposables.*; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.functions.*; |
| 31 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 32 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 33 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 34 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 35 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 36 |
import io.reactivex.rxjava3.subjects.UnicastSubject; |
| 37 |
import io.reactivex.rxjava3.testsupport.*; |
| 38 |
|
| 39 |
public class ObservableDistinctTest extends RxJavaTest { |
| 40 |
|
| 41 |
Observer<String> w; |
| 42 |
|
| 43 |
// nulls lead to exceptions |
| 44 |
final Function<String, String> TO_UPPER_WITH_EXCEPTION = new Function<String, String>() { |
| 45 |
@Override |
| 46 |
public String apply(String s) { |
| 47 |
if (s.equals("x")) { |
| 48 |
return "XX"; |
| 49 |
} |
| 50 |
return s.toUpperCase(); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
@Before |
| 55 |
public void before() { |
| 56 |
w = TestHelper.mockObserver(); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void distinctOfNone() { |
| 61 |
Observable<String> src = Observable.empty(); |
| 62 |
src.distinct().subscribe(w); |
| 63 |
|
| 64 |
verify(w, never()).onNext(anyString()); |
| 65 |
verify(w, never()).onError(any(Throwable.class)); |
| 66 |
verify(w, times(1)).onComplete(); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void distinctOfNoneWithKeySelector() { |
| 71 |
Observable<String> src = Observable.empty(); |
| 72 |
src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w); |
| 73 |
|
| 74 |
verify(w, never()).onNext(anyString()); |
| 75 |
verify(w, never()).onError(any(Throwable.class)); |
| 76 |
verify(w, times(1)).onComplete(); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void distinctOfNormalSource() { |
| 81 |
Observable<String> src = Observable.just("a", "b", "c", "c", "c", "b", "b", "a", "e"); |
| 82 |
src.distinct().subscribe(w); |
| 83 |
|
| 84 |
InOrder inOrder = inOrder(w); |
| 85 |
inOrder.verify(w, times(1)).onNext("a"); |
| 86 |
inOrder.verify(w, times(1)).onNext("b"); |
| 87 |
inOrder.verify(w, times(1)).onNext("c"); |
| 88 |
inOrder.verify(w, times(1)).onNext("e"); |
| 89 |
inOrder.verify(w, times(1)).onComplete(); |
| 90 |
inOrder.verify(w, never()).onNext(anyString()); |
| 91 |
verify(w, never()).onError(any(Throwable.class)); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void distinctOfNormalSourceWithKeySelector() { |
| 96 |
Observable<String> src = Observable.just("a", "B", "c", "C", "c", "B", "b", "a", "E"); |
| 97 |
src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w); |
| 98 |
|
| 99 |
InOrder inOrder = inOrder(w); |
| 100 |
inOrder.verify(w, times(1)).onNext("a"); |
| 101 |
inOrder.verify(w, times(1)).onNext("B"); |
| 102 |
inOrder.verify(w, times(1)).onNext("c"); |
| 103 |
inOrder.verify(w, times(1)).onNext("E"); |
| 104 |
inOrder.verify(w, times(1)).onComplete(); |
| 105 |
inOrder.verify(w, never()).onNext(anyString()); |
| 106 |
verify(w, never()).onError(any(Throwable.class)); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void error() { |
| 111 |
Observable.error(new TestException()) |
| 112 |
.distinct() |
| 113 |
.test() |
| 114 |
.assertFailure(TestException.class); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void fusedSync() { |
| 119 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 120 |
|
| 121 |
Observable.just(1, 1, 2, 1, 3, 2, 4, 5, 4) |
| 122 |
.distinct() |
| 123 |
.subscribe(to); |
| 124 |
|
| 125 |
to.assertFusionMode(QueueFuseable.SYNC) |
| 126 |
.assertResult(1, 2, 3, 4, 5); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void fusedAsync() { |
| 131 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 132 |
|
| 133 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 134 |
|
| 135 |
us |
| 136 |
.distinct() |
| 137 |
.subscribe(to); |
| 138 |
|
| 139 |
TestHelper.emit(us, 1, 1, 2, 1, 3, 2, 4, 5, 4); |
| 140 |
|
| 141 |
to.assertFusionMode(QueueFuseable.ASYNC) |
| 142 |
.assertResult(1, 2, 3, 4, 5); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void fusedClear() { |
| 147 |
Observable.just(1, 1, 2, 1, 3, 2, 4, 5, 4) |
| 148 |
.distinct() |
| 149 |
.subscribe(new Observer<Integer>() { |
| 150 |
@Override |
| 151 |
public void onSubscribe(Disposable d) { |
| 152 |
QueueDisposable<?> qd = (QueueDisposable<?>)d; |
| 153 |
|
| 154 |
assertFalse(qd.isEmpty()); |
| 155 |
|
| 156 |
qd.clear(); |
| 157 |
|
| 158 |
assertTrue(qd.isEmpty()); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void onNext(Integer value) { |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void onError(Throwable e) { |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void onComplete() { |
| 171 |
} |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void collectionSupplierThrows() { |
| 177 |
Observable.just(1) |
| 178 |
.distinct(Functions.identity(), new Supplier<Collection<Object>>() { |
| 179 |
@Override |
| 180 |
public Collection<Object> get() throws Exception { |
| 181 |
throw new TestException(); |
| 182 |
} |
| 183 |
}) |
| 184 |
.test() |
| 185 |
.assertFailure(TestException.class); |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void collectionSupplierIsNull() { |
| 190 |
Observable.just(1) |
| 191 |
.distinct(Functions.identity(), new Supplier<Collection<Object>>() { |
| 192 |
@Override |
| 193 |
public Collection<Object> get() throws Exception { |
| 194 |
return null; |
| 195 |
} |
| 196 |
}) |
| 197 |
.to(TestHelper.<Integer>testConsumer()) |
| 198 |
.assertFailure(NullPointerException.class) |
| 199 |
.assertErrorMessage(ExceptionHelper.nullWarning("The collectionSupplier returned a null Collection.")); |
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void badSource() { |
| 204 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 205 |
try { |
| 206 |
new Observable<Integer>() { |
| 207 |
@Override |
| 208 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 209 |
observer.onSubscribe(Disposable.empty()); |
| 210 |
|
| 211 |
observer.onNext(1); |
| 212 |
observer.onComplete(); |
| 213 |
observer.onNext(2); |
| 214 |
observer.onError(new TestException()); |
| 215 |
observer.onComplete(); |
| 216 |
} |
| 217 |
} |
| 218 |
.distinct() |
| 219 |
.test() |
| 220 |
.assertResult(1); |
| 221 |
|
| 222 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 223 |
} finally { |
| 224 |
RxJavaPlugins.reset(); |
| 225 |
} |
| 226 |
} |
| 227 |
} |