| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 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.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.observers.*; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 31 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 32 |
|
| 33 |
public class ObservableSubscriberTest extends RxJavaTest { |
| 34 |
@Test |
| 35 |
public void onStartCalledOnceViaSubscribe() { |
| 36 |
final AtomicInteger c = new AtomicInteger(); |
| 37 |
Observable.just(1, 2, 3, 4).take(2).subscribe(new DefaultObserver<Integer>() { |
| 38 |
|
| 39 |
@Override |
| 40 |
public void onStart() { |
| 41 |
c.incrementAndGet(); |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void onComplete() { |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void onError(Throwable e) { |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onNext(Integer t) { |
| 56 |
} |
| 57 |
|
| 58 |
}); |
| 59 |
|
| 60 |
assertEquals(1, c.get()); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void onStartCalledOnceViaUnsafeSubscribe() { |
| 65 |
final AtomicInteger c = new AtomicInteger(); |
| 66 |
Observable.just(1, 2, 3, 4).take(2).subscribe(new DefaultObserver<Integer>() { |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onStart() { |
| 70 |
c.incrementAndGet(); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onComplete() { |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void onError(Throwable e) { |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onNext(Integer t) { |
| 85 |
} |
| 86 |
|
| 87 |
}); |
| 88 |
|
| 89 |
assertEquals(1, c.get()); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void onStartCalledOnceViaLift() { |
| 94 |
final AtomicInteger c = new AtomicInteger(); |
| 95 |
Observable.just(1, 2, 3, 4).lift(new ObservableOperator<Integer, Integer>() { |
| 96 |
|
| 97 |
@Override |
| 98 |
public Observer<? super Integer> apply(final Observer<? super Integer> child) { |
| 99 |
return new DefaultObserver<Integer>() { |
| 100 |
|
| 101 |
@Override |
| 102 |
public void onStart() { |
| 103 |
c.incrementAndGet(); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onComplete() { |
| 108 |
child.onComplete(); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void onError(Throwable e) { |
| 113 |
child.onError(e); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void onNext(Integer t) { |
| 118 |
child.onNext(t); |
| 119 |
} |
| 120 |
|
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
}).subscribe(); |
| 125 |
|
| 126 |
assertEquals(1, c.get()); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void subscribeConsumerConsumer() { |
| 131 |
final List<Integer> list = new ArrayList<>(); |
| 132 |
|
| 133 |
Observable.just(1).subscribe(new Consumer<Integer>() { |
| 134 |
@Override |
| 135 |
public void accept(Integer v) throws Exception { |
| 136 |
list.add(v); |
| 137 |
} |
| 138 |
}, new Consumer<Throwable>() { |
| 139 |
@Override |
| 140 |
public void accept(Throwable e) throws Exception { |
| 141 |
list.add(100); |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
assertEquals(Arrays.asList(1), list); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void subscribeConsumerConsumerWithError() { |
| 150 |
final List<Integer> list = new ArrayList<>(); |
| 151 |
|
| 152 |
Observable.<Integer>error(new TestException()).subscribe(new Consumer<Integer>() { |
| 153 |
@Override |
| 154 |
public void accept(Integer v) throws Exception { |
| 155 |
list.add(v); |
| 156 |
} |
| 157 |
}, new Consumer<Throwable>() { |
| 158 |
@Override |
| 159 |
public void accept(Throwable e) throws Exception { |
| 160 |
list.add(100); |
| 161 |
} |
| 162 |
}); |
| 163 |
|
| 164 |
assertEquals(Arrays.asList(100), list); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void methodTestCancelled() { |
| 169 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 170 |
|
| 171 |
ps.test(true); |
| 172 |
|
| 173 |
assertFalse(ps.hasObservers()); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void safeSubscriberAlreadySafe() { |
| 178 |
TestObserver<Integer> to = new TestObserver<>(); |
| 179 |
Observable.just(1).safeSubscribe(new SafeObserver<>(to)); |
| 180 |
|
| 181 |
to.assertResult(1); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void methodTestNoCancel() { |
| 186 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 187 |
|
| 188 |
ps.test(false); |
| 189 |
|
| 190 |
assertTrue(ps.hasObservers()); |
| 191 |
} |
| 192 |
|
| 193 |
@SuppressWarnings("rawtypes") |
| 194 |
@Test |
| 195 |
public void pluginNull() { |
| 196 |
RxJavaPlugins.setOnObservableSubscribe(new BiFunction<Observable, Observer, Observer>() { |
| 197 |
@Override |
| 198 |
public Observer apply(Observable a, Observer b) throws Exception { |
| 199 |
return null; |
| 200 |
} |
| 201 |
}); |
| 202 |
|
| 203 |
try { |
| 204 |
try { |
| 205 |
|
| 206 |
Observable.just(1).test(); |
| 207 |
fail("Should have thrown"); |
| 208 |
} catch (NullPointerException ex) { |
| 209 |
assertEquals("The RxJavaPlugins.onSubscribe hook returned a null Observer. Please change the handler provided to RxJavaPlugins.setOnObservableSubscribe for invalid null returns. Further reading: https://github.com/ReactiveX/RxJava/wiki/Plugins", ex.getMessage()); |
| 210 |
} |
| 211 |
} finally { |
| 212 |
RxJavaPlugins.reset(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static final class BadObservable extends Observable<Integer> { |
| 217 |
@Override |
| 218 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 219 |
throw new IllegalArgumentException(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void subscribeActualThrows() { |
| 225 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 226 |
try { |
| 227 |
try { |
| 228 |
new BadObservable().test(); |
| 229 |
fail("Should have thrown!"); |
| 230 |
} catch (NullPointerException ex) { |
| 231 |
if (!(ex.getCause() instanceof IllegalArgumentException)) { |
| 232 |
fail(ex.toString() + ": Should be NPE(IAE)"); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
TestHelper.assertError(list, 0, IllegalArgumentException.class); |
| 237 |
} finally { |
| 238 |
RxJavaPlugins.reset(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
} |