| 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.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.mockito.Mockito; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.core.Observable; |
| 28 |
import io.reactivex.rxjava3.core.Observer; |
| 29 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 30 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 31 |
import io.reactivex.rxjava3.functions.Function; |
| 32 |
import io.reactivex.rxjava3.internal.util.CrashingIterable; |
| 33 |
import io.reactivex.rxjava3.observers.*; |
| 34 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 35 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 36 |
import io.reactivex.rxjava3.testsupport.*; |
| 37 |
|
| 38 |
public class ObservableFromIterableTest extends RxJavaTest { |
| 39 |
|
| 40 |
@Test |
| 41 |
public void listIterable() { |
| 42 |
Observable<String> o = Observable.fromIterable(Arrays.<String> asList("one", "two", "three")); |
| 43 |
|
| 44 |
Observer<String> observer = TestHelper.mockObserver(); |
| 45 |
|
| 46 |
o.subscribe(observer); |
| 47 |
|
| 48 |
verify(observer, times(1)).onNext("one"); |
| 49 |
verify(observer, times(1)).onNext("two"); |
| 50 |
verify(observer, times(1)).onNext("three"); |
| 51 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 52 |
verify(observer, times(1)).onComplete(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* This tests the path that can not optimize based on size so must use setProducer. |
| 57 |
*/ |
| 58 |
@Test |
| 59 |
public void rawIterable() { |
| 60 |
Iterable<String> it = new Iterable<String>() { |
| 61 |
|
| 62 |
@Override |
| 63 |
public Iterator<String> iterator() { |
| 64 |
return new Iterator<String>() { |
| 65 |
|
| 66 |
int i; |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean hasNext() { |
| 70 |
return i < 3; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public String next() { |
| 75 |
return String.valueOf(++i); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void remove() { |
| 80 |
} |
| 81 |
|
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
}; |
| 86 |
Observable<String> o = Observable.fromIterable(it); |
| 87 |
|
| 88 |
Observer<String> observer = TestHelper.mockObserver(); |
| 89 |
|
| 90 |
o.subscribe(observer); |
| 91 |
|
| 92 |
verify(observer, times(1)).onNext("1"); |
| 93 |
verify(observer, times(1)).onNext("2"); |
| 94 |
verify(observer, times(1)).onNext("3"); |
| 95 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 96 |
verify(observer, times(1)).onComplete(); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void observableFromIterable() { |
| 101 |
Observable<String> o = Observable.fromIterable(Arrays.<String> asList("one", "two", "three")); |
| 102 |
|
| 103 |
Observer<String> observer = TestHelper.mockObserver(); |
| 104 |
|
| 105 |
o.subscribe(observer); |
| 106 |
|
| 107 |
verify(observer, times(1)).onNext("one"); |
| 108 |
verify(observer, times(1)).onNext("two"); |
| 109 |
verify(observer, times(1)).onNext("three"); |
| 110 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 111 |
verify(observer, times(1)).onComplete(); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void noBackpressure() { |
| 116 |
Observable<Integer> o = Observable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)); |
| 117 |
|
| 118 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 119 |
|
| 120 |
o.subscribe(to); |
| 121 |
|
| 122 |
to.assertValues(1, 2, 3, 4, 5); |
| 123 |
to.assertTerminated(); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void subscribeMultipleTimes() { |
| 128 |
Observable<Integer> o = Observable.fromIterable(Arrays.asList(1, 2, 3)); |
| 129 |
|
| 130 |
for (int i = 0; i < 10; i++) { |
| 131 |
TestObserver<Integer> to = new TestObserver<>(); |
| 132 |
|
| 133 |
o.subscribe(to); |
| 134 |
|
| 135 |
to.assertValues(1, 2, 3); |
| 136 |
to.assertNoErrors(); |
| 137 |
to.assertComplete(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void doesNotCallIteratorHasNextMoreThanRequiredWithBackpressure() { |
| 143 |
final AtomicBoolean called = new AtomicBoolean(false); |
| 144 |
Iterable<Integer> iterable = new Iterable<Integer>() { |
| 145 |
|
| 146 |
@Override |
| 147 |
public Iterator<Integer> iterator() { |
| 148 |
return new Iterator<Integer>() { |
| 149 |
|
| 150 |
int count = 1; |
| 151 |
|
| 152 |
@Override |
| 153 |
public void remove() { |
| 154 |
// ignore |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public boolean hasNext() { |
| 159 |
if (count > 1) { |
| 160 |
called.set(true); |
| 161 |
return false; |
| 162 |
} |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public Integer next() { |
| 168 |
return count++; |
| 169 |
} |
| 170 |
|
| 171 |
}; |
| 172 |
} |
| 173 |
}; |
| 174 |
Observable.fromIterable(iterable).take(1).subscribe(); |
| 175 |
assertFalse(called.get()); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void doesNotCallIteratorHasNextMoreThanRequiredFastPath() { |
| 180 |
final AtomicBoolean called = new AtomicBoolean(false); |
| 181 |
Iterable<Integer> iterable = new Iterable<Integer>() { |
| 182 |
|
| 183 |
@Override |
| 184 |
public Iterator<Integer> iterator() { |
| 185 |
return new Iterator<Integer>() { |
| 186 |
|
| 187 |
@Override |
| 188 |
public void remove() { |
| 189 |
// ignore |
| 190 |
} |
| 191 |
|
| 192 |
int count = 1; |
| 193 |
|
| 194 |
@Override |
| 195 |
public boolean hasNext() { |
| 196 |
if (count > 1) { |
| 197 |
called.set(true); |
| 198 |
return false; |
| 199 |
} |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public Integer next() { |
| 205 |
return count++; |
| 206 |
} |
| 207 |
|
| 208 |
}; |
| 209 |
} |
| 210 |
}; |
| 211 |
Observable.fromIterable(iterable).subscribe(new DefaultObserver<Integer>() { |
| 212 |
|
| 213 |
@Override |
| 214 |
public void onComplete() { |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public void onError(Throwable e) { |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public void onNext(Integer t) { |
| 225 |
// unsubscribe on first emission |
| 226 |
cancel(); |
| 227 |
} |
| 228 |
}); |
| 229 |
assertFalse(called.get()); |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
public void fusionWithConcatMap() { |
| 234 |
TestObserver<Integer> to = new TestObserver<>(); |
| 235 |
|
| 236 |
Observable.fromIterable(Arrays.asList(1, 2, 3, 4)).concatMap( |
| 237 |
new Function<Integer, ObservableSource<Integer>>() { |
| 238 |
@Override |
| 239 |
public ObservableSource<Integer> apply(Integer v) { |
| 240 |
return Observable.range(v, 2); |
| 241 |
} |
| 242 |
}).subscribe(to); |
| 243 |
|
| 244 |
to.assertValues(1, 2, 2, 3, 3, 4, 4, 5); |
| 245 |
to.assertNoErrors(); |
| 246 |
to.assertComplete(); |
| 247 |
} |
| 248 |
|
| 249 |
@Test |
| 250 |
public void iteratorThrows() { |
| 251 |
Observable.fromIterable(new CrashingIterable(1, 100, 100)) |
| 252 |
.to(TestHelper.<Integer>testConsumer()) |
| 253 |
.assertFailureAndMessage(TestException.class, "iterator()"); |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void hasNext2Throws() { |
| 258 |
Observable.fromIterable(new CrashingIterable(100, 2, 100)) |
| 259 |
.to(TestHelper.<Integer>testConsumer()) |
| 260 |
.assertFailureAndMessage(TestException.class, "hasNext()", 0); |
| 261 |
} |
| 262 |
|
| 263 |
@Test |
| 264 |
public void hasNextCancels() { |
| 265 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 266 |
|
| 267 |
Observable.fromIterable(new Iterable<Integer>() { |
| 268 |
@Override |
| 269 |
public Iterator<Integer> iterator() { |
| 270 |
return new Iterator<Integer>() { |
| 271 |
int count; |
| 272 |
|
| 273 |
@Override |
| 274 |
public boolean hasNext() { |
| 275 |
if (++count == 2) { |
| 276 |
to.dispose(); |
| 277 |
} |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
@Override |
| 282 |
public Integer next() { |
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public void remove() { |
| 288 |
throw new UnsupportedOperationException(); |
| 289 |
} |
| 290 |
}; |
| 291 |
} |
| 292 |
}) |
| 293 |
.subscribe(to); |
| 294 |
|
| 295 |
to.assertValue(1) |
| 296 |
.assertNoErrors() |
| 297 |
.assertNotComplete(); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void fusionRejected() { |
| 302 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ASYNC); |
| 303 |
|
| 304 |
Observable.fromIterable(Arrays.asList(1, 2, 3)) |
| 305 |
.subscribe(to); |
| 306 |
|
| 307 |
to.assertFusionMode(QueueFuseable.NONE) |
| 308 |
.assertResult(1, 2, 3); |
| 309 |
} |
| 310 |
|
| 311 |
@Test |
| 312 |
public void fusionClear() { |
| 313 |
Observable.fromIterable(Arrays.asList(1, 2, 3)) |
| 314 |
.subscribe(new Observer<Integer>() { |
| 315 |
@Override |
| 316 |
public void onSubscribe(Disposable d) { |
| 317 |
@SuppressWarnings("unchecked") |
| 318 |
QueueDisposable<Integer> qd = (QueueDisposable<Integer>)d; |
| 319 |
|
| 320 |
qd.requestFusion(QueueFuseable.ANY); |
| 321 |
|
| 322 |
try { |
| 323 |
assertEquals(1, qd.poll().intValue()); |
| 324 |
} catch (Throwable ex) { |
| 325 |
fail(ex.toString()); |
| 326 |
} |
| 327 |
|
| 328 |
qd.clear(); |
| 329 |
try { |
| 330 |
assertNull(qd.poll()); |
| 331 |
} catch (Throwable ex) { |
| 332 |
fail(ex.toString()); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
@Override |
| 337 |
public void onNext(Integer value) { |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public void onError(Throwable e) { |
| 342 |
} |
| 343 |
|
| 344 |
@Override |
| 345 |
public void onComplete() { |
| 346 |
} |
| 347 |
}); |
| 348 |
} |
| 349 |
|
| 350 |
@Test |
| 351 |
public void disposeAfterHasNext() { |
| 352 |
TestObserver<Integer> to = new TestObserver<>(); |
| 353 |
|
| 354 |
Observable.fromIterable(() -> new Iterator<Integer>() { |
| 355 |
int count; |
| 356 |
@Override |
| 357 |
public boolean hasNext() { |
| 358 |
if (count++ == 2) { |
| 359 |
to.dispose(); |
| 360 |
return false; |
| 361 |
} |
| 362 |
return true; |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public Integer next() { |
| 367 |
return 1; |
| 368 |
} |
| 369 |
}) |
| 370 |
.subscribeWith(to) |
| 371 |
.assertValuesOnly(1, 1); |
| 372 |
} |
| 373 |
} |