Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableToIteratorTest.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.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.core.Observable;
25 import io.reactivex.rxjava3.core.Observer;
26 import io.reactivex.rxjava3.disposables.Disposable;
27 import io.reactivex.rxjava3.exceptions.TestException;
28 import io.reactivex.rxjava3.internal.operators.observable.BlockingObservableIterable.BlockingObservableIterator;
29 import io.reactivex.rxjava3.schedulers.Schedulers;
30 import io.reactivex.rxjava3.subjects.PublishSubject;
31
32 public class BlockingObservableToIteratorTest extends RxJavaTest {
33
34 @Test
35 public void toIterator() {
36 Observable<String> obs = Observable.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 Observable<String> obs = Observable.unsafeCreate(new ObservableSource<String>() {
56
57 @Override
58 public void subscribe(Observer<? super String> observer) {
59 observer.onSubscribe(Disposable.empty());
60 observer.onNext("one");
61 observer.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 dispose() {
76 BlockingObservableIterator<Integer> it = new BlockingObservableIterator<>(128);
77
78 assertFalse(it.isDisposed());
79
80 it.dispose();
81
82 assertTrue(it.isDisposed());
83 }
84
85 @Test
86 public void interruptWait() {
87 BlockingObservableIterator<Integer> it = new BlockingObservableIterator<>(128);
88
89 try {
90 Thread.currentThread().interrupt();
91
92 it.hasNext();
93 } catch (RuntimeException ex) {
94 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
95 }
96 }
97
98 @Test(expected = NoSuchElementException.class)
99 public void emptyThrowsNoSuch() {
100 BlockingObservableIterator<Integer> it = new BlockingObservableIterator<>(128);
101 it.onComplete();
102 it.next();
103 }
104
105 @Test(expected = UnsupportedOperationException.class)
106 public void remove() {
107 BlockingObservableIterator<Integer> it = new BlockingObservableIterator<>(128);
108 it.remove();
109 }
110
111 @Test(expected = NoSuchElementException.class)
112 public void disposedIteratorHasNextReturns() {
113 Iterator<Integer> it = PublishSubject.<Integer>create()
114 .blockingIterable().iterator();
115 ((Disposable)it).dispose();
116 assertFalse(it.hasNext());
117 it.next();
118 }
119
120 @Test
121 public void asyncDisposeUnblocks() {
122 final Iterator<Integer> it = PublishSubject.<Integer>create()
123 .blockingIterable().iterator();
124
125 Schedulers.single().scheduleDirect(new Runnable() {
126 @Override
127 public void run() {
128 ((Disposable)it).dispose();
129 }
130 }, 1, TimeUnit.SECONDS);
131
132 assertFalse(it.hasNext());
133 }
134
135 @Test(expected = TestException.class)
136 public void errorAfterDispose() {
137 Iterator<Object> it = Observable.error(new TestException()).blockingIterable().iterator();
138
139 ((Disposable)it).dispose();
140
141 it.hasNext();
142 }
143 }