| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.List; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 25 |
import org.junit.*; |
| 26 |
import org.mockito.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.core.*; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.functions.Function; |
| 31 |
import io.reactivex.rxjava3.internal.operators.observable.ObservableTimer.TimerObserver; |
| 32 |
import io.reactivex.rxjava3.observables.ConnectableObservable; |
| 33 |
import io.reactivex.rxjava3.observers.*; |
| 34 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 35 |
import io.reactivex.rxjava3.schedulers.*; |
| 36 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 37 |
|
| 38 |
public class ObservableTimerTest extends RxJavaTest { |
| 39 |
@Mock |
| 40 |
Observer<Object> observer; |
| 41 |
@Mock |
| 42 |
Observer<Long> observer2; |
| 43 |
|
| 44 |
TestScheduler scheduler; |
| 45 |
|
| 46 |
@Before |
| 47 |
public void before() { |
| 48 |
observer = TestHelper.mockObserver(); |
| 49 |
|
| 50 |
observer2 = TestHelper.mockObserver(); |
| 51 |
|
| 52 |
scheduler = new TestScheduler(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void timerOnce() { |
| 57 |
Observable.timer(100, TimeUnit.MILLISECONDS, scheduler).subscribe(observer); |
| 58 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 59 |
|
| 60 |
verify(observer, times(1)).onNext(0L); |
| 61 |
verify(observer, times(1)).onComplete(); |
| 62 |
verify(observer, never()).onError(any(Throwable.class)); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void timerPeriodically() { |
| 67 |
TestObserver<Long> to = new TestObserver<>(); |
| 68 |
|
| 69 |
Observable.interval(100, 100, TimeUnit.MILLISECONDS, scheduler).subscribe(to); |
| 70 |
|
| 71 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 72 |
|
| 73 |
to.assertValue(0L); |
| 74 |
|
| 75 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 76 |
to.assertValues(0L, 1L); |
| 77 |
|
| 78 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 79 |
to.assertValues(0L, 1L, 2L); |
| 80 |
|
| 81 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 82 |
to.assertValues(0L, 1L, 2L, 3L); |
| 83 |
|
| 84 |
to.dispose(); |
| 85 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 86 |
to.assertValues(0L, 1L, 2L, 3L); |
| 87 |
|
| 88 |
to.assertNotComplete(); |
| 89 |
to.assertNoErrors(); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void interval() { |
| 94 |
Observable<Long> w = Observable.interval(1, TimeUnit.SECONDS, scheduler); |
| 95 |
TestObserver<Long> to = new TestObserver<>(); |
| 96 |
w.subscribe(to); |
| 97 |
|
| 98 |
to.assertNoValues(); |
| 99 |
to.assertNoErrors(); |
| 100 |
to.assertNotComplete(); |
| 101 |
|
| 102 |
scheduler.advanceTimeTo(2, TimeUnit.SECONDS); |
| 103 |
|
| 104 |
to.assertValues(0L, 1L); |
| 105 |
to.assertNoErrors(); |
| 106 |
to.assertNotComplete(); |
| 107 |
|
| 108 |
to.dispose(); |
| 109 |
|
| 110 |
scheduler.advanceTimeTo(4, TimeUnit.SECONDS); |
| 111 |
to.assertValues(0L, 1L); |
| 112 |
to.assertNoErrors(); |
| 113 |
to.assertNotComplete(); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void withMultipleSubscribersStartingAtSameTime() { |
| 118 |
Observable<Long> w = Observable.interval(1, TimeUnit.SECONDS, scheduler); |
| 119 |
|
| 120 |
TestObserver<Long> to1 = new TestObserver<>(); |
| 121 |
TestObserver<Long> to2 = new TestObserver<>(); |
| 122 |
|
| 123 |
w.subscribe(to1); |
| 124 |
w.subscribe(to2); |
| 125 |
|
| 126 |
to1.assertNoValues(); |
| 127 |
to2.assertNoValues(); |
| 128 |
|
| 129 |
scheduler.advanceTimeTo(2, TimeUnit.SECONDS); |
| 130 |
|
| 131 |
to1.assertValues(0L, 1L); |
| 132 |
to1.assertNoErrors(); |
| 133 |
to1.assertNotComplete(); |
| 134 |
|
| 135 |
to2.assertValues(0L, 1L); |
| 136 |
to2.assertNoErrors(); |
| 137 |
to2.assertNotComplete(); |
| 138 |
|
| 139 |
to1.dispose(); |
| 140 |
to2.dispose(); |
| 141 |
|
| 142 |
scheduler.advanceTimeTo(4, TimeUnit.SECONDS); |
| 143 |
|
| 144 |
to1.assertValues(0L, 1L); |
| 145 |
to1.assertNoErrors(); |
| 146 |
to1.assertNotComplete(); |
| 147 |
|
| 148 |
to2.assertValues(0L, 1L); |
| 149 |
to2.assertNoErrors(); |
| 150 |
to2.assertNotComplete(); |
| 151 |
} |
| 152 |
|
| 153 |
@Test |
| 154 |
public void withMultipleStaggeredSubscribers() { |
| 155 |
Observable<Long> w = Observable.interval(1, TimeUnit.SECONDS, scheduler); |
| 156 |
|
| 157 |
TestObserver<Long> to1 = new TestObserver<>(); |
| 158 |
|
| 159 |
w.subscribe(to1); |
| 160 |
|
| 161 |
to1.assertNoErrors(); |
| 162 |
|
| 163 |
scheduler.advanceTimeTo(2, TimeUnit.SECONDS); |
| 164 |
|
| 165 |
TestObserver<Long> to2 = new TestObserver<>(); |
| 166 |
|
| 167 |
w.subscribe(to2); |
| 168 |
|
| 169 |
to1.assertValues(0L, 1L); |
| 170 |
to1.assertNoErrors(); |
| 171 |
to1.assertNotComplete(); |
| 172 |
|
| 173 |
to2.assertNoValues(); |
| 174 |
|
| 175 |
scheduler.advanceTimeTo(4, TimeUnit.SECONDS); |
| 176 |
|
| 177 |
to1.assertValues(0L, 1L, 2L, 3L); |
| 178 |
|
| 179 |
to2.assertValues(0L, 1L); |
| 180 |
|
| 181 |
to1.dispose(); |
| 182 |
to2.dispose(); |
| 183 |
|
| 184 |
to1.assertValues(0L, 1L, 2L, 3L); |
| 185 |
to1.assertNoErrors(); |
| 186 |
to1.assertNotComplete(); |
| 187 |
|
| 188 |
to2.assertValues(0L, 1L); |
| 189 |
to2.assertNoErrors(); |
| 190 |
to2.assertNotComplete(); |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void withMultipleStaggeredSubscribersAndPublish() { |
| 195 |
ConnectableObservable<Long> w = Observable.interval(1, TimeUnit.SECONDS, scheduler).publish(); |
| 196 |
|
| 197 |
TestObserver<Long> to1 = new TestObserver<>(); |
| 198 |
|
| 199 |
w.subscribe(to1); |
| 200 |
w.connect(); |
| 201 |
|
| 202 |
to1.assertNoValues(); |
| 203 |
|
| 204 |
scheduler.advanceTimeTo(2, TimeUnit.SECONDS); |
| 205 |
|
| 206 |
TestObserver<Long> to2 = new TestObserver<>(); |
| 207 |
w.subscribe(to2); |
| 208 |
|
| 209 |
to1.assertValues(0L, 1L); |
| 210 |
to1.assertNoErrors(); |
| 211 |
to1.assertNotComplete(); |
| 212 |
|
| 213 |
to2.assertNoValues(); |
| 214 |
|
| 215 |
scheduler.advanceTimeTo(4, TimeUnit.SECONDS); |
| 216 |
|
| 217 |
to1.assertValues(0L, 1L, 2L, 3L); |
| 218 |
|
| 219 |
to2.assertValues(2L, 3L); |
| 220 |
|
| 221 |
to1.dispose(); |
| 222 |
to2.dispose(); |
| 223 |
|
| 224 |
to1.assertValues(0L, 1L, 2L, 3L); |
| 225 |
to1.assertNoErrors(); |
| 226 |
to1.assertNotComplete(); |
| 227 |
|
| 228 |
to2.assertValues(2L, 3L); |
| 229 |
to2.assertNoErrors(); |
| 230 |
to2.assertNotComplete(); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void onceObserverThrows() { |
| 235 |
Observable<Long> source = Observable.timer(100, TimeUnit.MILLISECONDS, scheduler); |
| 236 |
|
| 237 |
source.safeSubscribe(new DefaultObserver<Long>() { |
| 238 |
|
| 239 |
@Override |
| 240 |
public void onNext(Long t) { |
| 241 |
throw new TestException(); |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public void onError(Throwable e) { |
| 246 |
observer.onError(e); |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public void onComplete() { |
| 251 |
observer.onComplete(); |
| 252 |
} |
| 253 |
}); |
| 254 |
|
| 255 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 256 |
|
| 257 |
verify(observer).onError(any(TestException.class)); |
| 258 |
verify(observer, never()).onNext(anyLong()); |
| 259 |
verify(observer, never()).onComplete(); |
| 260 |
} |
| 261 |
|
| 262 |
@Test |
| 263 |
public void periodicObserverThrows() { |
| 264 |
Observable<Long> source = Observable.interval(100, 100, TimeUnit.MILLISECONDS, scheduler); |
| 265 |
|
| 266 |
InOrder inOrder = inOrder(observer); |
| 267 |
|
| 268 |
source.safeSubscribe(new DefaultObserver<Long>() { |
| 269 |
|
| 270 |
@Override |
| 271 |
public void onNext(Long t) { |
| 272 |
if (t > 0) { |
| 273 |
throw new TestException(); |
| 274 |
} |
| 275 |
observer.onNext(t); |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public void onError(Throwable e) { |
| 280 |
observer.onError(e); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public void onComplete() { |
| 285 |
observer.onComplete(); |
| 286 |
} |
| 287 |
}); |
| 288 |
|
| 289 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 290 |
|
| 291 |
inOrder.verify(observer).onNext(0L); |
| 292 |
inOrder.verify(observer).onError(any(TestException.class)); |
| 293 |
inOrder.verifyNoMoreInteractions(); |
| 294 |
verify(observer, never()).onComplete(); |
| 295 |
} |
| 296 |
|
| 297 |
@Test |
| 298 |
public void disposed() { |
| 299 |
TestHelper.checkDisposed(Observable.timer(1, TimeUnit.DAYS)); |
| 300 |
} |
| 301 |
|
| 302 |
@Test |
| 303 |
public void timerDelayZero() { |
| 304 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 305 |
try { |
| 306 |
for (int i = 0; i < 1000; i++) { |
| 307 |
Observable.timer(0, TimeUnit.MILLISECONDS).blockingFirst(); |
| 308 |
} |
| 309 |
|
| 310 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 311 |
} finally { |
| 312 |
RxJavaPlugins.reset(); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
@Test |
| 317 |
public void timerInterruptible() throws Exception { |
| 318 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 319 |
try { |
| 320 |
for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) { |
| 321 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 322 |
TestObserver<Long> to = Observable.timer(1, TimeUnit.MILLISECONDS, s) |
| 323 |
.map(new Function<Long, Long>() { |
| 324 |
@Override |
| 325 |
public Long apply(Long v) throws Exception { |
| 326 |
try { |
| 327 |
Thread.sleep(3000); |
| 328 |
} catch (InterruptedException ex) { |
| 329 |
interrupted.set(true); |
| 330 |
} |
| 331 |
return v; |
| 332 |
} |
| 333 |
}) |
| 334 |
.test(); |
| 335 |
|
| 336 |
Thread.sleep(500); |
| 337 |
|
| 338 |
to.dispose(); |
| 339 |
|
| 340 |
Thread.sleep(500); |
| 341 |
|
| 342 |
assertTrue(s.getClass().getSimpleName(), interrupted.get()); |
| 343 |
} |
| 344 |
} finally { |
| 345 |
exec.shutdown(); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void cancelledAndRun() { |
| 351 |
TestObserver<Long> to = new TestObserver<>(); |
| 352 |
to.onSubscribe(Disposable.empty()); |
| 353 |
TimerObserver tm = new TimerObserver(to); |
| 354 |
|
| 355 |
tm.dispose(); |
| 356 |
|
| 357 |
tm.run(); |
| 358 |
|
| 359 |
to.assertEmpty(); |
| 360 |
} |
| 361 |
} |