| 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 |
/* |
| 15 |
* Copyright 2016-2019 David Karnok |
| 16 |
* |
| 17 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 |
* you may not use this file except in compliance with the License. |
| 19 |
* You may obtain a copy of the License at |
| 20 |
* |
| 21 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 22 |
* |
| 23 |
* Unless required by applicable law or agreed to in writing, software |
| 24 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 25 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 |
* See the License for the specific language governing permissions and |
| 27 |
* limitations under the License. |
| 28 |
*/ |
| 29 |
|
| 30 |
package io.reactivex.rxjava3.internal.observers; |
| 31 |
|
| 32 |
import static org.junit.Assert.*; |
| 33 |
|
| 34 |
import java.io.IOException; |
| 35 |
import java.util.*; |
| 36 |
|
| 37 |
import org.junit.Test; |
| 38 |
|
| 39 |
import io.reactivex.rxjava3.core.*; |
| 40 |
import io.reactivex.rxjava3.disposables.*; |
| 41 |
import io.reactivex.rxjava3.exceptions.CompositeException; |
| 42 |
import io.reactivex.rxjava3.functions.Consumer; |
| 43 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 44 |
import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection; |
| 45 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 46 |
import io.reactivex.rxjava3.subjects.SingleSubject; |
| 47 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 48 |
|
| 49 |
public class SingleConsumersTest implements Consumer<Object> { |
| 50 |
|
| 51 |
final CompositeDisposable composite = new CompositeDisposable(); |
| 52 |
|
| 53 |
final SingleSubject<Integer> processor = SingleSubject.create(); |
| 54 |
|
| 55 |
final List<Object> events = new ArrayList<>(); |
| 56 |
|
| 57 |
@Override |
| 58 |
public void accept(Object t) throws Exception { |
| 59 |
events.add(t); |
| 60 |
} |
| 61 |
|
| 62 |
static <T> Disposable subscribeAutoDispose(Single<T> source, CompositeDisposable composite, |
| 63 |
Consumer<? super T> onSuccess, Consumer<? super Throwable> onError) { |
| 64 |
return source.subscribe(onSuccess, onError, composite); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void onSuccessNormal() { |
| 69 |
|
| 70 |
Disposable d = subscribeAutoDispose(processor, composite, this, Functions.ON_ERROR_MISSING); |
| 71 |
|
| 72 |
assertFalse(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError()); |
| 73 |
|
| 74 |
assertTrue(composite.size() > 0); |
| 75 |
|
| 76 |
assertTrue(events.toString(), events.isEmpty()); |
| 77 |
|
| 78 |
processor.onSuccess(1); |
| 79 |
|
| 80 |
assertEquals(0, composite.size()); |
| 81 |
|
| 82 |
assertEquals(Arrays.<Object>asList(1), events); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void onErrorNormal() { |
| 88 |
|
| 89 |
subscribeAutoDispose(processor, composite, this, this); |
| 90 |
|
| 91 |
assertTrue(composite.size() > 0); |
| 92 |
|
| 93 |
assertTrue(events.toString(), events.isEmpty()); |
| 94 |
|
| 95 |
processor.onSuccess(1); |
| 96 |
|
| 97 |
assertEquals(0, composite.size()); |
| 98 |
|
| 99 |
assertEquals(Arrays.<Object>asList(1), events); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void onErrorError() { |
| 105 |
|
| 106 |
Disposable d = subscribeAutoDispose(processor, composite, this, this); |
| 107 |
|
| 108 |
assertTrue(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError()); |
| 109 |
|
| 110 |
assertTrue(composite.size() > 0); |
| 111 |
|
| 112 |
assertTrue(events.toString(), events.isEmpty()); |
| 113 |
|
| 114 |
processor.onError(new IOException()); |
| 115 |
|
| 116 |
assertTrue(events.toString(), events.get(0) instanceof IOException); |
| 117 |
|
| 118 |
assertEquals(0, composite.size()); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void onSuccessCrash() { |
| 123 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 124 |
try { |
| 125 |
subscribeAutoDispose(processor, composite, new Consumer<Object>() { |
| 126 |
@Override |
| 127 |
public void accept(Object t) throws Exception { |
| 128 |
throw new IOException(); |
| 129 |
} |
| 130 |
}, this); |
| 131 |
|
| 132 |
processor.onSuccess(1); |
| 133 |
|
| 134 |
assertTrue(events.toString(), events.isEmpty()); |
| 135 |
|
| 136 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 137 |
} finally { |
| 138 |
RxJavaPlugins.reset(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void onErrorCrash() { |
| 144 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 145 |
try { |
| 146 |
subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() { |
| 147 |
@Override |
| 148 |
public void accept(Throwable t) throws Exception { |
| 149 |
throw new IOException(t); |
| 150 |
} |
| 151 |
}); |
| 152 |
|
| 153 |
processor.onError(new IllegalArgumentException()); |
| 154 |
|
| 155 |
assertTrue(events.toString(), events.isEmpty()); |
| 156 |
|
| 157 |
TestHelper.assertError(errors, 0, CompositeException.class); |
| 158 |
List<Throwable> inners = TestHelper.compositeList(errors.get(0)); |
| 159 |
TestHelper.assertError(inners, 0, IllegalArgumentException.class); |
| 160 |
TestHelper.assertError(inners, 1, IOException.class); |
| 161 |
} finally { |
| 162 |
RxJavaPlugins.reset(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void badSource() { |
| 168 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 169 |
try { |
| 170 |
subscribeAutoDispose( |
| 171 |
new Single<Integer>() { |
| 172 |
@Override |
| 173 |
protected void subscribeActual( |
| 174 |
SingleObserver<? super Integer> observer) { |
| 175 |
observer.onSubscribe(Disposable.empty()); |
| 176 |
observer.onSuccess(1); |
| 177 |
|
| 178 |
observer.onSubscribe(Disposable.empty()); |
| 179 |
observer.onSuccess(2); |
| 180 |
observer.onError(new IOException()); |
| 181 |
} |
| 182 |
}, composite, this, this |
| 183 |
); |
| 184 |
|
| 185 |
assertEquals(Arrays.<Object>asList(1), events); |
| 186 |
|
| 187 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 188 |
} finally { |
| 189 |
RxJavaPlugins.reset(); |
| 190 |
} |
| 191 |
} |
| 192 |
} |