| 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.schedulers; |
| 15 |
|
| 16 |
import static io.reactivex.rxjava3.core.Flowable.*; |
| 17 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 18 |
import static org.junit.Assert.*; |
| 19 |
|
| 20 |
import java.util.concurrent.atomic.AtomicInteger; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 26 |
import io.reactivex.rxjava3.disposables.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.schedulers.SchedulerWhen.*; |
| 30 |
import io.reactivex.rxjava3.observers.DisposableCompletableObserver; |
| 31 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 32 |
import io.reactivex.rxjava3.schedulers.*; |
| 33 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 34 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 35 |
|
| 36 |
public class SchedulerWhenTest extends RxJavaTest { |
| 37 |
@Test |
| 38 |
public void asyncMaxConcurrent() { |
| 39 |
TestScheduler tSched = new TestScheduler(); |
| 40 |
SchedulerWhen sched = maxConcurrentScheduler(tSched); |
| 41 |
TestSubscriber<Long> tSub = TestSubscriber.create(); |
| 42 |
|
| 43 |
asyncWork(sched).subscribe(tSub); |
| 44 |
|
| 45 |
tSub.assertValueCount(0); |
| 46 |
|
| 47 |
tSched.advanceTimeBy(0, SECONDS); |
| 48 |
tSub.assertValueCount(0); |
| 49 |
|
| 50 |
tSched.advanceTimeBy(1, SECONDS); |
| 51 |
tSub.assertValueCount(2); |
| 52 |
|
| 53 |
tSched.advanceTimeBy(1, SECONDS); |
| 54 |
tSub.assertValueCount(4); |
| 55 |
|
| 56 |
tSched.advanceTimeBy(1, SECONDS); |
| 57 |
tSub.assertValueCount(5); |
| 58 |
tSub.assertComplete(); |
| 59 |
|
| 60 |
sched.dispose(); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void asyncDelaySubscription() { |
| 65 |
final TestScheduler tSched = new TestScheduler(); |
| 66 |
SchedulerWhen sched = throttleScheduler(tSched); |
| 67 |
TestSubscriber<Long> tSub = TestSubscriber.create(); |
| 68 |
|
| 69 |
asyncWork(sched).subscribe(tSub); |
| 70 |
|
| 71 |
tSub.assertValueCount(0); |
| 72 |
|
| 73 |
tSched.advanceTimeBy(0, SECONDS); |
| 74 |
tSub.assertValueCount(0); |
| 75 |
|
| 76 |
tSched.advanceTimeBy(1, SECONDS); |
| 77 |
tSub.assertValueCount(1); |
| 78 |
|
| 79 |
tSched.advanceTimeBy(1, SECONDS); |
| 80 |
tSub.assertValueCount(1); |
| 81 |
|
| 82 |
tSched.advanceTimeBy(1, SECONDS); |
| 83 |
tSub.assertValueCount(2); |
| 84 |
|
| 85 |
tSched.advanceTimeBy(1, SECONDS); |
| 86 |
tSub.assertValueCount(2); |
| 87 |
|
| 88 |
tSched.advanceTimeBy(1, SECONDS); |
| 89 |
tSub.assertValueCount(3); |
| 90 |
|
| 91 |
tSched.advanceTimeBy(1, SECONDS); |
| 92 |
tSub.assertValueCount(3); |
| 93 |
|
| 94 |
tSched.advanceTimeBy(1, SECONDS); |
| 95 |
tSub.assertValueCount(4); |
| 96 |
|
| 97 |
tSched.advanceTimeBy(1, SECONDS); |
| 98 |
tSub.assertValueCount(4); |
| 99 |
|
| 100 |
tSched.advanceTimeBy(1, SECONDS); |
| 101 |
tSub.assertValueCount(5); |
| 102 |
tSub.assertComplete(); |
| 103 |
|
| 104 |
sched.dispose(); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void syncMaxConcurrent() { |
| 109 |
TestScheduler tSched = new TestScheduler(); |
| 110 |
SchedulerWhen sched = maxConcurrentScheduler(tSched); |
| 111 |
TestSubscriber<Long> tSub = TestSubscriber.create(); |
| 112 |
|
| 113 |
syncWork(sched).subscribe(tSub); |
| 114 |
|
| 115 |
tSub.assertValueCount(0); |
| 116 |
tSched.advanceTimeBy(0, SECONDS); |
| 117 |
|
| 118 |
// since all the work is synchronous nothing is blocked and its all done |
| 119 |
tSub.assertValueCount(5); |
| 120 |
tSub.assertComplete(); |
| 121 |
|
| 122 |
sched.dispose(); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void syncDelaySubscription() { |
| 127 |
final TestScheduler tSched = new TestScheduler(); |
| 128 |
SchedulerWhen sched = throttleScheduler(tSched); |
| 129 |
TestSubscriber<Long> tSub = TestSubscriber.create(); |
| 130 |
|
| 131 |
syncWork(sched).subscribe(tSub); |
| 132 |
|
| 133 |
tSub.assertValueCount(0); |
| 134 |
|
| 135 |
tSched.advanceTimeBy(0, SECONDS); |
| 136 |
tSub.assertValueCount(1); |
| 137 |
|
| 138 |
tSched.advanceTimeBy(1, SECONDS); |
| 139 |
tSub.assertValueCount(2); |
| 140 |
|
| 141 |
tSched.advanceTimeBy(1, SECONDS); |
| 142 |
tSub.assertValueCount(3); |
| 143 |
|
| 144 |
tSched.advanceTimeBy(1, SECONDS); |
| 145 |
tSub.assertValueCount(4); |
| 146 |
|
| 147 |
tSched.advanceTimeBy(1, SECONDS); |
| 148 |
tSub.assertValueCount(5); |
| 149 |
tSub.assertComplete(); |
| 150 |
|
| 151 |
sched.dispose(); |
| 152 |
} |
| 153 |
|
| 154 |
private Flowable<Long> asyncWork(final Scheduler sched) { |
| 155 |
return Flowable.range(1, 5).flatMap(new Function<Integer, Flowable<Long>>() { |
| 156 |
@Override |
| 157 |
public Flowable<Long> apply(Integer t) { |
| 158 |
return Flowable.timer(1, SECONDS, sched); |
| 159 |
} |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
private Flowable<Long> syncWork(final Scheduler sched) { |
| 164 |
return Flowable.range(1, 5).flatMap(new Function<Integer, Flowable<Long>>() { |
| 165 |
@Override |
| 166 |
public Flowable<Long> apply(Integer t) { |
| 167 |
return Flowable.defer(new Supplier<Flowable<Long>>() { |
| 168 |
@Override |
| 169 |
public Flowable<Long> get() { |
| 170 |
return Flowable.just(0l); |
| 171 |
} |
| 172 |
}).subscribeOn(sched); |
| 173 |
} |
| 174 |
}); |
| 175 |
} |
| 176 |
|
| 177 |
private SchedulerWhen maxConcurrentScheduler(TestScheduler tSched) { |
| 178 |
SchedulerWhen sched = new SchedulerWhen(new Function<Flowable<Flowable<Completable>>, Completable>() { |
| 179 |
@Override |
| 180 |
public Completable apply(Flowable<Flowable<Completable>> workerActions) { |
| 181 |
Flowable<Completable> workers = workerActions.map(new Function<Flowable<Completable>, Completable>() { |
| 182 |
@Override |
| 183 |
public Completable apply(Flowable<Completable> actions) { |
| 184 |
return Completable.concat(actions); |
| 185 |
} |
| 186 |
}); |
| 187 |
return Completable.merge(workers, 2); |
| 188 |
} |
| 189 |
}, tSched); |
| 190 |
return sched; |
| 191 |
} |
| 192 |
|
| 193 |
private SchedulerWhen throttleScheduler(final TestScheduler tSched) { |
| 194 |
SchedulerWhen sched = new SchedulerWhen(new Function<Flowable<Flowable<Completable>>, Completable>() { |
| 195 |
@Override |
| 196 |
public Completable apply(Flowable<Flowable<Completable>> workerActions) { |
| 197 |
Flowable<Completable> workers = workerActions.map(new Function<Flowable<Completable>, Completable>() { |
| 198 |
@Override |
| 199 |
public Completable apply(Flowable<Completable> actions) { |
| 200 |
return Completable.concat(actions); |
| 201 |
} |
| 202 |
}); |
| 203 |
return Completable.concat(workers.map(new Function<Completable, Completable>() { |
| 204 |
@Override |
| 205 |
public Completable apply(Completable worker) { |
| 206 |
return worker.delay(1, SECONDS, tSched); |
| 207 |
} |
| 208 |
})); |
| 209 |
} |
| 210 |
}, tSched); |
| 211 |
return sched; |
| 212 |
} |
| 213 |
|
| 214 |
@Test |
| 215 |
public void raceConditions() { |
| 216 |
Scheduler comp = Schedulers.computation(); |
| 217 |
Scheduler limited = comp.when(new Function<Flowable<Flowable<Completable>>, Completable>() { |
| 218 |
@Override |
| 219 |
public Completable apply(Flowable<Flowable<Completable>> t) { |
| 220 |
return Completable.merge(Flowable.merge(t, 10)); |
| 221 |
} |
| 222 |
}); |
| 223 |
|
| 224 |
merge(just(just(1).subscribeOn(limited).observeOn(comp)).repeat(1000)).blockingSubscribe(); |
| 225 |
} |
| 226 |
|
| 227 |
@Test |
| 228 |
public void subscribedDisposable() { |
| 229 |
SchedulerWhen.SUBSCRIBED.dispose(); |
| 230 |
assertFalse(SchedulerWhen.SUBSCRIBED.isDisposed()); |
| 231 |
} |
| 232 |
|
| 233 |
@Test(expected = TestException.class) |
| 234 |
public void combineCrashInConstructor() { |
| 235 |
new SchedulerWhen(new Function<Flowable<Flowable<Completable>>, Completable>() { |
| 236 |
@Override |
| 237 |
public Completable apply(Flowable<Flowable<Completable>> v) |
| 238 |
throws Exception { |
| 239 |
throw new TestException(); |
| 240 |
} |
| 241 |
}, Schedulers.single()); |
| 242 |
} |
| 243 |
|
| 244 |
@Test |
| 245 |
public void disposed() { |
| 246 |
SchedulerWhen sw = new SchedulerWhen(new Function<Flowable<Flowable<Completable>>, Completable>() { |
| 247 |
@Override |
| 248 |
public Completable apply(Flowable<Flowable<Completable>> v) |
| 249 |
throws Exception { |
| 250 |
return Completable.never(); |
| 251 |
} |
| 252 |
}, Schedulers.single()); |
| 253 |
|
| 254 |
assertFalse(sw.isDisposed()); |
| 255 |
|
| 256 |
sw.dispose(); |
| 257 |
|
| 258 |
assertTrue(sw.isDisposed()); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void scheduledActiondisposedSetRace() { |
| 263 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 264 |
final ScheduledAction sa = new ScheduledAction() { |
| 265 |
|
| 266 |
private static final long serialVersionUID = -672980251643733156L; |
| 267 |
|
| 268 |
@Override |
| 269 |
protected Disposable callActual(Worker actualWorker, |
| 270 |
CompletableObserver actionCompletable) { |
| 271 |
return Disposable.empty(); |
| 272 |
} |
| 273 |
|
| 274 |
}; |
| 275 |
|
| 276 |
assertFalse(sa.isDisposed()); |
| 277 |
|
| 278 |
Runnable r1 = new Runnable() { |
| 279 |
@Override |
| 280 |
public void run() { |
| 281 |
sa.dispose(); |
| 282 |
} |
| 283 |
}; |
| 284 |
|
| 285 |
TestHelper.race(r1, r1); |
| 286 |
|
| 287 |
assertTrue(sa.isDisposed()); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
@Test |
| 292 |
public void scheduledActionStates() { |
| 293 |
final AtomicInteger count = new AtomicInteger(); |
| 294 |
ScheduledAction sa = new ScheduledAction() { |
| 295 |
|
| 296 |
private static final long serialVersionUID = -672980251643733156L; |
| 297 |
|
| 298 |
@Override |
| 299 |
protected Disposable callActual(Worker actualWorker, |
| 300 |
CompletableObserver actionCompletable) { |
| 301 |
count.incrementAndGet(); |
| 302 |
return Disposable.empty(); |
| 303 |
} |
| 304 |
|
| 305 |
}; |
| 306 |
|
| 307 |
assertFalse(sa.isDisposed()); |
| 308 |
|
| 309 |
sa.dispose(); |
| 310 |
|
| 311 |
assertTrue(sa.isDisposed()); |
| 312 |
|
| 313 |
sa.dispose(); |
| 314 |
|
| 315 |
assertTrue(sa.isDisposed()); |
| 316 |
|
| 317 |
// should not run when disposed |
| 318 |
sa.call(Schedulers.single().createWorker(), null); |
| 319 |
|
| 320 |
assertEquals(0, count.get()); |
| 321 |
|
| 322 |
// should not run when already scheduled |
| 323 |
sa.set(Disposable.empty()); |
| 324 |
|
| 325 |
sa.call(Schedulers.single().createWorker(), null); |
| 326 |
|
| 327 |
assertEquals(0, count.get()); |
| 328 |
|
| 329 |
// disposed while in call |
| 330 |
sa = new ScheduledAction() { |
| 331 |
|
| 332 |
private static final long serialVersionUID = -672980251643733156L; |
| 333 |
|
| 334 |
@Override |
| 335 |
protected Disposable callActual(Worker actualWorker, |
| 336 |
CompletableObserver actionCompletable) { |
| 337 |
count.incrementAndGet(); |
| 338 |
dispose(); |
| 339 |
return Disposable.empty(); |
| 340 |
} |
| 341 |
|
| 342 |
}; |
| 343 |
|
| 344 |
sa.call(Schedulers.single().createWorker(), null); |
| 345 |
|
| 346 |
assertEquals(1, count.get()); |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void onCompleteActionRunCrash() { |
| 351 |
final AtomicInteger count = new AtomicInteger(); |
| 352 |
|
| 353 |
OnCompletedAction a = new OnCompletedAction(new Runnable() { |
| 354 |
@Override |
| 355 |
public void run() { |
| 356 |
throw new TestException(); |
| 357 |
} |
| 358 |
}, new DisposableCompletableObserver() { |
| 359 |
|
| 360 |
@Override |
| 361 |
public void onComplete() { |
| 362 |
count.incrementAndGet(); |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public void onError(Throwable e) { |
| 367 |
count.decrementAndGet(); |
| 368 |
e.printStackTrace(); |
| 369 |
} |
| 370 |
}); |
| 371 |
|
| 372 |
try { |
| 373 |
a.run(); |
| 374 |
fail("Should have thrown"); |
| 375 |
} catch (TestException expected) { |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
assertEquals(1, count.get()); |
| 380 |
} |
| 381 |
|
| 382 |
@Test |
| 383 |
public void queueWorkerDispose() { |
| 384 |
QueueWorker qw = new QueueWorker(PublishProcessor.<ScheduledAction>create(), Schedulers.single().createWorker()); |
| 385 |
|
| 386 |
assertFalse(qw.isDisposed()); |
| 387 |
|
| 388 |
qw.dispose(); |
| 389 |
|
| 390 |
assertTrue(qw.isDisposed()); |
| 391 |
|
| 392 |
qw.dispose(); |
| 393 |
|
| 394 |
assertTrue(qw.isDisposed()); |
| 395 |
} |
| 396 |
} |