| 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.schedulers; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.concurrent.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.Scheduler; |
| 24 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 25 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 26 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 27 |
import io.reactivex.rxjava3.internal.schedulers.SingleScheduler.ScheduledWorker; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.schedulers.*; |
| 30 |
import io.reactivex.rxjava3.testsupport.*; |
| 31 |
|
| 32 |
public class SingleSchedulerTest extends AbstractSchedulerTests { |
| 33 |
|
| 34 |
@Test |
| 35 |
@SuppressUndeliverable |
| 36 |
public void shutdownRejects() { |
| 37 |
final int[] calls = { 0 }; |
| 38 |
|
| 39 |
Runnable r = new Runnable() { |
| 40 |
@Override |
| 41 |
public void run() { |
| 42 |
calls[0]++; |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
Scheduler s = new SingleScheduler(); |
| 47 |
s.shutdown(); |
| 48 |
|
| 49 |
assertEquals(Disposable.disposed(), s.scheduleDirect(r)); |
| 50 |
|
| 51 |
assertEquals(Disposable.disposed(), s.scheduleDirect(r, 1, TimeUnit.SECONDS)); |
| 52 |
|
| 53 |
assertEquals(Disposable.disposed(), s.schedulePeriodicallyDirect(r, 1, 1, TimeUnit.SECONDS)); |
| 54 |
|
| 55 |
Worker w = s.createWorker(); |
| 56 |
((ScheduledWorker)w).executor.shutdownNow(); |
| 57 |
|
| 58 |
assertEquals(Disposable.disposed(), w.schedule(r)); |
| 59 |
|
| 60 |
assertEquals(Disposable.disposed(), w.schedule(r, 1, TimeUnit.SECONDS)); |
| 61 |
|
| 62 |
assertEquals(Disposable.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS)); |
| 63 |
|
| 64 |
assertEquals(0, calls[0]); |
| 65 |
|
| 66 |
w.dispose(); |
| 67 |
|
| 68 |
assertTrue(w.isDisposed()); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void startRace() { |
| 73 |
final Scheduler s = new SingleScheduler(); |
| 74 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 75 |
s.shutdown(); |
| 76 |
|
| 77 |
Runnable r1 = new Runnable() { |
| 78 |
@Override |
| 79 |
public void run() { |
| 80 |
s.start(); |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
TestHelper.race(r1, r1); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void runnableDisposedAsync() throws Exception { |
| 90 |
final Scheduler s = Schedulers.single(); |
| 91 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE); |
| 92 |
|
| 93 |
while (!d.isDisposed()) { |
| 94 |
Thread.sleep(1); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void runnableDisposedAsyncCrash() throws Exception { |
| 100 |
final Scheduler s = Schedulers.single(); |
| 101 |
|
| 102 |
Disposable d = s.scheduleDirect(new Runnable() { |
| 103 |
@Override |
| 104 |
public void run() { |
| 105 |
throw new IllegalStateException(); |
| 106 |
} |
| 107 |
}); |
| 108 |
|
| 109 |
while (!d.isDisposed()) { |
| 110 |
Thread.sleep(1); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void runnableDisposedAsyncTimed() throws Exception { |
| 116 |
final Scheduler s = Schedulers.single(); |
| 117 |
|
| 118 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE, 1, TimeUnit.MILLISECONDS); |
| 119 |
|
| 120 |
while (!d.isDisposed()) { |
| 121 |
Thread.sleep(1); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Override protected Scheduler getScheduler() { |
| 126 |
return Schedulers.single(); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void zeroPeriodRejectedExecution() throws Throwable { |
| 131 |
TestHelper.withErrorTracking(errors -> { |
| 132 |
Scheduler s = RxJavaPlugins.createSingleScheduler(new RxThreadFactory("Test")); |
| 133 |
s.shutdown(); |
| 134 |
Runnable run = mock(Runnable.class); |
| 135 |
|
| 136 |
s.schedulePeriodicallyDirect(run, 1, 0, TimeUnit.MILLISECONDS); |
| 137 |
|
| 138 |
Thread.sleep(100); |
| 139 |
|
| 140 |
verify(run, never()).run(); |
| 141 |
|
| 142 |
TestHelper.assertUndeliverable(errors, 0, RejectedExecutionException.class); |
| 143 |
}); |
| 144 |
} |
| 145 |
} |