| 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.*; |
| 17 |
|
| 18 |
import java.util.concurrent.TimeUnit; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 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.Action; |
| 26 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 27 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 28 |
import io.reactivex.rxjava3.subjects.CompletableSubject; |
| 29 |
|
| 30 |
public class CompletableDelaySubscriptionTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void normal() { |
| 34 |
final AtomicInteger counter = new AtomicInteger(); |
| 35 |
|
| 36 |
Completable.fromAction(new Action() { |
| 37 |
@Override |
| 38 |
public void run() throws Exception { |
| 39 |
counter.incrementAndGet(); |
| 40 |
} |
| 41 |
}) |
| 42 |
.delaySubscription(100, TimeUnit.MILLISECONDS) |
| 43 |
.test() |
| 44 |
.awaitDone(5, TimeUnit.SECONDS) |
| 45 |
.assertResult(); |
| 46 |
|
| 47 |
assertEquals(1, counter.get()); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void error() { |
| 52 |
final AtomicInteger counter = new AtomicInteger(); |
| 53 |
|
| 54 |
Completable.fromAction(new Action() { |
| 55 |
@Override |
| 56 |
public void run() throws Exception { |
| 57 |
counter.incrementAndGet(); |
| 58 |
|
| 59 |
throw new TestException(); |
| 60 |
} |
| 61 |
}) |
| 62 |
.delaySubscription(100, TimeUnit.MILLISECONDS) |
| 63 |
.test() |
| 64 |
.awaitDone(5, TimeUnit.SECONDS) |
| 65 |
.assertFailure(TestException.class); |
| 66 |
|
| 67 |
assertEquals(1, counter.get()); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void disposeBeforeTime() { |
| 72 |
TestScheduler scheduler = new TestScheduler(); |
| 73 |
|
| 74 |
final AtomicInteger counter = new AtomicInteger(); |
| 75 |
|
| 76 |
Completable result = Completable.fromAction(new Action() { |
| 77 |
@Override |
| 78 |
public void run() throws Exception { |
| 79 |
counter.incrementAndGet(); |
| 80 |
} |
| 81 |
}) |
| 82 |
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler); |
| 83 |
TestObserver<Void> to = result.test(); |
| 84 |
|
| 85 |
to.assertEmpty(); |
| 86 |
|
| 87 |
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS); |
| 88 |
|
| 89 |
to.dispose(); |
| 90 |
|
| 91 |
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS); |
| 92 |
|
| 93 |
to.assertEmpty(); |
| 94 |
|
| 95 |
assertEquals(0, counter.get()); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void timestep() { |
| 100 |
TestScheduler scheduler = new TestScheduler(); |
| 101 |
final AtomicInteger counter = new AtomicInteger(); |
| 102 |
|
| 103 |
Completable result = Completable.fromAction(new Action() { |
| 104 |
@Override |
| 105 |
public void run() throws Exception { |
| 106 |
counter.incrementAndGet(); |
| 107 |
} |
| 108 |
}) |
| 109 |
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler); |
| 110 |
|
| 111 |
TestObserver<Void> to = result.test(); |
| 112 |
|
| 113 |
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS); |
| 114 |
to.assertEmpty(); |
| 115 |
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS); |
| 116 |
to.assertResult(); |
| 117 |
|
| 118 |
assertEquals(1, counter.get()); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void timestepError() { |
| 123 |
TestScheduler scheduler = new TestScheduler(); |
| 124 |
final AtomicInteger counter = new AtomicInteger(); |
| 125 |
|
| 126 |
Completable result = Completable.fromAction(new Action() { |
| 127 |
@Override |
| 128 |
public void run() throws Exception { |
| 129 |
counter.incrementAndGet(); |
| 130 |
|
| 131 |
throw new TestException(); |
| 132 |
} |
| 133 |
}) |
| 134 |
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler); |
| 135 |
|
| 136 |
TestObserver<Void> to = result.test(); |
| 137 |
|
| 138 |
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS); |
| 139 |
|
| 140 |
to.assertEmpty(); |
| 141 |
|
| 142 |
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS); |
| 143 |
|
| 144 |
to.assertFailure(TestException.class); |
| 145 |
|
| 146 |
assertEquals(1, counter.get()); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void disposeMain() { |
| 151 |
CompletableSubject cs = CompletableSubject.create(); |
| 152 |
|
| 153 |
TestScheduler scheduler = new TestScheduler(); |
| 154 |
|
| 155 |
TestObserver<Void> to = cs |
| 156 |
.delaySubscription(1, TimeUnit.SECONDS, scheduler) |
| 157 |
.test(); |
| 158 |
|
| 159 |
assertFalse(cs.hasObservers()); |
| 160 |
|
| 161 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 162 |
|
| 163 |
assertTrue(cs.hasObservers()); |
| 164 |
|
| 165 |
to.dispose(); |
| 166 |
|
| 167 |
assertFalse(cs.hasObservers()); |
| 168 |
} |
| 169 |
} |