Skip to content

Commit 0638fc2

Browse files
authored
Add LambdaObserver tests from RxJava (#3)
* Add LambdaObserver test from RxJava In happy cases, AutoDisposing observers should effectively have identical behavior, so borrowing this test. Still one kink to work out with handling bad onSubscribes, but putting this up to start and get eyes. Once `LambdaObserverTest` is good to go, I'll add LambdaSubscriberTest * Fix rebase issues (bring back down to java 7) * Ignore the badOnSubscribe test for now It's a defensive test and one we can revisit later * Add other lambda observer tests
1 parent 26e5988 commit 0638fc2

7 files changed

Lines changed: 1328 additions & 0 deletions

File tree

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/*
2+
* Copyright (C) 2017. Uber Technologies
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.uber.autodispose;
18+
19+
import com.uber.autodispose.observers.AutoDisposingCompletableObserver;
20+
import io.reactivex.Completable;
21+
import io.reactivex.CompletableObserver;
22+
import io.reactivex.Maybe;
23+
import io.reactivex.disposables.Disposable;
24+
import io.reactivex.disposables.Disposables;
25+
import io.reactivex.exceptions.CompositeException;
26+
import io.reactivex.functions.Action;
27+
import io.reactivex.functions.Consumer;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.List;
32+
import org.junit.Ignore;
33+
import org.junit.Rule;
34+
import org.junit.Test;
35+
36+
import static com.google.common.truth.Truth.assertThat;
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertFalse;
39+
import static org.junit.Assert.assertTrue;
40+
41+
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") public class LambdaCompletableObserverTest {
42+
43+
@Rule public RxErrorsRule errors = new RxErrorsRule();
44+
45+
private final Maybe<Integer> lifecycle = Maybe.empty();
46+
47+
@Test public void onSubscribeThrows() {
48+
final List<Object> received = new ArrayList<>();
49+
50+
AutoDisposingCompletableObserver o =
51+
new AutoDisposingCompletableObserverImpl(lifecycle, new Action() {
52+
@Override public void run() throws Exception {
53+
received.add(100);
54+
}
55+
}, new Consumer<Object>() {
56+
@Override public void accept(Object o) throws Exception {
57+
received.add(o);
58+
}
59+
}, new Consumer<Disposable>() {
60+
@Override public void accept(Disposable disposable) throws Exception {
61+
throw new TestException();
62+
}
63+
});
64+
65+
assertFalse(o.isDisposed());
66+
67+
Completable.complete()
68+
.subscribe(o);
69+
70+
assertTrue(received.toString(), received.get(0) instanceof TestException);
71+
assertEquals(received.toString(), 1, received.size());
72+
73+
assertTrue(o.isDisposed());
74+
}
75+
76+
@Test public void onSuccessThrows() {
77+
final List<Object> received = new ArrayList<>();
78+
79+
AutoDisposingCompletableObserver o =
80+
new AutoDisposingCompletableObserverImpl(lifecycle, new Action() {
81+
@Override public void run() throws Exception {
82+
received.add(100);
83+
}
84+
}, new Consumer<Throwable>() {
85+
@Override public void accept(Throwable o) throws Exception {
86+
received.add(o);
87+
}
88+
}, new Consumer<Disposable>() {
89+
@Override public void accept(Disposable disposable) throws Exception {
90+
throw new TestException();
91+
}
92+
});
93+
94+
assertFalse(o.isDisposed());
95+
96+
Completable.complete()
97+
.subscribe(o);
98+
99+
assertTrue(received.toString(), received.get(0) instanceof TestException);
100+
assertEquals(received.toString(), 1, received.size());
101+
102+
assertTrue(o.isDisposed());
103+
}
104+
105+
@Test public void onErrorThrows() {
106+
final List<Object> received = new ArrayList<>();
107+
108+
AutoDisposingCompletableObserver o =
109+
new AutoDisposingCompletableObserverImpl(lifecycle, new Action() {
110+
@Override public void run() throws Exception {
111+
received.add(100);
112+
}
113+
}, new Consumer<Throwable>() {
114+
@Override public void accept(Throwable o) throws Exception {
115+
throw new TestException("Inner");
116+
}
117+
}, new Consumer<Disposable>() {
118+
@Override public void accept(Disposable disposable) throws Exception {
119+
}
120+
});
121+
122+
assertFalse(o.isDisposed());
123+
124+
Completable.<Integer>error(new TestException("Outer")).subscribe(o);
125+
126+
assertTrue(received.toString(), received.isEmpty());
127+
128+
assertTrue(o.isDisposed());
129+
130+
CompositeException ex = errors.takeCompositeException();
131+
List<Throwable> ce = ex.getExceptions();
132+
assertThat(ce).hasSize(2);
133+
assertThat(ce.get(0)).hasMessage("Outer");
134+
assertThat(ce.get(1)).hasMessage("Inner");
135+
}
136+
137+
@Test public void onCompleteThrows() {
138+
final List<Object> received = new ArrayList<>();
139+
140+
AutoDisposingCompletableObserver o =
141+
new AutoDisposingCompletableObserverImpl(lifecycle, new Action() {
142+
@Override public void run() throws Exception {
143+
throw new TestException();
144+
}
145+
}, new Consumer<Object>() {
146+
@Override public void accept(Object o) throws Exception {
147+
received.add(o);
148+
}
149+
}, new Consumer<Disposable>() {
150+
@Override public void accept(Disposable disposable) throws Exception {
151+
}
152+
});
153+
154+
assertFalse(o.isDisposed());
155+
156+
Completable.complete()
157+
.subscribe(o);
158+
159+
assertTrue(received.toString(), received.isEmpty());
160+
161+
assertTrue(o.isDisposed());
162+
163+
assertThat(errors.take()).isInstanceOf(TestException.class);
164+
}
165+
166+
@Test @Ignore public void badSourceOnSubscribe() {
167+
Completable source = new Completable() {
168+
@Override public void subscribeActual(CompletableObserver s) {
169+
Disposable s1 = Disposables.empty();
170+
s.onSubscribe(s1);
171+
Disposable s2 = Disposables.empty();
172+
s.onSubscribe(s2);
173+
174+
assertFalse(s1.isDisposed());
175+
assertTrue(s2.isDisposed());
176+
177+
s.onComplete();
178+
}
179+
};
180+
181+
final List<Object> received = new ArrayList<>();
182+
183+
AutoDisposingCompletableObserver o =
184+
new AutoDisposingCompletableObserverImpl(lifecycle, new Action() {
185+
@Override public void run() throws Exception {
186+
received.add(100);
187+
}
188+
}, new Consumer<Object>() {
189+
@Override public void accept(Object o) throws Exception {
190+
received.add(o);
191+
}
192+
}, new Consumer<Disposable>() {
193+
@Override public void accept(Disposable disposable) throws Exception {
194+
195+
}
196+
});
197+
198+
source.subscribe(o);
199+
200+
assertEquals(Arrays.asList(1, 100), received);
201+
}
202+
203+
@Test public void badSourceEmitAfterDone() {
204+
Completable source = new Completable() {
205+
@Override public void subscribeActual(CompletableObserver s) {
206+
s.onSubscribe(Disposables.empty());
207+
208+
s.onComplete();
209+
s.onError(new TestException());
210+
s.onComplete();
211+
}
212+
};
213+
214+
final List<Object> received = new ArrayList<>();
215+
216+
AutoDisposingCompletableObserver o =
217+
new AutoDisposingCompletableObserverImpl(lifecycle, new Action() {
218+
@Override public void run() throws Exception {
219+
received.add(100);
220+
}
221+
}, new Consumer<Object>() {
222+
@Override public void accept(Object o) throws Exception {
223+
received.add(o);
224+
}
225+
}, new Consumer<Disposable>() {
226+
@Override public void accept(Disposable disposable) throws Exception {
227+
}
228+
});
229+
230+
source.subscribe(o);
231+
232+
assertEquals(Collections.singletonList(100), received);
233+
}
234+
}

0 commit comments

Comments
 (0)