| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 23 |
import io.reactivex.rxjava3.functions.*; |
| 24 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 27 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class MaybeCacheTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void offlineSuccess() { |
| 34 |
Maybe<Integer> source = Maybe.just(1).cache(); |
| 35 |
assertEquals(1, source.blockingGet().intValue()); |
| 36 |
|
| 37 |
source.test() |
| 38 |
.assertResult(1); |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
public void offlineError() { |
| 43 |
Maybe<Integer> source = Maybe.<Integer>error(new TestException()).cache(); |
| 44 |
|
| 45 |
try { |
| 46 |
source.blockingGet(); |
| 47 |
fail("Should have thrown"); |
| 48 |
} catch (TestException ex) { |
| 49 |
// expected |
| 50 |
} |
| 51 |
|
| 52 |
source.test() |
| 53 |
.assertFailure(TestException.class); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void offlineComplete() { |
| 58 |
Maybe<Integer> source = Maybe.<Integer>empty().cache(); |
| 59 |
|
| 60 |
assertNull(source.blockingGet()); |
| 61 |
|
| 62 |
source.test() |
| 63 |
.assertResult(); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void onlineSuccess() { |
| 68 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 69 |
|
| 70 |
Maybe<Integer> source = pp.singleElement().cache(); |
| 71 |
|
| 72 |
assertFalse(pp.hasSubscribers()); |
| 73 |
|
| 74 |
assertNotNull(((MaybeCache<Integer>)source).source.get()); |
| 75 |
|
| 76 |
TestObserver<Integer> to = source.test(); |
| 77 |
|
| 78 |
assertNull(((MaybeCache<Integer>)source).source.get()); |
| 79 |
|
| 80 |
assertTrue(pp.hasSubscribers()); |
| 81 |
|
| 82 |
source.test(true).assertEmpty(); |
| 83 |
|
| 84 |
to.assertEmpty(); |
| 85 |
|
| 86 |
pp.onNext(1); |
| 87 |
pp.onComplete(); |
| 88 |
|
| 89 |
to.assertResult(1); |
| 90 |
|
| 91 |
source.test().assertResult(1); |
| 92 |
|
| 93 |
source.test(true).assertEmpty(); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void onlineError() { |
| 98 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 99 |
|
| 100 |
Maybe<Integer> source = pp.singleElement().cache(); |
| 101 |
|
| 102 |
assertFalse(pp.hasSubscribers()); |
| 103 |
|
| 104 |
assertNotNull(((MaybeCache<Integer>)source).source.get()); |
| 105 |
|
| 106 |
TestObserver<Integer> to = source.test(); |
| 107 |
|
| 108 |
assertNull(((MaybeCache<Integer>)source).source.get()); |
| 109 |
|
| 110 |
assertTrue(pp.hasSubscribers()); |
| 111 |
|
| 112 |
source.test(true).assertEmpty(); |
| 113 |
|
| 114 |
to.assertEmpty(); |
| 115 |
|
| 116 |
pp.onError(new TestException()); |
| 117 |
|
| 118 |
to.assertFailure(TestException.class); |
| 119 |
|
| 120 |
source.test().assertFailure(TestException.class); |
| 121 |
|
| 122 |
source.test(true).assertEmpty(); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void onlineComplete() { |
| 127 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 128 |
|
| 129 |
Maybe<Integer> source = pp.singleElement().cache(); |
| 130 |
|
| 131 |
assertFalse(pp.hasSubscribers()); |
| 132 |
|
| 133 |
assertNotNull(((MaybeCache<Integer>)source).source.get()); |
| 134 |
|
| 135 |
TestObserver<Integer> to = source.test(); |
| 136 |
|
| 137 |
assertNull(((MaybeCache<Integer>)source).source.get()); |
| 138 |
|
| 139 |
assertTrue(pp.hasSubscribers()); |
| 140 |
|
| 141 |
source.test(true).assertEmpty(); |
| 142 |
|
| 143 |
to.assertEmpty(); |
| 144 |
|
| 145 |
pp.onComplete(); |
| 146 |
|
| 147 |
to.assertResult(); |
| 148 |
|
| 149 |
source.test().assertResult(); |
| 150 |
|
| 151 |
source.test(true).assertEmpty(); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void crossCancelOnSuccess() { |
| 156 |
|
| 157 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 158 |
|
| 159 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 160 |
|
| 161 |
Maybe<Integer> source = pp.singleElement().cache(); |
| 162 |
|
| 163 |
source.subscribe(new Consumer<Integer>() { |
| 164 |
@Override |
| 165 |
public void accept(Integer v) throws Exception { |
| 166 |
ts.cancel(); |
| 167 |
} |
| 168 |
}); |
| 169 |
|
| 170 |
source.toFlowable().subscribe(ts); |
| 171 |
|
| 172 |
pp.onNext(1); |
| 173 |
pp.onComplete(); |
| 174 |
|
| 175 |
ts.assertEmpty(); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void crossCancelOnError() { |
| 180 |
|
| 181 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 182 |
|
| 183 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 184 |
|
| 185 |
Maybe<Integer> source = pp.singleElement().cache(); |
| 186 |
|
| 187 |
source.subscribe(Functions.emptyConsumer(), new Consumer<Object>() { |
| 188 |
@Override |
| 189 |
public void accept(Object v) throws Exception { |
| 190 |
ts.cancel(); |
| 191 |
} |
| 192 |
}); |
| 193 |
|
| 194 |
source.toFlowable().subscribe(ts); |
| 195 |
|
| 196 |
pp.onError(new TestException()); |
| 197 |
|
| 198 |
ts.assertEmpty(); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void crossCancelOnComplete() { |
| 203 |
|
| 204 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 205 |
|
| 206 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 207 |
|
| 208 |
Maybe<Integer> source = pp.singleElement().cache(); |
| 209 |
|
| 210 |
source.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer(), new Action() { |
| 211 |
@Override |
| 212 |
public void run() throws Exception { |
| 213 |
ts.cancel(); |
| 214 |
} |
| 215 |
}); |
| 216 |
|
| 217 |
source.toFlowable().subscribe(ts); |
| 218 |
|
| 219 |
pp.onComplete(); |
| 220 |
|
| 221 |
ts.assertEmpty(); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void addAddRace() { |
| 226 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 227 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 228 |
|
| 229 |
final Maybe<Integer> source = pp.singleElement().cache(); |
| 230 |
|
| 231 |
Runnable r = new Runnable() { |
| 232 |
@Override |
| 233 |
public void run() { |
| 234 |
source.test(); |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
TestHelper.race(r, r); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
@Test |
| 243 |
public void removeRemoveRace() { |
| 244 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 245 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 246 |
|
| 247 |
final Maybe<Integer> source = pp.singleElement().cache(); |
| 248 |
|
| 249 |
final TestObserver<Integer> to1 = source.test(); |
| 250 |
final TestObserver<Integer> to2 = source.test(); |
| 251 |
|
| 252 |
Runnable r1 = new Runnable() { |
| 253 |
@Override |
| 254 |
public void run() { |
| 255 |
to1.dispose(); |
| 256 |
} |
| 257 |
}; |
| 258 |
|
| 259 |
Runnable r2 = new Runnable() { |
| 260 |
@Override |
| 261 |
public void run() { |
| 262 |
to2.dispose(); |
| 263 |
} |
| 264 |
}; |
| 265 |
|
| 266 |
TestHelper.race(r1, r2); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
@Test |
| 271 |
public void doubleDispose() { |
| 272 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 273 |
|
| 274 |
final Maybe<Integer> source = pp.singleElement().cache(); |
| 275 |
|
| 276 |
final Disposable[] dout = { null }; |
| 277 |
|
| 278 |
source.subscribe(new MaybeObserver<Integer>() { |
| 279 |
|
| 280 |
@Override |
| 281 |
public void onSubscribe(Disposable d) { |
| 282 |
dout[0] = d; |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
public void onSuccess(Integer value) { |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public void onError(Throwable e) { |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
public void onComplete() { |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
}); |
| 301 |
|
| 302 |
dout[0].dispose(); |
| 303 |
dout[0].dispose(); |
| 304 |
} |
| 305 |
} |