| 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.schedulers; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.concurrent.*; |
| 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.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.schedulers.ComputationScheduler; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.testsupport.SuppressUndeliverable; |
| 31 |
|
| 32 |
public class ComputationSchedulerTests extends AbstractSchedulerConcurrencyTests { |
| 33 |
|
| 34 |
@Override |
| 35 |
protected Scheduler getScheduler() { |
| 36 |
// this is an implementation of ExecutorScheduler |
| 37 |
return Schedulers.computation(); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void threadSafetyWhenSchedulerIsHoppingBetweenThreads() { |
| 42 |
|
| 43 |
final int NUM = 1000000; |
| 44 |
final CountDownLatch latch = new CountDownLatch(1); |
| 45 |
final HashMap<String, Integer> map = new HashMap<>(); |
| 46 |
|
| 47 |
final Scheduler.Worker inner = Schedulers.computation().createWorker(); |
| 48 |
|
| 49 |
try { |
| 50 |
inner.schedule(new Runnable() { |
| 51 |
|
| 52 |
private HashMap<String, Integer> statefulMap = map; |
| 53 |
int nonThreadSafeCounter; |
| 54 |
|
| 55 |
@Override |
| 56 |
public void run() { |
| 57 |
Integer i = statefulMap.get("a"); |
| 58 |
if (i == null) { |
| 59 |
i = 1; |
| 60 |
statefulMap.put("a", i); |
| 61 |
statefulMap.put("b", i); |
| 62 |
} else { |
| 63 |
i++; |
| 64 |
statefulMap.put("a", i); |
| 65 |
statefulMap.put("b", i); |
| 66 |
} |
| 67 |
nonThreadSafeCounter++; |
| 68 |
statefulMap.put("nonThreadSafeCounter", nonThreadSafeCounter); |
| 69 |
if (i < NUM) { |
| 70 |
inner.schedule(this); |
| 71 |
} else { |
| 72 |
latch.countDown(); |
| 73 |
} |
| 74 |
} |
| 75 |
}); |
| 76 |
|
| 77 |
try { |
| 78 |
latch.await(); |
| 79 |
} catch (InterruptedException e) { |
| 80 |
e.printStackTrace(); |
| 81 |
} |
| 82 |
|
| 83 |
System.out.println("Count A: " + map.get("a")); |
| 84 |
System.out.println("Count B: " + map.get("b")); |
| 85 |
System.out.println("nonThreadSafeCounter: " + map.get("nonThreadSafeCounter")); |
| 86 |
|
| 87 |
assertEquals(NUM, map.get("a").intValue()); |
| 88 |
assertEquals(NUM, map.get("b").intValue()); |
| 89 |
assertEquals(NUM, map.get("nonThreadSafeCounter").intValue()); |
| 90 |
} finally { |
| 91 |
inner.dispose(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public final void computationThreadPool1() { |
| 97 |
Flowable<Integer> f1 = Flowable.<Integer> just(1, 2, 3, 4, 5); |
| 98 |
Flowable<Integer> f2 = Flowable.<Integer> just(6, 7, 8, 9, 10); |
| 99 |
Flowable<String> f = Flowable.<Integer> merge(f1, f2).map(new Function<Integer, String>() { |
| 100 |
|
| 101 |
@Override |
| 102 |
public String apply(Integer t) { |
| 103 |
assertTrue(Thread.currentThread().getName().startsWith("RxComputationThreadPool")); |
| 104 |
return "Value_" + t + "_Thread_" + Thread.currentThread().getName(); |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
f.subscribeOn(Schedulers.computation()).blockingForEach(new Consumer<String>() { |
| 109 |
|
| 110 |
@Override |
| 111 |
public void accept(String t) { |
| 112 |
System.out.println("t: " + t); |
| 113 |
} |
| 114 |
}); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public final void mergeWithExecutorScheduler() { |
| 119 |
|
| 120 |
final String currentThreadName = Thread.currentThread().getName(); |
| 121 |
|
| 122 |
Flowable<Integer> f1 = Flowable.<Integer> just(1, 2, 3, 4, 5); |
| 123 |
Flowable<Integer> f2 = Flowable.<Integer> just(6, 7, 8, 9, 10); |
| 124 |
Flowable<String> f = Flowable.<Integer> merge(f1, f2).subscribeOn(Schedulers.computation()).map(new Function<Integer, String>() { |
| 125 |
|
| 126 |
@Override |
| 127 |
public String apply(Integer t) { |
| 128 |
assertNotEquals(Thread.currentThread().getName(), currentThreadName); |
| 129 |
assertTrue(Thread.currentThread().getName().startsWith("RxComputationThreadPool")); |
| 130 |
return "Value_" + t + "_Thread_" + Thread.currentThread().getName(); |
| 131 |
} |
| 132 |
}); |
| 133 |
|
| 134 |
f.blockingForEach(new Consumer<String>() { |
| 135 |
|
| 136 |
@Override |
| 137 |
public void accept(String t) { |
| 138 |
System.out.println("t: " + t); |
| 139 |
} |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public final void handledErrorIsNotDeliveredToThreadHandler() throws InterruptedException { |
| 145 |
SchedulerTestHelper.handledErrorIsNotDeliveredToThreadHandler(getScheduler()); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void cancelledTaskRetention() throws InterruptedException { |
| 150 |
Worker w = Schedulers.computation().createWorker(); |
| 151 |
try { |
| 152 |
ExecutorSchedulerTest.cancelledRetention(w, false); |
| 153 |
} finally { |
| 154 |
w.dispose(); |
| 155 |
} |
| 156 |
w = Schedulers.computation().createWorker(); |
| 157 |
try { |
| 158 |
ExecutorSchedulerTest.cancelledRetention(w, true); |
| 159 |
} finally { |
| 160 |
w.dispose(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
@SuppressUndeliverable |
| 166 |
public void shutdownRejects() { |
| 167 |
final int[] calls = { 0 }; |
| 168 |
|
| 169 |
Runnable r = new Runnable() { |
| 170 |
@Override |
| 171 |
public void run() { |
| 172 |
calls[0]++; |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
Scheduler s = new ComputationScheduler(); |
| 177 |
s.shutdown(); |
| 178 |
s.shutdown(); |
| 179 |
|
| 180 |
assertEquals(Disposable.disposed(), s.scheduleDirect(r)); |
| 181 |
|
| 182 |
assertEquals(Disposable.disposed(), s.scheduleDirect(r, 1, TimeUnit.SECONDS)); |
| 183 |
|
| 184 |
assertEquals(Disposable.disposed(), s.schedulePeriodicallyDirect(r, 1, 1, TimeUnit.SECONDS)); |
| 185 |
|
| 186 |
Worker w = s.createWorker(); |
| 187 |
w.dispose(); |
| 188 |
|
| 189 |
assertTrue(w.isDisposed()); |
| 190 |
|
| 191 |
assertEquals(Disposable.disposed(), w.schedule(r)); |
| 192 |
|
| 193 |
assertEquals(Disposable.disposed(), w.schedule(r, 1, TimeUnit.SECONDS)); |
| 194 |
|
| 195 |
assertEquals(Disposable.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS)); |
| 196 |
|
| 197 |
assertEquals(0, calls[0]); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void exceptionFromObservableShouldNotBeSwallowed() throws Exception { |
| 202 |
CountDownLatch latch = new CountDownLatch(1); |
| 203 |
|
| 204 |
// #3 thread's uncaught exception handler |
| 205 |
Scheduler computationScheduler = new ComputationScheduler(new ThreadFactory() { |
| 206 |
@Override |
| 207 |
public Thread newThread(Runnable r) { |
| 208 |
Thread t = new Thread(r); |
| 209 |
t.setUncaughtExceptionHandler((thread, throwable) -> { |
| 210 |
latch.countDown(); |
| 211 |
}); |
| 212 |
return t; |
| 213 |
} |
| 214 |
}); |
| 215 |
|
| 216 |
// #2 RxJava exception handler |
| 217 |
RxJavaPlugins.setErrorHandler(h -> { |
| 218 |
latch.countDown(); |
| 219 |
}); |
| 220 |
|
| 221 |
// Exceptions, fatal or not, should be handled by |
| 222 |
// #1 observer's onError(), or |
| 223 |
// #2 RxJava exception handler, or |
| 224 |
// #3 thread's uncaught exception handler, |
| 225 |
// and should not be swallowed. |
| 226 |
try { |
| 227 |
|
| 228 |
// #1 observer's onError() |
| 229 |
Observable.create(s -> { |
| 230 |
|
| 231 |
s.onNext(1); |
| 232 |
throw new OutOfMemoryError(); |
| 233 |
}) |
| 234 |
.subscribeOn(computationScheduler) |
| 235 |
.subscribe(v -> { }, |
| 236 |
e -> { latch.countDown(); } |
| 237 |
); |
| 238 |
|
| 239 |
assertTrue(latch.await(2, TimeUnit.SECONDS)); |
| 240 |
} finally { |
| 241 |
RxJavaPlugins.reset(); |
| 242 |
computationScheduler.shutdown(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void exceptionFromObserverShouldNotBeSwallowed() throws Exception { |
| 248 |
CountDownLatch latch = new CountDownLatch(1); |
| 249 |
|
| 250 |
// #3 thread's uncaught exception handler |
| 251 |
Scheduler computationScheduler = new ComputationScheduler(new ThreadFactory() { |
| 252 |
@Override |
| 253 |
public Thread newThread(Runnable r) { |
| 254 |
Thread t = new Thread(r); |
| 255 |
t.setUncaughtExceptionHandler((thread, throwable) -> { |
| 256 |
latch.countDown(); |
| 257 |
}); |
| 258 |
return t; |
| 259 |
} |
| 260 |
}); |
| 261 |
|
| 262 |
// #2 RxJava exception handler |
| 263 |
RxJavaPlugins.setErrorHandler(h -> { |
| 264 |
latch.countDown(); |
| 265 |
}); |
| 266 |
|
| 267 |
// Exceptions, fatal or not, should be handled by |
| 268 |
// #1 observer's onError(), or |
| 269 |
// #2 RxJava exception handler, or |
| 270 |
// #3 thread's uncaught exception handler, |
| 271 |
// and should not be swallowed. |
| 272 |
try { |
| 273 |
|
| 274 |
// #1 observer's onError() |
| 275 |
Flowable.interval(500, TimeUnit.MILLISECONDS, computationScheduler) |
| 276 |
.subscribe(v -> { |
| 277 |
throw new OutOfMemoryError(); |
| 278 |
}, e -> { |
| 279 |
latch.countDown(); |
| 280 |
}); |
| 281 |
|
| 282 |
assertTrue(latch.await(2, TimeUnit.SECONDS)); |
| 283 |
} finally { |
| 284 |
RxJavaPlugins.reset(); |
| 285 |
computationScheduler.shutdown(); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
@SuppressUndeliverable |
| 291 |
public void periodicTaskShouldStopOnError() throws Exception { |
| 292 |
AtomicInteger repeatCount = new AtomicInteger(); |
| 293 |
|
| 294 |
Schedulers.computation().schedulePeriodicallyDirect(new Runnable() { |
| 295 |
@Override |
| 296 |
public void run() { |
| 297 |
repeatCount.incrementAndGet(); |
| 298 |
throw new OutOfMemoryError(); |
| 299 |
} |
| 300 |
}, 0, 1, TimeUnit.MILLISECONDS); |
| 301 |
|
| 302 |
Thread.sleep(200); |
| 303 |
|
| 304 |
assertEquals(1, repeatCount.get()); |
| 305 |
} |
| 306 |
|
| 307 |
@Test |
| 308 |
@SuppressUndeliverable |
| 309 |
public void periodicTaskShouldStopOnError2() throws Exception { |
| 310 |
AtomicInteger repeatCount = new AtomicInteger(); |
| 311 |
|
| 312 |
Schedulers.computation().schedulePeriodicallyDirect(new Runnable() { |
| 313 |
@Override |
| 314 |
public void run() { |
| 315 |
repeatCount.incrementAndGet(); |
| 316 |
throw new OutOfMemoryError(); |
| 317 |
} |
| 318 |
}, 0, 1, TimeUnit.NANOSECONDS); |
| 319 |
|
| 320 |
Thread.sleep(200); |
| 321 |
|
| 322 |
assertEquals(1, repeatCount.get()); |
| 323 |
} |
| 324 |
} |