Debian Codegraph @

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