| 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.assertTrue; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.functions.Action; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.schedulers.*; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class CompletableTimerTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void dispose() { |
| 33 |
TestHelper.checkDisposed(Completable.timer(1, TimeUnit.SECONDS, new TestScheduler())); |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
public void timerInterruptible() throws Exception { |
| 38 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 39 |
try { |
| 40 |
for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) { |
| 41 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 42 |
TestObserver<Void> to = Completable.timer(1, TimeUnit.MILLISECONDS, s) |
| 43 |
.doOnComplete(new Action() { |
| 44 |
@Override |
| 45 |
public void run() throws Exception { |
| 46 |
try { |
| 47 |
Thread.sleep(3000); |
| 48 |
} catch (InterruptedException ex) { |
| 49 |
interrupted.set(true); |
| 50 |
} |
| 51 |
} |
| 52 |
}) |
| 53 |
.test(); |
| 54 |
|
| 55 |
Thread.sleep(500); |
| 56 |
|
| 57 |
to.dispose(); |
| 58 |
|
| 59 |
Thread.sleep(500); |
| 60 |
|
| 61 |
assertTrue(s.getClass().getSimpleName(), interrupted.get()); |
| 62 |
} |
| 63 |
} finally { |
| 64 |
exec.shutdown(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
} |