Debian Codegraph @

src/test/java/io/reactivex/rxjava3/schedulers/NewThreadSchedulerTest.java

raw
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.concurrent.TimeUnit;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.Scheduler;
23 import io.reactivex.rxjava3.core.Scheduler.Worker;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.internal.schedulers.NewThreadWorker;
26 import io.reactivex.rxjava3.testsupport.SuppressUndeliverable;
27
28 public class NewThreadSchedulerTest extends AbstractSchedulerConcurrencyTests {
29
30 @Override
31 protected Scheduler getScheduler() {
32 return Schedulers.newThread();
33 }
34
35 @Test
36 public final void handledErrorIsNotDeliveredToThreadHandler() throws InterruptedException {
37 SchedulerTestHelper.handledErrorIsNotDeliveredToThreadHandler(getScheduler());
38 }
39
40 @Test
41 @SuppressUndeliverable
42 public void shutdownRejects() {
43 final int[] calls = { 0 };
44
45 Runnable r = new Runnable() {
46 @Override
47 public void run() {
48 calls[0]++;
49 }
50 };
51
52 Scheduler s = getScheduler();
53 Worker w = s.createWorker();
54 w.dispose();
55
56 assertTrue(w.isDisposed());
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 NewThreadWorker actual = (NewThreadWorker)w;
65
66 CompositeDisposable cd = new CompositeDisposable();
67
68 actual.scheduleActual(r, 1, TimeUnit.SECONDS, cd);
69
70 assertEquals(0, cd.size());
71
72 assertEquals(0, calls[0]);
73 }
74
75 /**
76 * Regression test to ensure there is no NPE when the worker has been disposed.
77 * @throws Exception on error
78 */
79 @Test
80 @SuppressUndeliverable
81 public void npeRegression() throws Exception {
82 Scheduler s = getScheduler();
83 NewThreadWorker w = (NewThreadWorker) s.createWorker();
84 w.dispose();
85
86 //This method used to throw a NPE when the worker has been disposed and the parent is null
87 w.scheduleActual(new Runnable() {
88 @Override
89 public void run() {
90 }
91 }, 0, TimeUnit.MILLISECONDS, null);
92
93 }
94 }