| 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 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.*; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.processors.*; |
| 26 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 27 |
|
| 28 |
public class BlockingFlowableMostRecentTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void mostRecent() { |
| 32 |
FlowableProcessor<String> s = PublishProcessor.create(); |
| 33 |
|
| 34 |
Iterator<String> it = s.blockingMostRecent("default").iterator(); |
| 35 |
|
| 36 |
assertTrue(it.hasNext()); |
| 37 |
assertEquals("default", it.next()); |
| 38 |
assertEquals("default", it.next()); |
| 39 |
|
| 40 |
s.onNext("one"); |
| 41 |
assertTrue(it.hasNext()); |
| 42 |
assertEquals("one", it.next()); |
| 43 |
assertEquals("one", it.next()); |
| 44 |
|
| 45 |
s.onNext("two"); |
| 46 |
assertTrue(it.hasNext()); |
| 47 |
assertEquals("two", it.next()); |
| 48 |
assertEquals("two", it.next()); |
| 49 |
|
| 50 |
s.onComplete(); |
| 51 |
assertFalse(it.hasNext()); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
@Test(expected = TestException.class) |
| 56 |
public void mostRecentWithException() { |
| 57 |
FlowableProcessor<String> s = PublishProcessor.create(); |
| 58 |
|
| 59 |
Iterator<String> it = s.blockingMostRecent("default").iterator(); |
| 60 |
|
| 61 |
assertTrue(it.hasNext()); |
| 62 |
assertEquals("default", it.next()); |
| 63 |
assertEquals("default", it.next()); |
| 64 |
|
| 65 |
s.onError(new TestException()); |
| 66 |
assertTrue(it.hasNext()); |
| 67 |
|
| 68 |
it.next(); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void singleSourceManyIterators() { |
| 73 |
TestScheduler scheduler = new TestScheduler(); |
| 74 |
Flowable<Long> source = Flowable.interval(1, TimeUnit.SECONDS, scheduler).take(10); |
| 75 |
|
| 76 |
Iterable<Long> iter = source.blockingMostRecent(-1L); |
| 77 |
|
| 78 |
for (int j = 0; j < 3; j++) { |
| 79 |
Iterator<Long> it = iter.iterator(); |
| 80 |
|
| 81 |
Assert.assertEquals(Long.valueOf(-1), it.next()); |
| 82 |
|
| 83 |
for (int i = 0; i < 9; i++) { |
| 84 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 85 |
|
| 86 |
Assert.assertTrue(it.hasNext()); |
| 87 |
Assert.assertEquals(Long.valueOf(i), it.next()); |
| 88 |
} |
| 89 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 90 |
|
| 91 |
Assert.assertFalse(it.hasNext()); |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void empty() { |
| 98 |
Iterator<Integer> it = Flowable.<Integer>empty() |
| 99 |
.blockingMostRecent(1) |
| 100 |
.iterator(); |
| 101 |
|
| 102 |
try { |
| 103 |
it.next(); |
| 104 |
fail("Should have thrown"); |
| 105 |
} catch (NoSuchElementException ex) { |
| 106 |
// expected |
| 107 |
} |
| 108 |
|
| 109 |
try { |
| 110 |
it.remove(); |
| 111 |
fail("Should have thrown"); |
| 112 |
} catch (UnsupportedOperationException ex) { |
| 113 |
// expected |
| 114 |
} |
| 115 |
} |
| 116 |
} |