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