| 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.*; |
| 43 |
import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection; |
| 44 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 45 |
import io.reactivex.rxjava3.subjects.CompletableSubject; |
| 46 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 47 |
|
| 48 |
public class CompletableConsumersTest implements Consumer<Object>, Action { |
| 49 |
|
| 50 |
final CompositeDisposable composite = new CompositeDisposable(); |
| 51 |
|
| 52 |
final CompletableSubject processor = CompletableSubject.create(); |
| 53 |
|
| 54 |
final List<Object> events = new ArrayList<>(); |
| 55 |
|
| 56 |
@Override |
| 57 |
public void run() throws Exception { |
| 58 |
events.add("OnComplete"); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void accept(Object t) throws Exception { |
| 63 |
events.add(t); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void onErrorNormal() { |
| 68 |
|
| 69 |
processor.subscribe(this, this, composite); |
| 70 |
|
| 71 |
assertTrue(composite.size() > 0); |
| 72 |
|
| 73 |
assertTrue(events.toString(), events.isEmpty()); |
| 74 |
|
| 75 |
processor.onComplete(); |
| 76 |
|
| 77 |
assertEquals(0, composite.size()); |
| 78 |
|
| 79 |
assertEquals(Arrays.<Object>asList("OnComplete"), events); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void onErrorError() { |
| 85 |
|
| 86 |
Disposable d = processor.subscribe(this, this, composite); |
| 87 |
|
| 88 |
assertTrue(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError()); |
| 89 |
|
| 90 |
assertTrue(composite.size() > 0); |
| 91 |
|
| 92 |
assertTrue(events.toString(), events.isEmpty()); |
| 93 |
|
| 94 |
processor.onError(new IOException()); |
| 95 |
|
| 96 |
assertTrue(events.toString(), events.get(0) instanceof IOException); |
| 97 |
|
| 98 |
assertEquals(0, composite.size()); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void onCompleteNormal() { |
| 103 |
|
| 104 |
processor.subscribe(this, this, composite); |
| 105 |
|
| 106 |
assertTrue(composite.size() > 0); |
| 107 |
|
| 108 |
assertTrue(events.toString(), events.isEmpty()); |
| 109 |
|
| 110 |
processor.onComplete(); |
| 111 |
|
| 112 |
assertEquals(0, composite.size()); |
| 113 |
|
| 114 |
assertEquals(Arrays.<Object>asList("OnComplete"), events); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void onCompleteError() { |
| 120 |
|
| 121 |
processor.subscribe(this, this, composite); |
| 122 |
|
| 123 |
assertTrue(composite.size() > 0); |
| 124 |
|
| 125 |
assertTrue(events.toString(), events.isEmpty()); |
| 126 |
|
| 127 |
processor.onError(new IOException()); |
| 128 |
|
| 129 |
assertTrue(events.toString(), events.get(0) instanceof IOException); |
| 130 |
|
| 131 |
assertEquals(0, composite.size()); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void onCompleteDispose() { |
| 136 |
|
| 137 |
Disposable d = processor.subscribe(this, this, composite); |
| 138 |
|
| 139 |
assertTrue(composite.size() > 0); |
| 140 |
|
| 141 |
assertTrue(events.toString(), events.isEmpty()); |
| 142 |
|
| 143 |
assertFalse(d.isDisposed()); |
| 144 |
|
| 145 |
d.dispose(); |
| 146 |
d.dispose(); |
| 147 |
|
| 148 |
assertTrue(d.isDisposed()); |
| 149 |
|
| 150 |
assertEquals(0, composite.size()); |
| 151 |
|
| 152 |
assertFalse(processor.hasObservers()); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void onErrorCrash() { |
| 157 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 158 |
try { |
| 159 |
processor.subscribe(this, t -> { |
| 160 |
throw new IOException(t); |
| 161 |
}, composite); |
| 162 |
|
| 163 |
processor.onError(new IllegalArgumentException()); |
| 164 |
|
| 165 |
assertTrue(events.toString(), events.isEmpty()); |
| 166 |
|
| 167 |
TestHelper.assertError(errors, 0, CompositeException.class); |
| 168 |
List<Throwable> inners = TestHelper.compositeList(errors.get(0)); |
| 169 |
TestHelper.assertError(inners, 0, IllegalArgumentException.class); |
| 170 |
TestHelper.assertError(inners, 1, IOException.class); |
| 171 |
} finally { |
| 172 |
RxJavaPlugins.reset(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void onCompleteCrash() { |
| 178 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 179 |
try { |
| 180 |
processor.subscribe(new Action() { |
| 181 |
@Override |
| 182 |
public void run() throws Exception { |
| 183 |
throw new IOException(); |
| 184 |
} |
| 185 |
}, this, composite); |
| 186 |
|
| 187 |
processor.onComplete(); |
| 188 |
|
| 189 |
assertTrue(events.toString(), events.isEmpty()); |
| 190 |
|
| 191 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 192 |
} finally { |
| 193 |
RxJavaPlugins.reset(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void badSource() { |
| 199 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 200 |
try { |
| 201 |
new Completable() { |
| 202 |
@Override |
| 203 |
protected void subscribeActual( |
| 204 |
CompletableObserver observer) { |
| 205 |
observer.onSubscribe(Disposable.empty()); |
| 206 |
observer.onComplete(); |
| 207 |
|
| 208 |
observer.onSubscribe(Disposable.empty()); |
| 209 |
observer.onComplete(); |
| 210 |
observer.onError(new IOException()); |
| 211 |
} |
| 212 |
}.subscribe(this, this, composite); |
| 213 |
|
| 214 |
assertEquals(Arrays.<Object>asList("OnComplete"), events); |
| 215 |
|
| 216 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 217 |
} finally { |
| 218 |
RxJavaPlugins.reset(); |
| 219 |
} |
| 220 |
} |
| 221 |
} |