Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/BlockingFlowableLatestTest.java

raw
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 import org.reactivestreams.Subscriber;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.processors.PublishProcessor;
28 import io.reactivex.rxjava3.schedulers.TestScheduler;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class BlockingFlowableLatestTest extends RxJavaTest {
32 @Test
33 public void simple() {
34 TestScheduler scheduler = new TestScheduler();
35
36 Flowable<Long> source = Flowable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
37
38 Iterable<Long> iter = source.blockingLatest();
39
40 Iterator<Long> it = iter.iterator();
41
42 // only 9 because take(10) will immediately call onComplete when receiving the 10th item
43 // which onComplete will overwrite the previous value
44 for (int i = 0; i < 9; i++) {
45 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
46
47 Assert.assertTrue(it.hasNext());
48
49 Assert.assertEquals(Long.valueOf(i), it.next());
50 }
51
52 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
53 Assert.assertFalse(it.hasNext());
54 }
55
56 @Test
57 public void sameSourceMultipleIterators() {
58 TestScheduler scheduler = new TestScheduler();
59
60 Flowable<Long> source = Flowable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
61
62 Iterable<Long> iter = source.blockingLatest();
63
64 for (int j = 0; j < 3; j++) {
65 Iterator<Long> it = iter.iterator();
66
67 // only 9 because take(10) will immediately call onComplete when receiving the 10th item
68 // which onComplete will overwrite the previous value
69 for (int i = 0; i < 9; i++) {
70 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
71
72 Assert.assertTrue(it.hasNext());
73
74 Assert.assertEquals(Long.valueOf(i), it.next());
75 }
76
77 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
78 Assert.assertFalse(it.hasNext());
79 }
80 }
81
82 @Test(expected = NoSuchElementException.class)
83 public void empty() {
84 Flowable<Long> source = Flowable.<Long> empty();
85
86 Iterable<Long> iter = source.blockingLatest();
87
88 Iterator<Long> it = iter.iterator();
89
90 Assert.assertFalse(it.hasNext());
91
92 it.next();
93 }
94
95 @Test(expected = NoSuchElementException.class)
96 public void simpleJustNext() {
97 TestScheduler scheduler = new TestScheduler();
98
99 Flowable<Long> source = Flowable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
100
101 Iterable<Long> iter = source.blockingLatest();
102
103 Iterator<Long> it = iter.iterator();
104
105 // only 9 because take(10) will immediately call onComplete when receiving the 10th item
106 // which onComplete will overwrite the previous value
107 for (int i = 0; i < 10; i++) {
108 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
109
110 Assert.assertEquals(Long.valueOf(i), it.next());
111 }
112 }
113
114 @Test(expected = RuntimeException.class)
115 public void hasNextThrows() {
116 TestScheduler scheduler = new TestScheduler();
117
118 Flowable<Long> source = Flowable.<Long> error(new RuntimeException("Forced failure!")).subscribeOn(scheduler);
119
120 Iterable<Long> iter = source.blockingLatest();
121
122 Iterator<Long> it = iter.iterator();
123
124 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
125
126 it.hasNext();
127 }
128
129 @Test(expected = RuntimeException.class)
130 public void nextThrows() {
131 TestScheduler scheduler = new TestScheduler();
132
133 Flowable<Long> source = Flowable.<Long> error(new RuntimeException("Forced failure!")).subscribeOn(scheduler);
134
135 Iterable<Long> iter = source.blockingLatest();
136 Iterator<Long> it = iter.iterator();
137
138 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
139
140 it.next();
141 }
142
143 @Test
144 public void fasterSource() {
145 PublishProcessor<Integer> source = PublishProcessor.create();
146 Flowable<Integer> blocker = source;
147
148 Iterable<Integer> iter = blocker.blockingLatest();
149 Iterator<Integer> it = iter.iterator();
150
151 source.onNext(1);
152
153 Assert.assertEquals(Integer.valueOf(1), it.next());
154
155 source.onNext(2);
156 source.onNext(3);
157
158 Assert.assertEquals(Integer.valueOf(3), it.next());
159
160 source.onNext(4);
161 source.onNext(5);
162 source.onNext(6);
163
164 Assert.assertEquals(Integer.valueOf(6), it.next());
165
166 source.onNext(7);
167 source.onComplete();
168
169 Assert.assertFalse(it.hasNext());
170 }
171
172 @Test(expected = UnsupportedOperationException.class)
173 public void remove() {
174 Flowable.never().blockingLatest().iterator().remove();
175 }
176
177 @Test
178 public void interrupted() {
179 Iterator<Object> it = Flowable.never().blockingLatest().iterator();
180
181 Thread.currentThread().interrupt();
182
183 try {
184 it.hasNext();
185 } catch (RuntimeException ex) {
186 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
187 }
188 Thread.interrupted();
189 }
190
191 @Test(expected = NoSuchElementException.class)
192 public void empty2() {
193 Flowable.empty().blockingLatest().iterator().next();
194 }
195
196 @Test(expected = TestException.class)
197 public void error() {
198 Flowable.error(new TestException()).blockingLatest().iterator().next();
199 }
200
201 @Test
202 public void error2() {
203 Iterator<Object> it = Flowable.error(new TestException()).blockingLatest().iterator();
204
205 for (int i = 0; i < 3; i++) {
206 try {
207 it.hasNext();
208 fail("Should have thrown");
209 } catch (TestException ex) {
210 // expected
211 }
212 }
213 }
214
215 @SuppressWarnings("unchecked")
216 @Test
217 public void onError() {
218 Iterator<Object> it = Flowable.never().blockingLatest().iterator();
219
220 List<Throwable> errors = TestHelper.trackPluginErrors();
221 try {
222 ((Subscriber<Object>)it).onError(new TestException());
223
224 TestHelper.assertUndeliverable(errors, 0, TestException.class);
225 } finally {
226 RxJavaPlugins.reset();
227 }
228 }
229 }