| 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 static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 27 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber; |
| 30 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 31 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 32 |
import io.reactivex.rxjava3.schedulers.*; |
| 33 |
import io.reactivex.rxjava3.subscribers.*; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class FlowableSubscribeOnTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void issue813() throws InterruptedException { |
| 40 |
// https://github.com/ReactiveX/RxJava/issues/813 |
| 41 |
final CountDownLatch scheduled = new CountDownLatch(1); |
| 42 |
final CountDownLatch latch = new CountDownLatch(1); |
| 43 |
final CountDownLatch doneLatch = new CountDownLatch(1); |
| 44 |
|
| 45 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 46 |
|
| 47 |
Flowable |
| 48 |
.unsafeCreate(new Publisher<Integer>() { |
| 49 |
@Override |
| 50 |
public void subscribe( |
| 51 |
final Subscriber<? super Integer> subscriber) { |
| 52 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 53 |
scheduled.countDown(); |
| 54 |
try { |
| 55 |
try { |
| 56 |
latch.await(); |
| 57 |
} catch (InterruptedException e) { |
| 58 |
// this means we were unsubscribed (Scheduler shut down and interrupts) |
| 59 |
// ... but we'll pretend we are like many Flowables that ignore interrupts |
| 60 |
} |
| 61 |
|
| 62 |
subscriber.onComplete(); |
| 63 |
} catch (Throwable e) { |
| 64 |
subscriber.onError(e); |
| 65 |
} finally { |
| 66 |
doneLatch.countDown(); |
| 67 |
} |
| 68 |
} |
| 69 |
}).subscribeOn(Schedulers.computation()).subscribe(ts); |
| 70 |
|
| 71 |
// wait for scheduling |
| 72 |
scheduled.await(); |
| 73 |
// trigger unsubscribe |
| 74 |
ts.cancel(); |
| 75 |
latch.countDown(); |
| 76 |
doneLatch.await(); |
| 77 |
ts.assertNoErrors(); |
| 78 |
ts.assertComplete(); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void onError() { |
| 83 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(); |
| 84 |
Flowable.unsafeCreate(new Publisher<String>() { |
| 85 |
|
| 86 |
@Override |
| 87 |
public void subscribe(Subscriber<? super String> s) { |
| 88 |
s.onSubscribe(new BooleanSubscription()); |
| 89 |
s.onError(new RuntimeException("fail")); |
| 90 |
} |
| 91 |
|
| 92 |
}).subscribeOn(Schedulers.computation()).subscribe(ts); |
| 93 |
ts.awaitDone(1000, TimeUnit.MILLISECONDS); |
| 94 |
ts.assertTerminated(); |
| 95 |
} |
| 96 |
|
| 97 |
public static class SlowScheduler extends Scheduler { |
| 98 |
final Scheduler actual; |
| 99 |
final long delay; |
| 100 |
final TimeUnit unit; |
| 101 |
|
| 102 |
public SlowScheduler() { |
| 103 |
this(Schedulers.computation(), 2, TimeUnit.SECONDS); |
| 104 |
} |
| 105 |
|
| 106 |
public SlowScheduler(Scheduler actual, long delay, TimeUnit unit) { |
| 107 |
this.actual = actual; |
| 108 |
this.delay = delay; |
| 109 |
this.unit = unit; |
| 110 |
} |
| 111 |
|
| 112 |
@NonNull |
| 113 |
@Override |
| 114 |
public Worker createWorker() { |
| 115 |
return new SlowInner(actual.createWorker()); |
| 116 |
} |
| 117 |
|
| 118 |
private final class SlowInner extends Worker { |
| 119 |
|
| 120 |
private final Scheduler.Worker actualInner; |
| 121 |
|
| 122 |
private SlowInner(Worker actual) { |
| 123 |
this.actualInner = actual; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void dispose() { |
| 128 |
actualInner.dispose(); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public boolean isDisposed() { |
| 133 |
return actualInner.isDisposed(); |
| 134 |
} |
| 135 |
|
| 136 |
@NonNull |
| 137 |
@Override |
| 138 |
public Disposable schedule(@NonNull final Runnable action) { |
| 139 |
return actualInner.schedule(action, delay, unit); |
| 140 |
} |
| 141 |
|
| 142 |
@NonNull |
| 143 |
@Override |
| 144 |
public Disposable schedule(@NonNull final Runnable action, final long delayTime, @NonNull final TimeUnit delayUnit) { |
| 145 |
TimeUnit common = delayUnit.compareTo(unit) < 0 ? delayUnit : unit; |
| 146 |
long t = common.convert(delayTime, delayUnit) + common.convert(delay, unit); |
| 147 |
return actualInner.schedule(action, t, common); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void unsubscribeInfiniteStream() throws InterruptedException { |
| 156 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 157 |
final AtomicInteger count = new AtomicInteger(); |
| 158 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 159 |
|
| 160 |
@Override |
| 161 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 162 |
BooleanSubscription bs = new BooleanSubscription(); |
| 163 |
sub.onSubscribe(bs); |
| 164 |
for (int i = 1; !bs.isCancelled(); i++) { |
| 165 |
count.incrementAndGet(); |
| 166 |
sub.onNext(i); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
}).subscribeOn(Schedulers.newThread()).take(10).subscribe(ts); |
| 171 |
|
| 172 |
ts.awaitDone(1000, TimeUnit.MILLISECONDS); |
| 173 |
ts.cancel(); |
| 174 |
Thread.sleep(200); // give time for the loop to continue |
| 175 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 176 |
assertEquals(10, count.get()); |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
public void backpressureReschedulesCorrectly() throws InterruptedException { |
| 181 |
final CountDownLatch latch = new CountDownLatch(10); |
| 182 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(new DefaultSubscriber<Integer>() { |
| 183 |
|
| 184 |
@Override |
| 185 |
public void onComplete() { |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void onError(Throwable e) { |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void onNext(Integer t) { |
| 194 |
latch.countDown(); |
| 195 |
} |
| 196 |
|
| 197 |
}); |
| 198 |
ts.request(10); |
| 199 |
Flowable.range(1, 10000000).subscribeOn(Schedulers.newThread()).take(20).subscribe(ts); |
| 200 |
latch.await(); |
| 201 |
Thread t = ts.lastThread(); |
| 202 |
System.out.println("First schedule: " + t); |
| 203 |
assertTrue(t.getName().startsWith("Rx")); |
| 204 |
ts.request(10); |
| 205 |
ts.awaitDone(20, TimeUnit.SECONDS); |
| 206 |
System.out.println("After reschedule: " + ts.lastThread()); |
| 207 |
assertEquals(t, ts.lastThread()); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void setProducerSynchronousRequest() { |
| 212 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 213 |
Flowable.just(1, 2, 3).lift(new FlowableOperator<Integer, Integer>() { |
| 214 |
|
| 215 |
@Override |
| 216 |
public Subscriber<? super Integer> apply(final Subscriber<? super Integer> child) { |
| 217 |
final AtomicLong requested = new AtomicLong(); |
| 218 |
child.onSubscribe(new Subscription() { |
| 219 |
|
| 220 |
@Override |
| 221 |
public void request(long n) { |
| 222 |
if (!requested.compareAndSet(0, n)) { |
| 223 |
child.onError(new RuntimeException("Expected to receive request before onNext but didn't")); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public void cancel() { |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
}); |
| 233 |
Subscriber<Integer> parent = new DefaultSubscriber<Integer>() { |
| 234 |
|
| 235 |
@Override |
| 236 |
public void onComplete() { |
| 237 |
child.onComplete(); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public void onError(Throwable e) { |
| 242 |
child.onError(e); |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public void onNext(Integer t) { |
| 247 |
if (requested.compareAndSet(0, -99)) { |
| 248 |
child.onError(new RuntimeException("Got values before requested")); |
| 249 |
} |
| 250 |
} |
| 251 |
}; |
| 252 |
|
| 253 |
return parent; |
| 254 |
} |
| 255 |
|
| 256 |
}).subscribeOn(Schedulers.newThread()).subscribe(ts); |
| 257 |
ts.awaitDone(20, TimeUnit.SECONDS); |
| 258 |
ts.assertNoErrors(); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void cancelBeforeActualSubscribe() { |
| 263 |
TestScheduler test = new TestScheduler(); |
| 264 |
|
| 265 |
TestSubscriberEx<Integer> ts = Flowable.just(1) |
| 266 |
.hide() |
| 267 |
.subscribeOn(test) |
| 268 |
.to(TestHelper.<Integer>testConsumer(true)); |
| 269 |
|
| 270 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 271 |
|
| 272 |
ts |
| 273 |
.assertSubscribed() |
| 274 |
.assertNoValues() |
| 275 |
.assertNotTerminated(); |
| 276 |
} |
| 277 |
|
| 278 |
@Test |
| 279 |
public void dispose() { |
| 280 |
TestHelper.checkDisposed(Flowable.just(1).subscribeOn(Schedulers.single())); |
| 281 |
} |
| 282 |
|
| 283 |
@Test |
| 284 |
public void deferredRequestRace() { |
| 285 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 286 |
|
| 287 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 288 |
|
| 289 |
Worker w = Schedulers.computation().createWorker(); |
| 290 |
|
| 291 |
final SubscribeOnSubscriber<Integer> so = new SubscribeOnSubscriber<>(ts, w, Flowable.<Integer>never(), true); |
| 292 |
ts.onSubscribe(so); |
| 293 |
|
| 294 |
final BooleanSubscription bs = new BooleanSubscription(); |
| 295 |
|
| 296 |
try { |
| 297 |
Runnable r1 = new Runnable() { |
| 298 |
@Override |
| 299 |
public void run() { |
| 300 |
so.onSubscribe(bs); |
| 301 |
} |
| 302 |
}; |
| 303 |
|
| 304 |
Runnable r2 = new Runnable() { |
| 305 |
@Override |
| 306 |
public void run() { |
| 307 |
so.request(1); |
| 308 |
} |
| 309 |
}; |
| 310 |
|
| 311 |
TestHelper.race(r1, r2); |
| 312 |
} finally { |
| 313 |
w.dispose(); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
@Test |
| 319 |
public void nonScheduledRequests() { |
| 320 |
TestSubscriber<Object> ts = Flowable.create(new FlowableOnSubscribe<Object>() { |
| 321 |
@Override |
| 322 |
public void subscribe(FlowableEmitter<Object> s) throws Exception { |
| 323 |
for (int i = 1; i < 1001; i++) { |
| 324 |
s.onNext(i); |
| 325 |
Thread.sleep(1); |
| 326 |
} |
| 327 |
s.onComplete(); |
| 328 |
} |
| 329 |
}, BackpressureStrategy.DROP) |
| 330 |
.subscribeOn(Schedulers.single()) |
| 331 |
.observeOn(Schedulers.computation()) |
| 332 |
.test() |
| 333 |
.awaitDone(20, TimeUnit.SECONDS) |
| 334 |
.assertNoErrors() |
| 335 |
.assertComplete(); |
| 336 |
|
| 337 |
int c = ts.values().size(); |
| 338 |
|
| 339 |
assertTrue("" + c, c > Flowable.bufferSize()); |
| 340 |
} |
| 341 |
|
| 342 |
@Test |
| 343 |
public void scheduledRequests() { |
| 344 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 345 |
@Override |
| 346 |
public void subscribe(FlowableEmitter<Object> s) throws Exception { |
| 347 |
for (int i = 1; i < 1001; i++) { |
| 348 |
s.onNext(i); |
| 349 |
Thread.sleep(1); |
| 350 |
} |
| 351 |
s.onComplete(); |
| 352 |
} |
| 353 |
}, BackpressureStrategy.DROP) |
| 354 |
.map(Functions.identity()) |
| 355 |
.subscribeOn(Schedulers.single()) |
| 356 |
.observeOn(Schedulers.computation()) |
| 357 |
.test() |
| 358 |
.awaitDone(20, TimeUnit.SECONDS) |
| 359 |
.assertValueCount(Flowable.bufferSize()) |
| 360 |
.assertNoErrors() |
| 361 |
.assertComplete(); |
| 362 |
} |
| 363 |
|
| 364 |
@Test |
| 365 |
public void nonScheduledRequestsNotSubsequentSubscribeOn() { |
| 366 |
TestSubscriber<Object> ts = Flowable.create(new FlowableOnSubscribe<Object>() { |
| 367 |
@Override |
| 368 |
public void subscribe(FlowableEmitter<Object> s) throws Exception { |
| 369 |
for (int i = 1; i < 1001; i++) { |
| 370 |
s.onNext(i); |
| 371 |
Thread.sleep(1); |
| 372 |
} |
| 373 |
s.onComplete(); |
| 374 |
} |
| 375 |
}, BackpressureStrategy.DROP) |
| 376 |
.map(Functions.identity()) |
| 377 |
.subscribeOn(Schedulers.single(), false) |
| 378 |
.observeOn(Schedulers.computation()) |
| 379 |
.test() |
| 380 |
.awaitDone(20, TimeUnit.SECONDS) |
| 381 |
.assertNoErrors() |
| 382 |
.assertComplete(); |
| 383 |
|
| 384 |
int c = ts.values().size(); |
| 385 |
|
| 386 |
assertTrue("" + c, c > Flowable.bufferSize()); |
| 387 |
} |
| 388 |
|
| 389 |
@Test |
| 390 |
public void scheduledRequestsNotSubsequentSubscribeOn() { |
| 391 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 392 |
@Override |
| 393 |
public void subscribe(FlowableEmitter<Object> s) throws Exception { |
| 394 |
for (int i = 1; i < 1001; i++) { |
| 395 |
s.onNext(i); |
| 396 |
Thread.sleep(1); |
| 397 |
} |
| 398 |
s.onComplete(); |
| 399 |
} |
| 400 |
}, BackpressureStrategy.DROP) |
| 401 |
.map(Functions.identity()) |
| 402 |
.subscribeOn(Schedulers.single(), true) |
| 403 |
.observeOn(Schedulers.computation()) |
| 404 |
.test() |
| 405 |
.awaitDone(20, TimeUnit.SECONDS) |
| 406 |
.assertValueCount(Flowable.bufferSize()) |
| 407 |
.assertNoErrors() |
| 408 |
.assertComplete(); |
| 409 |
} |
| 410 |
|
| 411 |
@Test |
| 412 |
public void badRequest() { |
| 413 |
TestHelper.assertBadRequestReported(Flowable.never().subscribeOn(ImmediateThinScheduler.INSTANCE)); |
| 414 |
} |
| 415 |
} |