| 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.single; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 20 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 21 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 22 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 23 |
|
| 24 |
public class SingleCacheTest extends RxJavaTest { |
| 25 |
|
| 26 |
@Test |
| 27 |
public void normal() { |
| 28 |
Single<Integer> cache = Single.just(1).cache(); |
| 29 |
|
| 30 |
cache |
| 31 |
.test() |
| 32 |
.assertResult(1); |
| 33 |
|
| 34 |
cache |
| 35 |
.test() |
| 36 |
.assertResult(1); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void error() { |
| 41 |
Single<Object> cache = Single.error(new TestException()) |
| 42 |
.cache(); |
| 43 |
|
| 44 |
cache |
| 45 |
.test() |
| 46 |
.assertFailure(TestException.class); |
| 47 |
|
| 48 |
cache |
| 49 |
.test() |
| 50 |
.assertFailure(TestException.class); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void delayed() { |
| 55 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 56 |
Single<Integer> cache = ps.single(-99).cache(); |
| 57 |
|
| 58 |
TestObserver<Integer> to1 = cache.test(); |
| 59 |
|
| 60 |
TestObserver<Integer> to2 = cache.test(); |
| 61 |
|
| 62 |
ps.onNext(1); |
| 63 |
ps.onComplete(); |
| 64 |
|
| 65 |
to1.assertResult(1); |
| 66 |
to2.assertResult(1); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void delayedDisposed() { |
| 71 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 72 |
Single<Integer> cache = ps.single(-99).cache(); |
| 73 |
|
| 74 |
TestObserver<Integer> to1 = cache.test(); |
| 75 |
|
| 76 |
TestObserver<Integer> to2 = cache.test(); |
| 77 |
|
| 78 |
to1.dispose(); |
| 79 |
|
| 80 |
ps.onNext(1); |
| 81 |
ps.onComplete(); |
| 82 |
|
| 83 |
to1.assertNoValues().assertNoErrors().assertNotComplete(); |
| 84 |
to2.assertResult(1); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void crossCancel() { |
| 89 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 90 |
Single<Integer> cache = ps.single(-99).cache(); |
| 91 |
|
| 92 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 93 |
|
| 94 |
TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() { |
| 95 |
@Override |
| 96 |
public void onNext(Integer t) { |
| 97 |
super.onNext(t); |
| 98 |
ts1.cancel(); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
cache.toFlowable().subscribe(ts2); |
| 103 |
cache.toFlowable().subscribe(ts1); |
| 104 |
|
| 105 |
ps.onNext(1); |
| 106 |
ps.onComplete(); |
| 107 |
|
| 108 |
ts1.assertNoValues().assertNoErrors().assertNotComplete(); |
| 109 |
ts2.assertResult(1); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void crossCancelOnError() { |
| 114 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 115 |
Single<Integer> cache = ps.single(-99).cache(); |
| 116 |
|
| 117 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 118 |
|
| 119 |
TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() { |
| 120 |
@Override |
| 121 |
public void onError(Throwable t) { |
| 122 |
super.onError(t); |
| 123 |
ts1.cancel(); |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
cache.toFlowable().subscribe(ts2); |
| 128 |
cache.toFlowable().subscribe(ts1); |
| 129 |
|
| 130 |
ps.onError(new TestException()); |
| 131 |
|
| 132 |
ts1.assertNoValues().assertNoErrors().assertNotComplete(); |
| 133 |
ts2.assertFailure(TestException.class); |
| 134 |
} |
| 135 |
|
| 136 |
} |