| 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 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 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.disposables.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.Action; |
| 29 |
import io.reactivex.rxjava3.internal.operators.completable.CompletableTimeout.TimeOutObserver; |
| 30 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.schedulers.*; |
| 33 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class CompletableTimeoutTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void timeoutException() throws Exception { |
| 40 |
|
| 41 |
Completable.never() |
| 42 |
.timeout(100, TimeUnit.MILLISECONDS, Schedulers.io()) |
| 43 |
.to(TestHelper.<Void>testConsumer()) |
| 44 |
.awaitDone(5, TimeUnit.SECONDS) |
| 45 |
.assertFailureAndMessage(TimeoutException.class, timeoutMessage(100, TimeUnit.MILLISECONDS)); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void timeoutContinueOther() throws Exception { |
| 50 |
|
| 51 |
final int[] call = { 0 }; |
| 52 |
|
| 53 |
Completable other = Completable.fromAction(new Action() { |
| 54 |
@Override |
| 55 |
public void run() throws Exception { |
| 56 |
call[0]++; |
| 57 |
} |
| 58 |
}); |
| 59 |
|
| 60 |
Completable.never() |
| 61 |
.timeout(100, TimeUnit.MILLISECONDS, Schedulers.io(), other) |
| 62 |
.test() |
| 63 |
.awaitDone(5, TimeUnit.SECONDS) |
| 64 |
.assertResult(); |
| 65 |
|
| 66 |
assertEquals(1, call[0]); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() { |
| 71 |
final PublishSubject<String> subject = PublishSubject.create(); |
| 72 |
final TestScheduler scheduler = new TestScheduler(); |
| 73 |
|
| 74 |
final TestObserver<Void> observer = subject.ignoreElements() |
| 75 |
.timeout(100, TimeUnit.MILLISECONDS, scheduler) |
| 76 |
.test(); |
| 77 |
|
| 78 |
assertTrue(subject.hasObservers()); |
| 79 |
|
| 80 |
observer.dispose(); |
| 81 |
|
| 82 |
assertFalse(subject.hasObservers()); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void otherErrors() { |
| 87 |
Completable.never() |
| 88 |
.timeout(1, TimeUnit.MILLISECONDS, Completable.error(new TestException())) |
| 89 |
.test() |
| 90 |
.awaitDone(5, TimeUnit.SECONDS) |
| 91 |
.assertFailure(TestException.class); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void mainSuccess() { |
| 96 |
Completable.complete() |
| 97 |
.timeout(1, TimeUnit.DAYS) |
| 98 |
.test() |
| 99 |
.awaitDone(5, TimeUnit.SECONDS) |
| 100 |
.assertResult(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void mainError() { |
| 105 |
Completable.error(new TestException()) |
| 106 |
.timeout(1, TimeUnit.DAYS) |
| 107 |
.test() |
| 108 |
.awaitDone(5, TimeUnit.SECONDS) |
| 109 |
.assertFailure(TestException.class); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void errorTimeoutRace() { |
| 114 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 115 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 116 |
|
| 117 |
try { |
| 118 |
final TestScheduler scheduler = new TestScheduler(); |
| 119 |
|
| 120 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 121 |
|
| 122 |
TestObserverEx<Void> to = ps.ignoreElements() |
| 123 |
.timeout(1, TimeUnit.MILLISECONDS, scheduler, Completable.complete()) |
| 124 |
.to(TestHelper.<Void>testConsumer()); |
| 125 |
|
| 126 |
final TestException ex = new TestException(); |
| 127 |
|
| 128 |
Runnable r1 = new Runnable() { |
| 129 |
@Override |
| 130 |
public void run() { |
| 131 |
ps.onError(ex); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
Runnable r2 = new Runnable() { |
| 136 |
@Override |
| 137 |
public void run() { |
| 138 |
scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS); |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
TestHelper.race(r1, r2); |
| 143 |
|
| 144 |
to.assertTerminated(); |
| 145 |
|
| 146 |
if (!errors.isEmpty()) { |
| 147 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 148 |
} |
| 149 |
|
| 150 |
} finally { |
| 151 |
RxJavaPlugins.reset(); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void ambRace() { |
| 158 |
TestObserver<Void> to = new TestObserver<>(); |
| 159 |
to.onSubscribe(Disposable.empty()); |
| 160 |
|
| 161 |
CompositeDisposable cd = new CompositeDisposable(); |
| 162 |
AtomicBoolean once = new AtomicBoolean(); |
| 163 |
TimeOutObserver a = new TimeOutObserver(cd, once, to); |
| 164 |
|
| 165 |
a.onComplete(); |
| 166 |
a.onComplete(); |
| 167 |
|
| 168 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 169 |
try { |
| 170 |
a.onError(new TestException()); |
| 171 |
|
| 172 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 173 |
} finally { |
| 174 |
RxJavaPlugins.reset(); |
| 175 |
} |
| 176 |
} |
| 177 |
} |