| 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.jdk8; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.Iterator; |
| 20 |
import java.util.concurrent.atomic.*; |
| 21 |
import java.util.stream.*; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 28 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 29 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 30 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 31 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 32 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 33 |
import io.reactivex.rxjava3.testsupport.*; |
| 34 |
|
| 35 |
public class ObservableFromStreamTest extends RxJavaTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void empty() { |
| 39 |
Observable.fromStream(Stream.<Integer>of()) |
| 40 |
.test() |
| 41 |
.assertResult(); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void just() { |
| 46 |
Observable.fromStream(Stream.<Integer>of(1)) |
| 47 |
.test() |
| 48 |
.assertResult(1); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void many() { |
| 53 |
Observable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5)) |
| 54 |
.test() |
| 55 |
.assertResult(1, 2, 3, 4, 5); |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void noReuse() { |
| 60 |
Observable<Integer> source = Observable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5)); |
| 61 |
|
| 62 |
source |
| 63 |
.test() |
| 64 |
.assertResult(1, 2, 3, 4, 5); |
| 65 |
|
| 66 |
source |
| 67 |
.test() |
| 68 |
.assertFailure(IllegalStateException.class); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void take() { |
| 73 |
Observable.fromStream(IntStream.rangeClosed(1, 10).boxed()) |
| 74 |
.take(5) |
| 75 |
.test() |
| 76 |
.assertResult(1, 2, 3, 4, 5); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void emptyConditional() { |
| 81 |
Observable.fromStream(Stream.<Integer>of()) |
| 82 |
.filter(v -> true) |
| 83 |
.test() |
| 84 |
.assertResult(); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void justConditional() { |
| 89 |
Observable.fromStream(Stream.<Integer>of(1)) |
| 90 |
.filter(v -> true) |
| 91 |
.test() |
| 92 |
.assertResult(1); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void manyConditional() { |
| 97 |
Observable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5)) |
| 98 |
.filter(v -> true) |
| 99 |
.test() |
| 100 |
.assertResult(1, 2, 3, 4, 5); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void manyConditionalSkip() { |
| 105 |
Observable.fromStream(IntStream.rangeClosed(1, 10).boxed()) |
| 106 |
.filter(v -> v % 2 == 0) |
| 107 |
.test() |
| 108 |
.assertResult(2, 4, 6, 8, 10); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void takeConditional() { |
| 113 |
Observable.fromStream(IntStream.rangeClosed(1, 10).boxed()) |
| 114 |
.filter(v -> true) |
| 115 |
.take(5) |
| 116 |
.test() |
| 117 |
.assertResult(1, 2, 3, 4, 5); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void noOfferNoCrashAfterClear() throws Throwable { |
| 122 |
AtomicReference<SimpleQueue<?>> queue = new AtomicReference<>(); |
| 123 |
|
| 124 |
Observable.fromStream(IntStream.rangeClosed(1, 10).boxed()) |
| 125 |
.subscribe(new Observer<Integer>() { |
| 126 |
@Override |
| 127 |
public void onSubscribe(@NonNull Disposable d) { |
| 128 |
queue.set((SimpleQueue<?>)d); |
| 129 |
((QueueDisposable<?>)d).requestFusion(QueueFuseable.ANY); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void onNext(Integer t) { |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onError(Throwable t) { |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void onComplete() { |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
SimpleQueue<?> q = queue.get(); |
| 146 |
TestHelper.assertNoOffer(q); |
| 147 |
|
| 148 |
assertFalse(q.isEmpty()); |
| 149 |
|
| 150 |
q.clear(); |
| 151 |
|
| 152 |
assertNull(q.poll()); |
| 153 |
|
| 154 |
assertTrue(q.isEmpty()); |
| 155 |
|
| 156 |
q.clear(); |
| 157 |
|
| 158 |
assertNull(q.poll()); |
| 159 |
|
| 160 |
assertTrue(q.isEmpty()); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void fusedPoll() throws Throwable { |
| 165 |
AtomicReference<SimpleQueue<?>> queue = new AtomicReference<>(); |
| 166 |
AtomicInteger calls = new AtomicInteger(); |
| 167 |
|
| 168 |
Observable.fromStream(Stream.of(1).onClose(() -> calls.getAndIncrement())) |
| 169 |
.subscribe(new Observer<Integer>() { |
| 170 |
@Override |
| 171 |
public void onSubscribe(@NonNull Disposable d) { |
| 172 |
queue.set((SimpleQueue<?>)d); |
| 173 |
((QueueDisposable<?>)d).requestFusion(QueueFuseable.ANY); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void onNext(Integer t) { |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public void onError(Throwable t) { |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void onComplete() { |
| 186 |
} |
| 187 |
}); |
| 188 |
|
| 189 |
SimpleQueue<?> q = queue.get(); |
| 190 |
|
| 191 |
assertFalse(q.isEmpty()); |
| 192 |
|
| 193 |
assertEquals(1, q.poll()); |
| 194 |
|
| 195 |
assertTrue(q.isEmpty()); |
| 196 |
|
| 197 |
assertEquals(1, calls.get()); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void fusedPoll2() throws Throwable { |
| 202 |
AtomicReference<SimpleQueue<?>> queue = new AtomicReference<>(); |
| 203 |
AtomicInteger calls = new AtomicInteger(); |
| 204 |
|
| 205 |
Observable.fromStream(Stream.of(1, 2).onClose(() -> calls.getAndIncrement())) |
| 206 |
.subscribe(new Observer<Integer>() { |
| 207 |
@Override |
| 208 |
public void onSubscribe(@NonNull Disposable d) { |
| 209 |
queue.set((SimpleQueue<?>)d); |
| 210 |
((QueueDisposable<?>)d).requestFusion(QueueFuseable.ANY); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public void onNext(Integer t) { |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public void onError(Throwable t) { |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
public void onComplete() { |
| 223 |
} |
| 224 |
}); |
| 225 |
|
| 226 |
SimpleQueue<?> q = queue.get(); |
| 227 |
|
| 228 |
assertFalse(q.isEmpty()); |
| 229 |
|
| 230 |
assertEquals(1, q.poll()); |
| 231 |
|
| 232 |
assertFalse(q.isEmpty()); |
| 233 |
|
| 234 |
assertEquals(2, q.poll()); |
| 235 |
|
| 236 |
assertTrue(q.isEmpty()); |
| 237 |
|
| 238 |
assertEquals(1, calls.get()); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void streamOfNull() { |
| 243 |
Observable.fromStream(Stream.of((Integer)null)) |
| 244 |
.test() |
| 245 |
.assertFailure(NullPointerException.class); |
| 246 |
} |
| 247 |
|
| 248 |
@Test |
| 249 |
public void streamOfNullConditional() { |
| 250 |
Observable.fromStream(Stream.of((Integer)null)) |
| 251 |
.filter(v -> true) |
| 252 |
.test() |
| 253 |
.assertFailure(NullPointerException.class); |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void syncFusionSupport() { |
| 258 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 259 |
to.setInitialFusionMode(QueueFuseable.ANY); |
| 260 |
|
| 261 |
Observable.fromStream(IntStream.rangeClosed(1, 10).boxed()) |
| 262 |
.subscribeWith(to) |
| 263 |
.assertFuseable() |
| 264 |
.assertFusionMode(QueueFuseable.SYNC) |
| 265 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void asyncFusionNotSupported() { |
| 270 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 271 |
to.setInitialFusionMode(QueueFuseable.ASYNC); |
| 272 |
|
| 273 |
Observable.fromStream(IntStream.rangeClosed(1, 10).boxed()) |
| 274 |
.subscribeWith(to) |
| 275 |
.assertFuseable() |
| 276 |
.assertFusionMode(QueueFuseable.NONE) |
| 277 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 278 |
} |
| 279 |
|
| 280 |
@Test |
| 281 |
public void runToEndCloseCrash() throws Throwable { |
| 282 |
TestHelper.withErrorTracking(errors -> { |
| 283 |
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5).onClose(() -> { throw new TestException(); }); |
| 284 |
|
| 285 |
Observable.fromStream(stream) |
| 286 |
.test() |
| 287 |
.assertResult(1, 2, 3, 4, 5); |
| 288 |
|
| 289 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 290 |
}); |
| 291 |
} |
| 292 |
|
| 293 |
@Test |
| 294 |
public void takeCloseCrash() throws Throwable { |
| 295 |
TestHelper.withErrorTracking(errors -> { |
| 296 |
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5).onClose(() -> { throw new TestException(); }); |
| 297 |
|
| 298 |
Observable.fromStream(stream) |
| 299 |
.take(3) |
| 300 |
.test() |
| 301 |
.assertResult(1, 2, 3); |
| 302 |
|
| 303 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 304 |
}); |
| 305 |
} |
| 306 |
|
| 307 |
@Test |
| 308 |
public void hasNextCrash() { |
| 309 |
AtomicInteger v = new AtomicInteger(); |
| 310 |
Observable.fromStream(Stream.<Integer>generate(() -> { |
| 311 |
int value = v.getAndIncrement(); |
| 312 |
if (value == 1) { |
| 313 |
throw new TestException(); |
| 314 |
} |
| 315 |
return value; |
| 316 |
})) |
| 317 |
.test() |
| 318 |
.assertFailure(TestException.class, 0); |
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void hasNextCrashConditional() { |
| 323 |
AtomicInteger counter = new AtomicInteger(); |
| 324 |
Observable.fromStream(Stream.<Integer>generate(() -> { |
| 325 |
int value = counter.getAndIncrement(); |
| 326 |
if (value == 1) { |
| 327 |
throw new TestException(); |
| 328 |
} |
| 329 |
return value; |
| 330 |
})) |
| 331 |
.filter(v -> true) |
| 332 |
.test() |
| 333 |
.assertFailure(TestException.class, 0); |
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void closeCalledOnEmpty() { |
| 338 |
AtomicInteger calls = new AtomicInteger(); |
| 339 |
|
| 340 |
Observable.fromStream(Stream.of().onClose(() -> calls.getAndIncrement())) |
| 341 |
.test() |
| 342 |
.assertResult(); |
| 343 |
|
| 344 |
assertEquals(1, calls.get()); |
| 345 |
} |
| 346 |
|
| 347 |
@Test |
| 348 |
public void closeCalledAfterItems() { |
| 349 |
AtomicInteger calls = new AtomicInteger(); |
| 350 |
|
| 351 |
Observable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement())) |
| 352 |
.test() |
| 353 |
.assertResult(1, 2, 3, 4, 5); |
| 354 |
|
| 355 |
assertEquals(1, calls.get()); |
| 356 |
} |
| 357 |
|
| 358 |
@Test |
| 359 |
public void closeCalledOnCancel() { |
| 360 |
AtomicInteger calls = new AtomicInteger(); |
| 361 |
|
| 362 |
Observable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement())) |
| 363 |
.take(3) |
| 364 |
.test() |
| 365 |
.assertResult(1, 2, 3); |
| 366 |
|
| 367 |
assertEquals(1, calls.get()); |
| 368 |
} |
| 369 |
|
| 370 |
@Test |
| 371 |
public void closeCalledOnItemCrash() { |
| 372 |
AtomicInteger calls = new AtomicInteger(); |
| 373 |
AtomicInteger counter = new AtomicInteger(); |
| 374 |
Observable.fromStream(Stream.<Integer>generate(() -> { |
| 375 |
int value = counter.getAndIncrement(); |
| 376 |
if (value == 1) { |
| 377 |
throw new TestException(); |
| 378 |
} |
| 379 |
return value; |
| 380 |
}).onClose(() -> calls.getAndIncrement())) |
| 381 |
.test() |
| 382 |
.assertFailure(TestException.class, 0); |
| 383 |
|
| 384 |
assertEquals(1, calls.get()); |
| 385 |
} |
| 386 |
|
| 387 |
@Test |
| 388 |
public void closeCalledAfterItemsConditional() { |
| 389 |
AtomicInteger calls = new AtomicInteger(); |
| 390 |
|
| 391 |
Observable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement())) |
| 392 |
.filter(v -> true) |
| 393 |
.test() |
| 394 |
.assertResult(1, 2, 3, 4, 5); |
| 395 |
|
| 396 |
assertEquals(1, calls.get()); |
| 397 |
} |
| 398 |
|
| 399 |
@Test |
| 400 |
public void closeCalledOnCancelConditional() { |
| 401 |
AtomicInteger calls = new AtomicInteger(); |
| 402 |
|
| 403 |
Observable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement())) |
| 404 |
.filter(v -> true) |
| 405 |
.take(3) |
| 406 |
.test() |
| 407 |
.assertResult(1, 2, 3); |
| 408 |
|
| 409 |
assertEquals(1, calls.get()); |
| 410 |
} |
| 411 |
|
| 412 |
@Test |
| 413 |
public void closeCalledOnItemCrashConditional() { |
| 414 |
AtomicInteger calls = new AtomicInteger(); |
| 415 |
AtomicInteger counter = new AtomicInteger(); |
| 416 |
Observable.fromStream(Stream.<Integer>generate(() -> { |
| 417 |
int value = counter.getAndIncrement(); |
| 418 |
if (value == 1) { |
| 419 |
throw new TestException(); |
| 420 |
} |
| 421 |
return value; |
| 422 |
}).onClose(() -> calls.getAndIncrement())) |
| 423 |
.filter(v -> true) |
| 424 |
.test() |
| 425 |
.assertFailure(TestException.class, 0); |
| 426 |
|
| 427 |
assertEquals(1, calls.get()); |
| 428 |
} |
| 429 |
|
| 430 |
@Test |
| 431 |
public void dispose() { |
| 432 |
TestHelper.checkDisposed(Observable.fromStream(Stream.of(1))); |
| 433 |
} |
| 434 |
|
| 435 |
@Test |
| 436 |
public void cancelAfterIteratorNext() throws Exception { |
| 437 |
TestObserver<Integer> to = new TestObserver<>(); |
| 438 |
|
| 439 |
@SuppressWarnings("unchecked") |
| 440 |
Stream<Integer> stream = mock(Stream.class); |
| 441 |
when(stream.iterator()).thenReturn(new Iterator<Integer>() { |
| 442 |
|
| 443 |
@Override |
| 444 |
public boolean hasNext() { |
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
@Override |
| 449 |
public Integer next() { |
| 450 |
to.dispose(); |
| 451 |
return 1; |
| 452 |
} |
| 453 |
}); |
| 454 |
|
| 455 |
Observable.fromStream(stream) |
| 456 |
.subscribe(to); |
| 457 |
|
| 458 |
to.assertEmpty(); |
| 459 |
} |
| 460 |
|
| 461 |
@Test |
| 462 |
public void cancelAfterIteratorHasNext() throws Exception { |
| 463 |
TestObserver<Integer> to = new TestObserver<>(); |
| 464 |
|
| 465 |
@SuppressWarnings("unchecked") |
| 466 |
Stream<Integer> stream = mock(Stream.class); |
| 467 |
when(stream.iterator()).thenReturn(new Iterator<Integer>() { |
| 468 |
|
| 469 |
int calls; |
| 470 |
|
| 471 |
@Override |
| 472 |
public boolean hasNext() { |
| 473 |
if (++calls == 1) { |
| 474 |
to.dispose(); |
| 475 |
} |
| 476 |
return true; |
| 477 |
} |
| 478 |
|
| 479 |
@Override |
| 480 |
public Integer next() { |
| 481 |
return 1; |
| 482 |
} |
| 483 |
}); |
| 484 |
|
| 485 |
Observable.fromStream(stream) |
| 486 |
.subscribe(to); |
| 487 |
|
| 488 |
to.assertEmpty(); |
| 489 |
} |
| 490 |
} |