| 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.TimeUnit; |
| 20 |
import java.util.concurrent.atomic.AtomicInteger; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 26 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 27 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler.*; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.testsupport.*; |
| 32 |
|
| 33 |
public class TrampolineSchedulerInternalTest extends RxJavaTest { |
| 34 |
|
| 35 |
@Test |
| 36 |
@SuppressUndeliverable |
| 37 |
public void scheduleDirectInterrupt() { |
| 38 |
Thread.currentThread().interrupt(); |
| 39 |
|
| 40 |
final int[] calls = { 0 }; |
| 41 |
|
| 42 |
assertSame(EmptyDisposable.INSTANCE, Schedulers.trampoline().scheduleDirect(new Runnable() { |
| 43 |
@Override |
| 44 |
public void run() { |
| 45 |
calls[0]++; |
| 46 |
} |
| 47 |
}, 1, TimeUnit.SECONDS)); |
| 48 |
|
| 49 |
assertTrue(Thread.interrupted()); |
| 50 |
|
| 51 |
assertEquals(0, calls[0]); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void dispose() { |
| 56 |
Worker w = Schedulers.trampoline().createWorker(); |
| 57 |
|
| 58 |
assertFalse(w.isDisposed()); |
| 59 |
|
| 60 |
w.dispose(); |
| 61 |
|
| 62 |
assertTrue(w.isDisposed()); |
| 63 |
|
| 64 |
assertEquals(EmptyDisposable.INSTANCE, w.schedule(Functions.EMPTY_RUNNABLE)); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void reentrantScheduleDispose() { |
| 69 |
final Worker w = Schedulers.trampoline().createWorker(); |
| 70 |
try { |
| 71 |
final int[] calls = { 0, 0 }; |
| 72 |
w.schedule(new Runnable() { |
| 73 |
@Override |
| 74 |
public void run() { |
| 75 |
calls[0]++; |
| 76 |
w.schedule(new Runnable() { |
| 77 |
@Override |
| 78 |
public void run() { |
| 79 |
calls[1]++; |
| 80 |
} |
| 81 |
}) |
| 82 |
.dispose(); |
| 83 |
} |
| 84 |
}); |
| 85 |
|
| 86 |
assertEquals(1, calls[0]); |
| 87 |
assertEquals(0, calls[1]); |
| 88 |
} finally { |
| 89 |
w.dispose(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void reentrantScheduleShutdown() { |
| 95 |
final Worker w = Schedulers.trampoline().createWorker(); |
| 96 |
try { |
| 97 |
final int[] calls = { 0, 0 }; |
| 98 |
w.schedule(new Runnable() { |
| 99 |
@Override |
| 100 |
public void run() { |
| 101 |
calls[0]++; |
| 102 |
w.schedule(new Runnable() { |
| 103 |
@Override |
| 104 |
public void run() { |
| 105 |
calls[1]++; |
| 106 |
} |
| 107 |
}, 1, TimeUnit.MILLISECONDS); |
| 108 |
|
| 109 |
w.dispose(); |
| 110 |
} |
| 111 |
}); |
| 112 |
|
| 113 |
assertEquals(1, calls[0]); |
| 114 |
assertEquals(0, calls[1]); |
| 115 |
} finally { |
| 116 |
w.dispose(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void reentrantScheduleShutdown2() { |
| 122 |
final Worker w = Schedulers.trampoline().createWorker(); |
| 123 |
try { |
| 124 |
final int[] calls = { 0, 0 }; |
| 125 |
w.schedule(new Runnable() { |
| 126 |
@Override |
| 127 |
public void run() { |
| 128 |
calls[0]++; |
| 129 |
w.dispose(); |
| 130 |
|
| 131 |
assertSame(EmptyDisposable.INSTANCE, w.schedule(new Runnable() { |
| 132 |
@Override |
| 133 |
public void run() { |
| 134 |
calls[1]++; |
| 135 |
} |
| 136 |
}, 1, TimeUnit.MILLISECONDS)); |
| 137 |
} |
| 138 |
}); |
| 139 |
|
| 140 |
assertEquals(1, calls[0]); |
| 141 |
assertEquals(0, calls[1]); |
| 142 |
} finally { |
| 143 |
w.dispose(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
@Test |
| 148 |
@SuppressUndeliverable |
| 149 |
public void reentrantScheduleInterrupt() { |
| 150 |
final Worker w = Schedulers.trampoline().createWorker(); |
| 151 |
try { |
| 152 |
final int[] calls = { 0 }; |
| 153 |
Thread.currentThread().interrupt(); |
| 154 |
w.schedule(new Runnable() { |
| 155 |
@Override |
| 156 |
public void run() { |
| 157 |
calls[0]++; |
| 158 |
} |
| 159 |
}, 1, TimeUnit.DAYS); |
| 160 |
|
| 161 |
assertTrue(Thread.interrupted()); |
| 162 |
|
| 163 |
assertEquals(0, calls[0]); |
| 164 |
} finally { |
| 165 |
w.dispose(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Test |
| 170 |
public void sleepingRunnableDisposedOnRun() { |
| 171 |
TrampolineWorker w = new TrampolineWorker(); |
| 172 |
|
| 173 |
Runnable r = mock(Runnable.class); |
| 174 |
|
| 175 |
SleepingRunnable run = new SleepingRunnable(r, w, 0); |
| 176 |
w.dispose(); |
| 177 |
run.run(); |
| 178 |
|
| 179 |
verify(r, never()).run(); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void sleepingRunnableNoDelayRun() { |
| 184 |
TrampolineWorker w = new TrampolineWorker(); |
| 185 |
|
| 186 |
Runnable r = mock(Runnable.class); |
| 187 |
|
| 188 |
SleepingRunnable run = new SleepingRunnable(r, w, 0); |
| 189 |
|
| 190 |
run.run(); |
| 191 |
|
| 192 |
verify(r).run(); |
| 193 |
} |
| 194 |
|
| 195 |
@Test |
| 196 |
public void sleepingRunnableDisposedOnDelayedRun() { |
| 197 |
final TrampolineWorker w = new TrampolineWorker(); |
| 198 |
|
| 199 |
Runnable r = mock(Runnable.class); |
| 200 |
|
| 201 |
SleepingRunnable run = new SleepingRunnable(r, w, System.currentTimeMillis() + 200); |
| 202 |
|
| 203 |
Schedulers.single().scheduleDirect(new Runnable() { |
| 204 |
@Override |
| 205 |
public void run() { |
| 206 |
w.dispose(); |
| 207 |
} |
| 208 |
}, 100, TimeUnit.MILLISECONDS); |
| 209 |
|
| 210 |
run.run(); |
| 211 |
|
| 212 |
verify(r, never()).run(); |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void submitAndDisposeNextTask() { |
| 217 |
Scheduler.Worker w = Schedulers.trampoline().createWorker(); |
| 218 |
|
| 219 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 220 |
Runnable run = mock(Runnable.class); |
| 221 |
|
| 222 |
AtomicInteger sync = new AtomicInteger(2); |
| 223 |
|
| 224 |
w.schedule(() -> { |
| 225 |
Disposable d = w.schedule(run); |
| 226 |
|
| 227 |
Schedulers.single().scheduleDirect(() -> { |
| 228 |
if (sync.decrementAndGet() != 0) { |
| 229 |
while (sync.get() != 0) { } |
| 230 |
} |
| 231 |
d.dispose(); |
| 232 |
}); |
| 233 |
if (sync.decrementAndGet() != 0) { |
| 234 |
while (sync.get() != 0) { } |
| 235 |
} |
| 236 |
}); |
| 237 |
} |
| 238 |
} |
| 239 |
} |