Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableMaterializeTest.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.ExecutionException;
20
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.core.Observable;
26 import io.reactivex.rxjava3.core.Observer;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.observers.DefaultObserver;
29 import io.reactivex.rxjava3.testsupport.*;
30
31 public class ObservableMaterializeTest extends RxJavaTest {
32
33 @Test
34 public void materialize1() {
35 // null will cause onError to be triggered before "three" can be
36 // returned
37 final TestAsyncErrorObservable o1 = new TestAsyncErrorObservable("one", "two", null,
38 "three");
39
40 TestLocalObserver observer = new TestLocalObserver();
41 Observable<Notification<String>> m = Observable.unsafeCreate(o1).materialize();
42 m.subscribe(observer);
43
44 try {
45 o1.t.join();
46 } catch (InterruptedException e) {
47 throw new RuntimeException(e);
48 }
49
50 assertFalse(observer.onError);
51 assertTrue(observer.onComplete);
52 assertEquals(3, observer.notifications.size());
53
54 assertTrue(observer.notifications.get(0).isOnNext());
55 assertEquals("one", observer.notifications.get(0).getValue());
56
57 assertTrue(observer.notifications.get(1).isOnNext());
58 assertEquals("two", observer.notifications.get(1).getValue());
59
60 assertTrue(observer.notifications.get(2).isOnError());
61 assertEquals(NullPointerException.class, observer.notifications.get(2).getError().getClass());
62 }
63
64 @Test
65 public void materialize2() {
66 final TestAsyncErrorObservable o1 = new TestAsyncErrorObservable("one", "two", "three");
67
68 TestLocalObserver observer = new TestLocalObserver();
69 Observable<Notification<String>> m = Observable.unsafeCreate(o1).materialize();
70 m.subscribe(observer);
71
72 try {
73 o1.t.join();
74 } catch (InterruptedException e) {
75 throw new RuntimeException(e);
76 }
77
78 assertFalse(observer.onError);
79 assertTrue(observer.onComplete);
80 assertEquals(4, observer.notifications.size());
81 assertTrue(observer.notifications.get(0).isOnNext());
82 assertEquals("one", observer.notifications.get(0).getValue());
83
84 assertTrue(observer.notifications.get(1).isOnNext());
85 assertEquals("two", observer.notifications.get(1).getValue());
86
87 assertTrue(observer.notifications.get(2).isOnNext());
88 assertEquals("three", observer.notifications.get(2).getValue());
89
90 assertTrue(observer.notifications.get(3).isOnComplete());
91 }
92
93 @Test
94 public void multipleSubscribes() throws InterruptedException, ExecutionException {
95 final TestAsyncErrorObservable o = new TestAsyncErrorObservable("one", "two", null, "three");
96
97 Observable<Notification<String>> m = Observable.unsafeCreate(o).materialize();
98
99 assertEquals(3, m.toList().toFuture().get().size());
100 assertEquals(3, m.toList().toFuture().get().size());
101 }
102
103 @Test
104 public void withCompletionCausingError() {
105 TestObserverEx<Notification<Integer>> to = new TestObserverEx<>();
106 final RuntimeException ex = new RuntimeException("boo");
107 Observable.<Integer>empty().materialize().doOnNext(new Consumer<Object>() {
108 @Override
109 public void accept(Object t) {
110 throw ex;
111 }
112 }).subscribe(to);
113 to.assertError(ex);
114 to.assertNoValues();
115 to.assertTerminated();
116 }
117
118 private static class TestLocalObserver extends DefaultObserver<Notification<String>> {
119
120 boolean onComplete;
121 boolean onError;
122 List<Notification<String>> notifications = new Vector<>();
123
124 @Override
125 public void onComplete() {
126 this.onComplete = true;
127 }
128
129 @Override
130 public void onError(Throwable e) {
131 this.onError = true;
132 }
133
134 @Override
135 public void onNext(Notification<String> value) {
136 this.notifications.add(value);
137 }
138
139 }
140
141 private static class TestAsyncErrorObservable implements ObservableSource<String> {
142
143 String[] valuesToReturn;
144
145 TestAsyncErrorObservable(String... values) {
146 valuesToReturn = values;
147 }
148
149 volatile Thread t;
150
151 @Override
152 public void subscribe(final Observer<? super String> observer) {
153 observer.onSubscribe(Disposable.empty());
154 t = new Thread(new Runnable() {
155
156 @Override
157 public void run() {
158 for (String s : valuesToReturn) {
159 if (s == null) {
160 System.out.println("throwing exception");
161 try {
162 Thread.sleep(100);
163 } catch (Throwable e) {
164
165 }
166 observer.onError(new NullPointerException());
167 return;
168 } else {
169 observer.onNext(s);
170 }
171 }
172 System.out.println("subscription complete");
173 observer.onComplete();
174 }
175
176 });
177 t.start();
178 }
179 }
180
181 @Test
182 public void dispose() {
183 TestHelper.checkDisposed(Observable.just(1).materialize());
184 }
185
186 @Test
187 public void doubleOnSubscribe() {
188 TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Notification<Object>>>() {
189 @Override
190 public ObservableSource<Notification<Object>> apply(Observable<Object> o) throws Exception {
191 return o.materialize();
192 }
193 });
194 }
195 }