Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/BlockingFlowableToIteratorTest.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.Test;
22 import org.reactivestreams.*;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.disposables.Disposable;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.internal.operators.flowable.BlockingFlowableIterable.BlockingFlowableIterator;
28 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
29 import io.reactivex.rxjava3.processors.PublishProcessor;
30 import io.reactivex.rxjava3.schedulers.Schedulers;
31
32 public class BlockingFlowableToIteratorTest extends RxJavaTest {
33
34 @Test
35 public void toIterator() {
36 Flowable<String> obs = Flowable.just("one", "two", "three");
37
38 Iterator<String> it = obs.blockingIterable().iterator();
39
40 assertTrue(it.hasNext());
41 assertEquals("one", it.next());
42
43 assertTrue(it.hasNext());
44 assertEquals("two", it.next());
45
46 assertTrue(it.hasNext());
47 assertEquals("three", it.next());
48
49 assertFalse(it.hasNext());
50
51 }
52
53 @Test(expected = TestException.class)
54 public void toIteratorWithException() {
55 Flowable<String> obs = Flowable.unsafeCreate(new Publisher<String>() {
56
57 @Override
58 public void subscribe(Subscriber<? super String> subscriber) {
59 subscriber.onSubscribe(new BooleanSubscription());
60 subscriber.onNext("one");
61 subscriber.onError(new TestException());
62 }
63 });
64
65 Iterator<String> it = obs.blockingIterable().iterator();
66
67 assertTrue(it.hasNext());
68 assertEquals("one", it.next());
69
70 assertTrue(it.hasNext());
71 it.next();
72 }
73
74 @Test
75 public void iteratorExertBackpressure() {
76 final Counter src = new Counter();
77
78 Flowable<Integer> obs = Flowable.fromIterable(new Iterable<Integer>() {
79 @Override
80 public Iterator<Integer> iterator() {
81 return src;
82 }
83 });
84
85 Iterator<Integer> it = obs.blockingIterable().iterator();
86 while (it.hasNext()) {
87 // Correct backpressure should cause this interleaved behavior.
88 // We first request RxRingBuffer.SIZE. Then in increments of
89 // SubscriberIterator.LIMIT.
90 int i = it.next();
91 int expected = i - (i % (Flowable.bufferSize() - (Flowable.bufferSize() >> 2))) + Flowable.bufferSize();
92 expected = Math.min(expected, Counter.MAX);
93
94 assertEquals(expected, src.count);
95 }
96 }
97
98 public static final class Counter implements Iterator<Integer> {
99 static final int MAX = 5 * Flowable.bufferSize();
100 public int count;
101
102 @Override
103 public boolean hasNext() {
104 return count < MAX;
105 }
106
107 @Override
108 public Integer next() {
109 return ++count;
110 }
111
112 @Override
113 public void remove() {
114 throw new UnsupportedOperationException();
115 }
116 }
117
118 @Test(expected = UnsupportedOperationException.class)
119 public void remove() {
120 BlockingFlowableIterator<Integer> it = new BlockingFlowableIterator<>(128);
121 it.remove();
122 }
123
124 @Test
125 public void dispose() {
126 BlockingFlowableIterator<Integer> it = new BlockingFlowableIterator<>(128);
127
128 assertFalse(it.isDisposed());
129
130 it.dispose();
131
132 assertTrue(it.isDisposed());
133 }
134
135 @Test
136 public void interruptWait() {
137 BlockingFlowableIterator<Integer> it = new BlockingFlowableIterator<>(128);
138
139 try {
140 Thread.currentThread().interrupt();
141
142 it.hasNext();
143 } catch (RuntimeException ex) {
144 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
145 }
146 }
147
148 @Test(expected = NoSuchElementException.class)
149 public void emptyThrowsNoSuch() {
150 BlockingFlowableIterator<Integer> it = new BlockingFlowableIterator<>(128);
151 it.onComplete();
152 it.next();
153 }
154
155 @Test(expected = MissingBackpressureException.class)
156 public void overflowQueue() {
157 Iterator<Integer> it = new Flowable<Integer>() {
158 @Override
159 protected void subscribeActual(Subscriber<? super Integer> s) {
160 s.onSubscribe(new BooleanSubscription());
161 s.onNext(1);
162 s.onNext(2);
163 }
164 }
165 .blockingIterable(1)
166 .iterator();
167
168 it.next();
169 }
170
171 @Test(expected = NoSuchElementException.class)
172 public void disposedIteratorHasNextReturns() {
173 Iterator<Integer> it = PublishProcessor.<Integer>create()
174 .blockingIterable().iterator();
175 ((Disposable)it).dispose();
176 assertFalse(it.hasNext());
177 it.next();
178 }
179
180 @Test
181 public void asyncDisposeUnblocks() {
182 final Iterator<Integer> it = PublishProcessor.<Integer>create()
183 .blockingIterable().iterator();
184
185 Schedulers.single().scheduleDirect(new Runnable() {
186 @Override
187 public void run() {
188 ((Disposable)it).dispose();
189 }
190 }, 1, TimeUnit.SECONDS);
191
192 assertFalse(it.hasNext());
193 }
194 }