| 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.util; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.*; |
| 21 |
import org.reactivestreams.Subscriber; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.ProtocolViolationException; |
| 26 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 27 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 28 |
import io.reactivex.rxjava3.observers.*; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.subscribers.*; |
| 31 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 32 |
|
| 33 |
public class EndConsumerHelperTest extends RxJavaTest { |
| 34 |
|
| 35 |
List<Throwable> errors; |
| 36 |
|
| 37 |
@Before |
| 38 |
public void before() { |
| 39 |
errors = TestHelper.trackPluginErrors(); |
| 40 |
} |
| 41 |
|
| 42 |
@After |
| 43 |
public void after() { |
| 44 |
RxJavaPlugins.reset(); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void utilityClass() { |
| 49 |
TestHelper.checkUtilityClass(EndConsumerHelper.class); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void checkDoubleDefaultSubscriber() { |
| 54 |
Subscriber<Integer> consumer = new DefaultSubscriber<Integer>() { |
| 55 |
@Override |
| 56 |
public void onNext(Integer t) { |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onError(Throwable t) { |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onComplete() { |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
BooleanSubscription sub1 = new BooleanSubscription(); |
| 69 |
|
| 70 |
consumer.onSubscribe(sub1); |
| 71 |
|
| 72 |
assertFalse(sub1.isCancelled()); |
| 73 |
|
| 74 |
BooleanSubscription sub2 = new BooleanSubscription(); |
| 75 |
|
| 76 |
consumer.onSubscribe(sub2); |
| 77 |
|
| 78 |
assertFalse(sub1.isCancelled()); |
| 79 |
|
| 80 |
assertTrue(sub2.isCancelled()); |
| 81 |
|
| 82 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 83 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 84 |
assertEquals(errors.toString(), 1, errors.size()); |
| 85 |
} |
| 86 |
|
| 87 |
static final class EndDefaultSubscriber extends DefaultSubscriber<Integer> { |
| 88 |
@Override |
| 89 |
public void onNext(Integer t) { |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onError(Throwable t) { |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void onComplete() { |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void checkDoubleDefaultSubscriberNonAnonymous() { |
| 103 |
Subscriber<Integer> consumer = new EndDefaultSubscriber(); |
| 104 |
|
| 105 |
BooleanSubscription sub1 = new BooleanSubscription(); |
| 106 |
|
| 107 |
consumer.onSubscribe(sub1); |
| 108 |
|
| 109 |
assertFalse(sub1.isCancelled()); |
| 110 |
|
| 111 |
BooleanSubscription sub2 = new BooleanSubscription(); |
| 112 |
|
| 113 |
consumer.onSubscribe(sub2); |
| 114 |
|
| 115 |
assertFalse(sub1.isCancelled()); |
| 116 |
|
| 117 |
assertTrue(sub2.isCancelled()); |
| 118 |
|
| 119 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 120 |
|
| 121 |
// with this consumer, the class name should be predictable |
| 122 |
assertEquals(EndConsumerHelper.composeMessage("io.reactivex.rxjava3.internal.util.EndConsumerHelperTest$EndDefaultSubscriber"), errors.get(0).getMessage()); |
| 123 |
assertEquals(errors.toString(), 1, errors.size()); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void checkDoubleDisposableSubscriber() { |
| 128 |
Subscriber<Integer> consumer = new DisposableSubscriber<Integer>() { |
| 129 |
@Override |
| 130 |
public void onNext(Integer t) { |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void onError(Throwable t) { |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onComplete() { |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
BooleanSubscription sub1 = new BooleanSubscription(); |
| 143 |
|
| 144 |
consumer.onSubscribe(sub1); |
| 145 |
|
| 146 |
assertFalse(sub1.isCancelled()); |
| 147 |
|
| 148 |
BooleanSubscription sub2 = new BooleanSubscription(); |
| 149 |
|
| 150 |
consumer.onSubscribe(sub2); |
| 151 |
|
| 152 |
assertFalse(sub1.isCancelled()); |
| 153 |
|
| 154 |
assertTrue(sub2.isCancelled()); |
| 155 |
|
| 156 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 157 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 158 |
assertEquals(errors.toString(), 1, errors.size()); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void checkDoubleResourceSubscriber() { |
| 163 |
Subscriber<Integer> consumer = new ResourceSubscriber<Integer>() { |
| 164 |
@Override |
| 165 |
public void onNext(Integer t) { |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onError(Throwable t) { |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void onComplete() { |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
BooleanSubscription sub1 = new BooleanSubscription(); |
| 178 |
|
| 179 |
consumer.onSubscribe(sub1); |
| 180 |
|
| 181 |
assertFalse(sub1.isCancelled()); |
| 182 |
|
| 183 |
BooleanSubscription sub2 = new BooleanSubscription(); |
| 184 |
|
| 185 |
consumer.onSubscribe(sub2); |
| 186 |
|
| 187 |
assertFalse(sub1.isCancelled()); |
| 188 |
|
| 189 |
assertTrue(sub2.isCancelled()); |
| 190 |
|
| 191 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 192 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 193 |
assertEquals(errors.toString(), 1, errors.size()); |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void checkDoubleDefaultObserver() { |
| 198 |
Observer<Integer> consumer = new DefaultObserver<Integer>() { |
| 199 |
@Override |
| 200 |
public void onNext(Integer t) { |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public void onError(Throwable t) { |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public void onComplete() { |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
Disposable sub1 = Disposable.empty(); |
| 213 |
|
| 214 |
consumer.onSubscribe(sub1); |
| 215 |
|
| 216 |
assertFalse(sub1.isDisposed()); |
| 217 |
|
| 218 |
Disposable sub2 = Disposable.empty(); |
| 219 |
|
| 220 |
consumer.onSubscribe(sub2); |
| 221 |
|
| 222 |
assertFalse(sub1.isDisposed()); |
| 223 |
|
| 224 |
assertTrue(sub2.isDisposed()); |
| 225 |
|
| 226 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 227 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 228 |
assertEquals(errors.toString(), 1, errors.size()); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void checkDoubleDisposableObserver() { |
| 233 |
Observer<Integer> consumer = new DisposableObserver<Integer>() { |
| 234 |
@Override |
| 235 |
public void onNext(Integer t) { |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void onError(Throwable t) { |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public void onComplete() { |
| 244 |
} |
| 245 |
}; |
| 246 |
|
| 247 |
Disposable sub1 = Disposable.empty(); |
| 248 |
|
| 249 |
consumer.onSubscribe(sub1); |
| 250 |
|
| 251 |
assertFalse(sub1.isDisposed()); |
| 252 |
|
| 253 |
Disposable sub2 = Disposable.empty(); |
| 254 |
|
| 255 |
consumer.onSubscribe(sub2); |
| 256 |
|
| 257 |
assertFalse(sub1.isDisposed()); |
| 258 |
|
| 259 |
assertTrue(sub2.isDisposed()); |
| 260 |
|
| 261 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 262 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 263 |
assertEquals(errors.toString(), 1, errors.size()); |
| 264 |
} |
| 265 |
|
| 266 |
@Test |
| 267 |
public void checkDoubleResourceObserver() { |
| 268 |
Observer<Integer> consumer = new ResourceObserver<Integer>() { |
| 269 |
@Override |
| 270 |
public void onNext(Integer t) { |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public void onError(Throwable t) { |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
public void onComplete() { |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
Disposable sub1 = Disposable.empty(); |
| 283 |
|
| 284 |
consumer.onSubscribe(sub1); |
| 285 |
|
| 286 |
assertFalse(sub1.isDisposed()); |
| 287 |
|
| 288 |
Disposable sub2 = Disposable.empty(); |
| 289 |
|
| 290 |
consumer.onSubscribe(sub2); |
| 291 |
|
| 292 |
assertFalse(sub1.isDisposed()); |
| 293 |
|
| 294 |
assertTrue(sub2.isDisposed()); |
| 295 |
|
| 296 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 297 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 298 |
assertEquals(errors.toString(), 1, errors.size()); |
| 299 |
} |
| 300 |
|
| 301 |
@Test |
| 302 |
public void checkDoubleDisposableSingleObserver() { |
| 303 |
SingleObserver<Integer> consumer = new DisposableSingleObserver<Integer>() { |
| 304 |
@Override |
| 305 |
public void onSuccess(Integer t) { |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void onError(Throwable t) { |
| 310 |
} |
| 311 |
}; |
| 312 |
|
| 313 |
Disposable sub1 = Disposable.empty(); |
| 314 |
|
| 315 |
consumer.onSubscribe(sub1); |
| 316 |
|
| 317 |
assertFalse(sub1.isDisposed()); |
| 318 |
|
| 319 |
Disposable sub2 = Disposable.empty(); |
| 320 |
|
| 321 |
consumer.onSubscribe(sub2); |
| 322 |
|
| 323 |
assertFalse(sub1.isDisposed()); |
| 324 |
|
| 325 |
assertTrue(sub2.isDisposed()); |
| 326 |
|
| 327 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 328 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 329 |
assertEquals(errors.toString(), 1, errors.size()); |
| 330 |
} |
| 331 |
|
| 332 |
@Test |
| 333 |
public void checkDoubleResourceSingleObserver() { |
| 334 |
SingleObserver<Integer> consumer = new ResourceSingleObserver<Integer>() { |
| 335 |
@Override |
| 336 |
public void onSuccess(Integer t) { |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public void onError(Throwable t) { |
| 341 |
} |
| 342 |
}; |
| 343 |
|
| 344 |
Disposable sub1 = Disposable.empty(); |
| 345 |
|
| 346 |
consumer.onSubscribe(sub1); |
| 347 |
|
| 348 |
assertFalse(sub1.isDisposed()); |
| 349 |
|
| 350 |
Disposable sub2 = Disposable.empty(); |
| 351 |
|
| 352 |
consumer.onSubscribe(sub2); |
| 353 |
|
| 354 |
assertFalse(sub1.isDisposed()); |
| 355 |
|
| 356 |
assertTrue(sub2.isDisposed()); |
| 357 |
|
| 358 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 359 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 360 |
assertEquals(errors.toString(), 1, errors.size()); |
| 361 |
} |
| 362 |
|
| 363 |
@Test |
| 364 |
public void checkDoubleDisposableMaybeObserver() { |
| 365 |
MaybeObserver<Integer> consumer = new DisposableMaybeObserver<Integer>() { |
| 366 |
@Override |
| 367 |
public void onSuccess(Integer t) { |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public void onError(Throwable t) { |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public void onComplete() { |
| 376 |
} |
| 377 |
}; |
| 378 |
|
| 379 |
Disposable sub1 = Disposable.empty(); |
| 380 |
|
| 381 |
consumer.onSubscribe(sub1); |
| 382 |
|
| 383 |
assertFalse(sub1.isDisposed()); |
| 384 |
|
| 385 |
Disposable sub2 = Disposable.empty(); |
| 386 |
|
| 387 |
consumer.onSubscribe(sub2); |
| 388 |
|
| 389 |
assertFalse(sub1.isDisposed()); |
| 390 |
|
| 391 |
assertTrue(sub2.isDisposed()); |
| 392 |
|
| 393 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 394 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 395 |
assertEquals(errors.toString(), 1, errors.size()); |
| 396 |
} |
| 397 |
|
| 398 |
@Test |
| 399 |
public void checkDoubleResourceMaybeObserver() { |
| 400 |
MaybeObserver<Integer> consumer = new ResourceMaybeObserver<Integer>() { |
| 401 |
@Override |
| 402 |
public void onSuccess(Integer t) { |
| 403 |
} |
| 404 |
|
| 405 |
@Override |
| 406 |
public void onError(Throwable t) { |
| 407 |
} |
| 408 |
|
| 409 |
@Override |
| 410 |
public void onComplete() { |
| 411 |
} |
| 412 |
}; |
| 413 |
|
| 414 |
Disposable sub1 = Disposable.empty(); |
| 415 |
|
| 416 |
consumer.onSubscribe(sub1); |
| 417 |
|
| 418 |
assertFalse(sub1.isDisposed()); |
| 419 |
|
| 420 |
Disposable sub2 = Disposable.empty(); |
| 421 |
|
| 422 |
consumer.onSubscribe(sub2); |
| 423 |
|
| 424 |
assertFalse(sub1.isDisposed()); |
| 425 |
|
| 426 |
assertTrue(sub2.isDisposed()); |
| 427 |
|
| 428 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 429 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 430 |
assertEquals(errors.toString(), 1, errors.size()); |
| 431 |
} |
| 432 |
|
| 433 |
@Test |
| 434 |
public void checkDoubleDisposableCompletableObserver() { |
| 435 |
CompletableObserver consumer = new DisposableCompletableObserver() { |
| 436 |
@Override |
| 437 |
public void onError(Throwable t) { |
| 438 |
} |
| 439 |
|
| 440 |
@Override |
| 441 |
public void onComplete() { |
| 442 |
} |
| 443 |
}; |
| 444 |
|
| 445 |
Disposable sub1 = Disposable.empty(); |
| 446 |
|
| 447 |
consumer.onSubscribe(sub1); |
| 448 |
|
| 449 |
assertFalse(sub1.isDisposed()); |
| 450 |
|
| 451 |
Disposable sub2 = Disposable.empty(); |
| 452 |
|
| 453 |
consumer.onSubscribe(sub2); |
| 454 |
|
| 455 |
assertFalse(sub1.isDisposed()); |
| 456 |
|
| 457 |
assertTrue(sub2.isDisposed()); |
| 458 |
|
| 459 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 460 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 461 |
assertEquals(errors.toString(), 1, errors.size()); |
| 462 |
} |
| 463 |
|
| 464 |
@Test |
| 465 |
public void checkDoubleResourceCompletableObserver() { |
| 466 |
CompletableObserver consumer = new ResourceCompletableObserver() { |
| 467 |
@Override |
| 468 |
public void onError(Throwable t) { |
| 469 |
} |
| 470 |
|
| 471 |
@Override |
| 472 |
public void onComplete() { |
| 473 |
} |
| 474 |
}; |
| 475 |
|
| 476 |
Disposable sub1 = Disposable.empty(); |
| 477 |
|
| 478 |
consumer.onSubscribe(sub1); |
| 479 |
|
| 480 |
assertFalse(sub1.isDisposed()); |
| 481 |
|
| 482 |
Disposable sub2 = Disposable.empty(); |
| 483 |
|
| 484 |
consumer.onSubscribe(sub2); |
| 485 |
|
| 486 |
assertFalse(sub1.isDisposed()); |
| 487 |
|
| 488 |
assertTrue(sub2.isDisposed()); |
| 489 |
|
| 490 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 491 |
assertEquals(EndConsumerHelper.composeMessage(consumer.getClass().getName()), errors.get(0).getMessage()); |
| 492 |
assertEquals(errors.toString(), 1, errors.size()); |
| 493 |
} |
| 494 |
|
| 495 |
@Test |
| 496 |
public void validateDisposable() { |
| 497 |
Disposable d1 = Disposable.empty(); |
| 498 |
|
| 499 |
assertFalse(EndConsumerHelper.validate(DisposableHelper.DISPOSED, d1, getClass())); |
| 500 |
|
| 501 |
assertTrue(d1.isDisposed()); |
| 502 |
|
| 503 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 504 |
} |
| 505 |
|
| 506 |
@Test |
| 507 |
public void validateSubscription() { |
| 508 |
BooleanSubscription bs1 = new BooleanSubscription(); |
| 509 |
|
| 510 |
assertFalse(EndConsumerHelper.validate(SubscriptionHelper.CANCELLED, bs1, getClass())); |
| 511 |
|
| 512 |
assertTrue(bs1.isCancelled()); |
| 513 |
|
| 514 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 515 |
} |
| 516 |
} |