| 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.completable; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertNotEquals; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.AtomicReference; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 27 |
import io.reactivex.rxjava3.schedulers.*; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class CompletableDelayTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void delayCustomScheduler() { |
| 34 |
|
| 35 |
Completable.complete() |
| 36 |
.delay(100, TimeUnit.MILLISECONDS, Schedulers.trampoline()) |
| 37 |
.test() |
| 38 |
.assertResult(); |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
public void onErrorCalledOnScheduler() throws Exception { |
| 43 |
final CountDownLatch latch = new CountDownLatch(1); |
| 44 |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
| 45 |
|
| 46 |
Completable.error(new Exception()) |
| 47 |
.delay(0, TimeUnit.MILLISECONDS, Schedulers.newThread()) |
| 48 |
.doOnError(new Consumer<Throwable>() { |
| 49 |
@Override |
| 50 |
public void accept(Throwable throwable) throws Exception { |
| 51 |
thread.set(Thread.currentThread()); |
| 52 |
latch.countDown(); |
| 53 |
} |
| 54 |
}) |
| 55 |
.onErrorComplete() |
| 56 |
.subscribe(); |
| 57 |
|
| 58 |
latch.await(); |
| 59 |
|
| 60 |
assertNotEquals(Thread.currentThread(), thread.get()); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void disposed() { |
| 65 |
TestHelper.checkDisposed(Completable.never().delay(1, TimeUnit.MINUTES)); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void doubleOnSubscribe() { |
| 70 |
TestHelper.checkDoubleOnSubscribeCompletable(new Function<Completable, CompletableSource>() { |
| 71 |
@Override |
| 72 |
public CompletableSource apply(Completable c) throws Exception { |
| 73 |
return c.delay(1, TimeUnit.MINUTES); |
| 74 |
} |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void normal() { |
| 80 |
Completable.complete() |
| 81 |
.delay(1, TimeUnit.MILLISECONDS) |
| 82 |
.test() |
| 83 |
.awaitDone(5, TimeUnit.SECONDS) |
| 84 |
.assertResult(); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void errorNotDelayed() { |
| 89 |
TestScheduler scheduler = new TestScheduler(); |
| 90 |
|
| 91 |
TestObserver<Void> to = Completable.error(new TestException()) |
| 92 |
.delay(100, TimeUnit.MILLISECONDS, scheduler, false) |
| 93 |
.test(); |
| 94 |
|
| 95 |
to.assertEmpty(); |
| 96 |
|
| 97 |
scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS); |
| 98 |
|
| 99 |
to.assertFailure(TestException.class); |
| 100 |
|
| 101 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 102 |
|
| 103 |
to.assertFailure(TestException.class); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void errorDelayed() { |
| 108 |
TestScheduler scheduler = new TestScheduler(); |
| 109 |
|
| 110 |
TestObserver<Void> to = Completable.error(new TestException()) |
| 111 |
.delay(100, TimeUnit.MILLISECONDS, scheduler, true) |
| 112 |
.test(); |
| 113 |
|
| 114 |
to.assertEmpty(); |
| 115 |
|
| 116 |
scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS); |
| 117 |
|
| 118 |
to.assertEmpty(); |
| 119 |
|
| 120 |
scheduler.advanceTimeBy(99, TimeUnit.MILLISECONDS); |
| 121 |
|
| 122 |
to.assertFailure(TestException.class); |
| 123 |
} |
| 124 |
} |