| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.Cancellable; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
import io.reactivex.rxjava3.testsupport.*; |
| 29 |
|
| 30 |
public class SingleCreateTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
@SuppressUndeliverable |
| 34 |
public void basic() { |
| 35 |
final Disposable d = Disposable.empty(); |
| 36 |
|
| 37 |
Single.<Integer>create(new SingleOnSubscribe<Integer>() { |
| 38 |
@Override |
| 39 |
public void subscribe(SingleEmitter<Integer> e) throws Exception { |
| 40 |
e.setDisposable(d); |
| 41 |
|
| 42 |
e.onSuccess(1); |
| 43 |
e.onError(new TestException()); |
| 44 |
e.onSuccess(2); |
| 45 |
e.onError(new TestException()); |
| 46 |
} |
| 47 |
}) |
| 48 |
.test() |
| 49 |
.assertResult(1); |
| 50 |
|
| 51 |
assertTrue(d.isDisposed()); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
@SuppressUndeliverable |
| 56 |
public void basicWithCancellable() { |
| 57 |
final Disposable d1 = Disposable.empty(); |
| 58 |
final Disposable d2 = Disposable.empty(); |
| 59 |
|
| 60 |
Single.<Integer>create(new SingleOnSubscribe<Integer>() { |
| 61 |
@Override |
| 62 |
public void subscribe(SingleEmitter<Integer> e) throws Exception { |
| 63 |
e.setDisposable(d1); |
| 64 |
e.setCancellable(new Cancellable() { |
| 65 |
@Override |
| 66 |
public void cancel() throws Exception { |
| 67 |
d2.dispose(); |
| 68 |
} |
| 69 |
}); |
| 70 |
|
| 71 |
e.onSuccess(1); |
| 72 |
e.onError(new TestException()); |
| 73 |
e.onSuccess(2); |
| 74 |
e.onError(new TestException()); |
| 75 |
} |
| 76 |
}) |
| 77 |
.test() |
| 78 |
.assertResult(1); |
| 79 |
|
| 80 |
assertTrue(d1.isDisposed()); |
| 81 |
assertTrue(d2.isDisposed()); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
@SuppressUndeliverable |
| 86 |
public void basicWithError() { |
| 87 |
final Disposable d = Disposable.empty(); |
| 88 |
|
| 89 |
Single.<Integer>create(new SingleOnSubscribe<Integer>() { |
| 90 |
@Override |
| 91 |
public void subscribe(SingleEmitter<Integer> e) throws Exception { |
| 92 |
e.setDisposable(d); |
| 93 |
|
| 94 |
e.onError(new TestException()); |
| 95 |
e.onSuccess(2); |
| 96 |
e.onError(new TestException()); |
| 97 |
} |
| 98 |
}) |
| 99 |
.test() |
| 100 |
.assertFailure(TestException.class); |
| 101 |
|
| 102 |
assertTrue(d.isDisposed()); |
| 103 |
} |
| 104 |
|
| 105 |
@Test(expected = IllegalArgumentException.class) |
| 106 |
public void unsafeCreate() { |
| 107 |
Single.unsafeCreate(Single.just(1)); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void createCallbackThrows() { |
| 112 |
Single.create(new SingleOnSubscribe<Object>() { |
| 113 |
@Override |
| 114 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 115 |
throw new TestException(); |
| 116 |
} |
| 117 |
}) |
| 118 |
.test() |
| 119 |
.assertFailure(TestException.class); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void dispose() { |
| 124 |
TestHelper.checkDisposed(Single.create(new SingleOnSubscribe<Object>() { |
| 125 |
@Override |
| 126 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 127 |
s.onSuccess(1); |
| 128 |
} |
| 129 |
})); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void createNullSuccess() { |
| 134 |
Single.create(new SingleOnSubscribe<Object>() { |
| 135 |
@Override |
| 136 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 137 |
s.onSuccess(null); |
| 138 |
s.onSuccess(null); |
| 139 |
} |
| 140 |
}) |
| 141 |
.test() |
| 142 |
.assertFailure(NullPointerException.class); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void createNullError() { |
| 147 |
Single.create(new SingleOnSubscribe<Object>() { |
| 148 |
@Override |
| 149 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 150 |
s.onError(null); |
| 151 |
s.onError(null); |
| 152 |
} |
| 153 |
}) |
| 154 |
.test() |
| 155 |
.assertFailure(NullPointerException.class); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void createConsumerThrows() { |
| 160 |
Single.create(new SingleOnSubscribe<Object>() { |
| 161 |
@Override |
| 162 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 163 |
try { |
| 164 |
s.onSuccess(1); |
| 165 |
fail("Should have thrown"); |
| 166 |
} catch (TestException ex) { |
| 167 |
// expected |
| 168 |
} |
| 169 |
} |
| 170 |
}) |
| 171 |
.subscribe(new SingleObserver<Object>() { |
| 172 |
@Override |
| 173 |
public void onSubscribe(Disposable d) { |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public void onSuccess(Object value) { |
| 179 |
throw new TestException(); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public void onError(Throwable e) { |
| 184 |
|
| 185 |
} |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
@Test |
| 190 |
public void createConsumerThrowsResource() { |
| 191 |
Single.create(new SingleOnSubscribe<Object>() { |
| 192 |
@Override |
| 193 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 194 |
Disposable d = Disposable.empty(); |
| 195 |
s.setDisposable(d); |
| 196 |
try { |
| 197 |
s.onSuccess(1); |
| 198 |
fail("Should have thrown"); |
| 199 |
} catch (TestException ex) { |
| 200 |
// expected |
| 201 |
} |
| 202 |
|
| 203 |
assertTrue(d.isDisposed()); |
| 204 |
} |
| 205 |
}) |
| 206 |
.subscribe(new SingleObserver<Object>() { |
| 207 |
@Override |
| 208 |
public void onSubscribe(Disposable d) { |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public void onSuccess(Object value) { |
| 214 |
throw new TestException(); |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public void onError(Throwable e) { |
| 219 |
|
| 220 |
} |
| 221 |
}); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void createConsumerThrowsOnError() { |
| 226 |
Single.create(new SingleOnSubscribe<Object>() { |
| 227 |
@Override |
| 228 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 229 |
try { |
| 230 |
s.onError(new IOException()); |
| 231 |
fail("Should have thrown"); |
| 232 |
} catch (TestException ex) { |
| 233 |
// expected |
| 234 |
} |
| 235 |
} |
| 236 |
}) |
| 237 |
.subscribe(new SingleObserver<Object>() { |
| 238 |
@Override |
| 239 |
public void onSubscribe(Disposable d) { |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public void onSuccess(Object value) { |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public void onError(Throwable e) { |
| 248 |
throw new TestException(); |
| 249 |
} |
| 250 |
}); |
| 251 |
} |
| 252 |
|
| 253 |
@Test |
| 254 |
public void createConsumerThrowsResourceOnError() { |
| 255 |
Single.create(new SingleOnSubscribe<Object>() { |
| 256 |
@Override |
| 257 |
public void subscribe(SingleEmitter<Object> s) throws Exception { |
| 258 |
Disposable d = Disposable.empty(); |
| 259 |
s.setDisposable(d); |
| 260 |
try { |
| 261 |
s.onError(new IOException()); |
| 262 |
fail("Should have thrown"); |
| 263 |
} catch (TestException ex) { |
| 264 |
// expected |
| 265 |
} |
| 266 |
|
| 267 |
assertTrue(d.isDisposed()); |
| 268 |
} |
| 269 |
}) |
| 270 |
.subscribe(new SingleObserver<Object>() { |
| 271 |
@Override |
| 272 |
public void onSubscribe(Disposable d) { |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public void onSuccess(Object value) { |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public void onError(Throwable e) { |
| 282 |
throw new TestException(); |
| 283 |
} |
| 284 |
}); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void tryOnError() { |
| 289 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 290 |
try { |
| 291 |
final Boolean[] response = { null }; |
| 292 |
Single.create(new SingleOnSubscribe<Object>() { |
| 293 |
@Override |
| 294 |
public void subscribe(SingleEmitter<Object> e) throws Exception { |
| 295 |
e.onSuccess(1); |
| 296 |
response[0] = e.tryOnError(new TestException()); |
| 297 |
} |
| 298 |
}) |
| 299 |
.test() |
| 300 |
.assertResult(1); |
| 301 |
|
| 302 |
assertFalse(response[0]); |
| 303 |
|
| 304 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 305 |
} finally { |
| 306 |
RxJavaPlugins.reset(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void emitterHasToString() { |
| 312 |
Single.create(new SingleOnSubscribe<Object>() { |
| 313 |
@Override |
| 314 |
public void subscribe(SingleEmitter<Object> emitter) throws Exception { |
| 315 |
assertTrue(emitter.toString().contains(SingleCreate.Emitter.class.getSimpleName())); |
| 316 |
} |
| 317 |
}).test().assertEmpty(); |
| 318 |
} |
| 319 |
} |