| 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.single; |
| 15 |
|
| 16 |
import static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage; |
| 17 |
import static org.junit.Assert.*; |
| 18 |
|
| 19 |
import java.util.List; |
| 20 |
import java.util.concurrent.*; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.Action; |
| 29 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 32 |
import io.reactivex.rxjava3.subjects.*; |
| 33 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 34 |
|
| 35 |
public class SingleTimeoutTest extends RxJavaTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() { |
| 39 |
final PublishSubject<String> subject = PublishSubject.create(); |
| 40 |
final TestScheduler scheduler = new TestScheduler(); |
| 41 |
|
| 42 |
final TestObserver<String> observer = subject.single("") |
| 43 |
.timeout(100, TimeUnit.MILLISECONDS, scheduler) |
| 44 |
.test(); |
| 45 |
|
| 46 |
assertTrue(subject.hasObservers()); |
| 47 |
|
| 48 |
observer.dispose(); |
| 49 |
|
| 50 |
assertFalse(subject.hasObservers()); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void otherErrors() { |
| 55 |
Single.never() |
| 56 |
.timeout(1, TimeUnit.MILLISECONDS, Single.error(new TestException())) |
| 57 |
.test() |
| 58 |
.awaitDone(5, TimeUnit.SECONDS) |
| 59 |
.assertFailure(TestException.class); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void mainSuccess() { |
| 64 |
Single.just(1) |
| 65 |
.timeout(1, TimeUnit.DAYS) |
| 66 |
.test() |
| 67 |
.awaitDone(5, TimeUnit.SECONDS) |
| 68 |
.assertResult(1); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void mainError() { |
| 73 |
Single.error(new TestException()) |
| 74 |
.timeout(1, TimeUnit.DAYS) |
| 75 |
.test() |
| 76 |
.awaitDone(5, TimeUnit.SECONDS) |
| 77 |
.assertFailure(TestException.class); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void disposeWhenFallback() { |
| 82 |
TestScheduler sch = new TestScheduler(); |
| 83 |
|
| 84 |
SingleSubject<Integer> subj = SingleSubject.create(); |
| 85 |
|
| 86 |
subj.timeout(1, TimeUnit.SECONDS, sch, Single.just(1)) |
| 87 |
.test(true) |
| 88 |
.assertEmpty(); |
| 89 |
|
| 90 |
assertFalse(subj.hasObservers()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void isDisposed() { |
| 95 |
TestHelper.checkDisposed(SingleSubject.create().timeout(1, TimeUnit.DAYS)); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void fallbackDispose() { |
| 100 |
TestScheduler sch = new TestScheduler(); |
| 101 |
|
| 102 |
SingleSubject<Integer> subj = SingleSubject.create(); |
| 103 |
|
| 104 |
SingleSubject<Integer> fallback = SingleSubject.create(); |
| 105 |
|
| 106 |
TestObserver<Integer> to = subj.timeout(1, TimeUnit.SECONDS, sch, fallback) |
| 107 |
.test(); |
| 108 |
|
| 109 |
assertFalse(fallback.hasObservers()); |
| 110 |
|
| 111 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 112 |
|
| 113 |
assertFalse(subj.hasObservers()); |
| 114 |
assertTrue(fallback.hasObservers()); |
| 115 |
|
| 116 |
to.dispose(); |
| 117 |
|
| 118 |
assertFalse(fallback.hasObservers()); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void normalSuccessDoesntDisposeMain() { |
| 123 |
final int[] calls = { 0 }; |
| 124 |
|
| 125 |
Single.just(1) |
| 126 |
.doOnDispose(new Action() { |
| 127 |
@Override |
| 128 |
public void run() throws Exception { |
| 129 |
calls[0]++; |
| 130 |
} |
| 131 |
}) |
| 132 |
.timeout(1, TimeUnit.DAYS) |
| 133 |
.test() |
| 134 |
.assertResult(1); |
| 135 |
|
| 136 |
assertEquals(0, calls[0]); |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void successTimeoutRace() { |
| 141 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 142 |
final SingleSubject<Integer> subj = SingleSubject.create(); |
| 143 |
SingleSubject<Integer> fallback = SingleSubject.create(); |
| 144 |
|
| 145 |
final TestScheduler sch = new TestScheduler(); |
| 146 |
|
| 147 |
TestObserver<Integer> to = subj.timeout(1, TimeUnit.MILLISECONDS, sch, fallback).test(); |
| 148 |
|
| 149 |
Runnable r1 = new Runnable() { |
| 150 |
@Override |
| 151 |
public void run() { |
| 152 |
subj.onSuccess(1); |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
Runnable r2 = new Runnable() { |
| 157 |
@Override |
| 158 |
public void run() { |
| 159 |
sch.advanceTimeBy(1, TimeUnit.MILLISECONDS); |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
TestHelper.race(r1, r2); |
| 164 |
|
| 165 |
if (!fallback.hasObservers()) { |
| 166 |
to.assertResult(1); |
| 167 |
} else { |
| 168 |
to.assertEmpty(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void errorTimeoutRace() { |
| 175 |
final TestException ex = new TestException(); |
| 176 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 177 |
try { |
| 178 |
|
| 179 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 180 |
final SingleSubject<Integer> subj = SingleSubject.create(); |
| 181 |
SingleSubject<Integer> fallback = SingleSubject.create(); |
| 182 |
|
| 183 |
final TestScheduler sch = new TestScheduler(); |
| 184 |
|
| 185 |
TestObserver<Integer> to = subj.timeout(1, TimeUnit.MILLISECONDS, sch, fallback).test(); |
| 186 |
|
| 187 |
Runnable r1 = new Runnable() { |
| 188 |
@Override |
| 189 |
public void run() { |
| 190 |
subj.onError(ex); |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
Runnable r2 = new Runnable() { |
| 195 |
@Override |
| 196 |
public void run() { |
| 197 |
sch.advanceTimeBy(1, TimeUnit.MILLISECONDS); |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
TestHelper.race(r1, r2); |
| 202 |
|
| 203 |
if (!fallback.hasObservers()) { |
| 204 |
to.assertFailure(TestException.class); |
| 205 |
} else { |
| 206 |
to.assertEmpty(); |
| 207 |
} |
| 208 |
if (!errors.isEmpty()) { |
| 209 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 210 |
} |
| 211 |
} |
| 212 |
} finally { |
| 213 |
RxJavaPlugins.reset(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void mainTimedOut() { |
| 219 |
Single |
| 220 |
.never() |
| 221 |
.timeout(1, TimeUnit.MILLISECONDS) |
| 222 |
.to(TestHelper.<Object>testConsumer()) |
| 223 |
.awaitDone(5, TimeUnit.SECONDS) |
| 224 |
.assertFailureAndMessage(TimeoutException.class, timeoutMessage(1, TimeUnit.MILLISECONDS)); |
| 225 |
} |
| 226 |
|
| 227 |
@Test |
| 228 |
public void mainTimeoutFallbackSuccess() { |
| 229 |
Single.never() |
| 230 |
.timeout(1, TimeUnit.MILLISECONDS, Single.just(1)) |
| 231 |
.test() |
| 232 |
.awaitDone(5, TimeUnit.SECONDS) |
| 233 |
.assertResult(1); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void timeoutBeforeOnSubscribeFromMain() { |
| 238 |
Disposable d = Disposable.empty(); |
| 239 |
|
| 240 |
new Single<Integer>() { |
| 241 |
@Override |
| 242 |
protected void subscribeActual(@NonNull SingleObserver<? super @NonNull Integer> observer) { |
| 243 |
try { |
| 244 |
Thread.sleep(100); |
| 245 |
} catch (InterruptedException e) { |
| 246 |
e.printStackTrace(); |
| 247 |
} |
| 248 |
observer.onSubscribe(d); |
| 249 |
} |
| 250 |
} |
| 251 |
.timeout(1, TimeUnit.MILLISECONDS, Single.just(1)) |
| 252 |
.test() |
| 253 |
.awaitDone(5, TimeUnit.SECONDS) |
| 254 |
.assertResult(1); |
| 255 |
|
| 256 |
assertTrue(d.isDisposed()); |
| 257 |
} |
| 258 |
} |