| 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.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.*; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.junit.*; |
| 20 |
import org.reactivestreams.Subscription; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 26 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 27 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class FlowableDelaySubscriptionOtherTest extends RxJavaTest { |
| 31 |
@Test |
| 32 |
public void noPrematureSubscription() { |
| 33 |
PublishProcessor<Object> other = PublishProcessor.create(); |
| 34 |
|
| 35 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 36 |
|
| 37 |
final AtomicInteger subscribed = new AtomicInteger(); |
| 38 |
|
| 39 |
Flowable.just(1) |
| 40 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 41 |
@Override |
| 42 |
public void accept(Subscription s) { |
| 43 |
subscribed.getAndIncrement(); |
| 44 |
} |
| 45 |
}) |
| 46 |
.delaySubscription(other) |
| 47 |
.subscribe(ts); |
| 48 |
|
| 49 |
ts.assertNotComplete(); |
| 50 |
ts.assertNoErrors(); |
| 51 |
ts.assertNoValues(); |
| 52 |
|
| 53 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 54 |
|
| 55 |
other.onNext(1); |
| 56 |
|
| 57 |
Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 58 |
|
| 59 |
ts.assertValue(1); |
| 60 |
ts.assertNoErrors(); |
| 61 |
ts.assertComplete(); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void noMultipleSubscriptions() { |
| 66 |
PublishProcessor<Object> other = PublishProcessor.create(); |
| 67 |
|
| 68 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 69 |
|
| 70 |
final AtomicInteger subscribed = new AtomicInteger(); |
| 71 |
|
| 72 |
Flowable.just(1) |
| 73 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 74 |
@Override |
| 75 |
public void accept(Subscription s) { |
| 76 |
subscribed.getAndIncrement(); |
| 77 |
} |
| 78 |
}) |
| 79 |
.delaySubscription(other) |
| 80 |
.subscribe(ts); |
| 81 |
|
| 82 |
ts.assertNotComplete(); |
| 83 |
ts.assertNoErrors(); |
| 84 |
ts.assertNoValues(); |
| 85 |
|
| 86 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 87 |
|
| 88 |
other.onNext(1); |
| 89 |
other.onNext(2); |
| 90 |
|
| 91 |
Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 92 |
|
| 93 |
ts.assertValue(1); |
| 94 |
ts.assertNoErrors(); |
| 95 |
ts.assertComplete(); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void completeTriggersSubscription() { |
| 100 |
PublishProcessor<Object> other = PublishProcessor.create(); |
| 101 |
|
| 102 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 103 |
|
| 104 |
final AtomicInteger subscribed = new AtomicInteger(); |
| 105 |
|
| 106 |
Flowable.just(1) |
| 107 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 108 |
@Override |
| 109 |
public void accept(Subscription s) { |
| 110 |
subscribed.getAndIncrement(); |
| 111 |
} |
| 112 |
}) |
| 113 |
.delaySubscription(other) |
| 114 |
.subscribe(ts); |
| 115 |
|
| 116 |
ts.assertNotComplete(); |
| 117 |
ts.assertNoErrors(); |
| 118 |
ts.assertNoValues(); |
| 119 |
|
| 120 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 121 |
|
| 122 |
other.onComplete(); |
| 123 |
|
| 124 |
Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 125 |
|
| 126 |
ts.assertValue(1); |
| 127 |
ts.assertNoErrors(); |
| 128 |
ts.assertComplete(); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void noPrematureSubscriptionToError() { |
| 133 |
PublishProcessor<Object> other = PublishProcessor.create(); |
| 134 |
|
| 135 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 136 |
|
| 137 |
final AtomicInteger subscribed = new AtomicInteger(); |
| 138 |
|
| 139 |
Flowable.<Integer>error(new TestException()) |
| 140 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 141 |
@Override |
| 142 |
public void accept(Subscription s) { |
| 143 |
subscribed.getAndIncrement(); |
| 144 |
} |
| 145 |
}) |
| 146 |
.delaySubscription(other) |
| 147 |
.subscribe(ts); |
| 148 |
|
| 149 |
ts.assertNotComplete(); |
| 150 |
ts.assertNoErrors(); |
| 151 |
ts.assertNoValues(); |
| 152 |
|
| 153 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 154 |
|
| 155 |
other.onComplete(); |
| 156 |
|
| 157 |
Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 158 |
|
| 159 |
ts.assertNoValues(); |
| 160 |
ts.assertNotComplete(); |
| 161 |
ts.assertError(TestException.class); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void noSubscriptionIfOtherErrors() { |
| 166 |
PublishProcessor<Object> other = PublishProcessor.create(); |
| 167 |
|
| 168 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 169 |
|
| 170 |
final AtomicInteger subscribed = new AtomicInteger(); |
| 171 |
|
| 172 |
Flowable.<Integer>error(new TestException()) |
| 173 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 174 |
@Override |
| 175 |
public void accept(Subscription s) { |
| 176 |
subscribed.getAndIncrement(); |
| 177 |
} |
| 178 |
}) |
| 179 |
.delaySubscription(other) |
| 180 |
.subscribe(ts); |
| 181 |
|
| 182 |
ts.assertNotComplete(); |
| 183 |
ts.assertNoErrors(); |
| 184 |
ts.assertNoValues(); |
| 185 |
|
| 186 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 187 |
|
| 188 |
other.onError(new TestException()); |
| 189 |
|
| 190 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 191 |
|
| 192 |
ts.assertNoValues(); |
| 193 |
ts.assertNotComplete(); |
| 194 |
ts.assertError(TestException.class); |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void backpressurePassesThrough() { |
| 199 |
|
| 200 |
PublishProcessor<Object> other = PublishProcessor.create(); |
| 201 |
|
| 202 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 203 |
|
| 204 |
final AtomicInteger subscribed = new AtomicInteger(); |
| 205 |
|
| 206 |
Flowable.just(1, 2, 3, 4, 5) |
| 207 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 208 |
@Override |
| 209 |
public void accept(Subscription s) { |
| 210 |
subscribed.getAndIncrement(); |
| 211 |
} |
| 212 |
}) |
| 213 |
.delaySubscription(other) |
| 214 |
.subscribe(ts); |
| 215 |
|
| 216 |
ts.assertNotComplete(); |
| 217 |
ts.assertNoErrors(); |
| 218 |
ts.assertNoValues(); |
| 219 |
|
| 220 |
Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 221 |
|
| 222 |
other.onNext(1); |
| 223 |
|
| 224 |
Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 225 |
|
| 226 |
Assert.assertFalse("Not unsubscribed from other", other.hasSubscribers()); |
| 227 |
|
| 228 |
ts.assertNotComplete(); |
| 229 |
ts.assertNoErrors(); |
| 230 |
ts.assertNoValues(); |
| 231 |
|
| 232 |
ts.request(1); |
| 233 |
ts.assertValue(1); |
| 234 |
ts.assertNoErrors(); |
| 235 |
ts.assertNotComplete(); |
| 236 |
|
| 237 |
ts.request(2); |
| 238 |
ts.assertValues(1, 2, 3); |
| 239 |
ts.assertNoErrors(); |
| 240 |
ts.assertNotComplete(); |
| 241 |
|
| 242 |
ts.request(10); |
| 243 |
ts.assertValues(1, 2, 3, 4, 5); |
| 244 |
ts.assertNoErrors(); |
| 245 |
ts.assertComplete(); |
| 246 |
} |
| 247 |
|
| 248 |
@Test |
| 249 |
public void unsubscriptionPropagatesBeforeSubscribe() { |
| 250 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 251 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 252 |
|
| 253 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 254 |
|
| 255 |
source.delaySubscription(other).subscribe(ts); |
| 256 |
|
| 257 |
Assert.assertFalse("source subscribed?", source.hasSubscribers()); |
| 258 |
Assert.assertTrue("other not subscribed?", other.hasSubscribers()); |
| 259 |
|
| 260 |
ts.cancel(); |
| 261 |
|
| 262 |
Assert.assertFalse("source subscribed?", source.hasSubscribers()); |
| 263 |
Assert.assertFalse("other still subscribed?", other.hasSubscribers()); |
| 264 |
} |
| 265 |
|
| 266 |
@Test |
| 267 |
public void unsubscriptionPropagatesAfterSubscribe() { |
| 268 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 269 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 270 |
|
| 271 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 272 |
|
| 273 |
source.delaySubscription(other).subscribe(ts); |
| 274 |
|
| 275 |
Assert.assertFalse("source subscribed?", source.hasSubscribers()); |
| 276 |
Assert.assertTrue("other not subscribed?", other.hasSubscribers()); |
| 277 |
|
| 278 |
other.onComplete(); |
| 279 |
|
| 280 |
Assert.assertTrue("source not subscribed?", source.hasSubscribers()); |
| 281 |
Assert.assertFalse("other still subscribed?", other.hasSubscribers()); |
| 282 |
|
| 283 |
ts.cancel(); |
| 284 |
|
| 285 |
Assert.assertFalse("source subscribed?", source.hasSubscribers()); |
| 286 |
Assert.assertFalse("other still subscribed?", other.hasSubscribers()); |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
public void delayAndTakeUntilNeverSubscribeToSource() { |
| 291 |
PublishProcessor<Integer> delayUntil = PublishProcessor.create(); |
| 292 |
PublishProcessor<Integer> interrupt = PublishProcessor.create(); |
| 293 |
final AtomicBoolean subscribed = new AtomicBoolean(false); |
| 294 |
|
| 295 |
Flowable.just(1) |
| 296 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 297 |
@Override |
| 298 |
public void accept(Subscription s) { |
| 299 |
subscribed.set(true); |
| 300 |
} |
| 301 |
}) |
| 302 |
.delaySubscription(delayUntil) |
| 303 |
.takeUntil(interrupt) |
| 304 |
.subscribe(); |
| 305 |
|
| 306 |
interrupt.onNext(9000); |
| 307 |
delayUntil.onNext(1); |
| 308 |
|
| 309 |
Assert.assertFalse(subscribed.get()); |
| 310 |
} |
| 311 |
|
| 312 |
@Test |
| 313 |
public void badSourceOther() { |
| 314 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 315 |
@Override |
| 316 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 317 |
return Flowable.just(1).delaySubscription(f); |
| 318 |
} |
| 319 |
}, false, 1, 1, 1); |
| 320 |
} |
| 321 |
|
| 322 |
@Test |
| 323 |
public void afterDelayNoInterrupt() { |
| 324 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 325 |
try { |
| 326 |
for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec) }) { |
| 327 |
final TestSubscriber<Boolean> ts = TestSubscriber.create(); |
| 328 |
ts.withTag(s.getClass().getSimpleName()); |
| 329 |
|
| 330 |
Flowable.<Boolean>create(new FlowableOnSubscribe<Boolean>() { |
| 331 |
@Override |
| 332 |
public void subscribe(FlowableEmitter<Boolean> emitter) throws Exception { |
| 333 |
emitter.onNext(Thread.interrupted()); |
| 334 |
emitter.onComplete(); |
| 335 |
} |
| 336 |
}, BackpressureStrategy.MISSING) |
| 337 |
.delaySubscription(100, TimeUnit.MILLISECONDS, s) |
| 338 |
.subscribe(ts); |
| 339 |
|
| 340 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 341 |
ts.assertValue(false); |
| 342 |
} |
| 343 |
} finally { |
| 344 |
exec.shutdown(); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
@Test |
| 349 |
public void doubleOnSubscribeMain() { |
| 350 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.delaySubscription(Flowable.empty())); |
| 351 |
} |
| 352 |
|
| 353 |
@Test |
| 354 |
public void doubleOnSubscribeOther() { |
| 355 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> PublishProcessor.create().delaySubscription(f)); |
| 356 |
} |
| 357 |
|
| 358 |
@Test |
| 359 |
public void badRequest() { |
| 360 |
TestHelper.assertBadRequestReported(PublishProcessor.create().delaySubscription(Flowable.empty())); |
| 361 |
} |
| 362 |
} |