Debian Codegraph @

src/test/java/io/reactivex/rxjava3/schedulers/TrampolineSchedulerTest.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.assertEquals;
17
18 import java.util.*;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
22 import org.reactivestreams.*;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.core.Scheduler.Worker;
26 import io.reactivex.rxjava3.disposables.*;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.subscribers.TestSubscriber;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class TrampolineSchedulerTest extends AbstractSchedulerTests {
33
34 @Override
35 protected Scheduler getScheduler() {
36 return Schedulers.trampoline();
37 }
38
39 @Test
40 public final void mergeWithCurrentThreadScheduler1() {
41
42 final String currentThreadName = Thread.currentThread().getName();
43
44 Flowable<Integer> f1 = Flowable.<Integer> just(1, 2, 3, 4, 5);
45 Flowable<Integer> f2 = Flowable.<Integer> just(6, 7, 8, 9, 10);
46 Flowable<String> f = Flowable.<Integer> merge(f1, f2).subscribeOn(Schedulers.trampoline()).map(new Function<Integer, String>() {
47
48 @Override
49 public String apply(Integer t) {
50 assertEquals(Thread.currentThread().getName(), currentThreadName);
51 return "Value_" + t + "_Thread_" + Thread.currentThread().getName();
52 }
53 });
54
55 f.blockingForEach(new Consumer<String>() {
56
57 @Override
58 public void accept(String t) {
59 System.out.println("t: " + t);
60 }
61 });
62 }
63
64 @Test
65 public void nestedTrampolineWithUnsubscribe() {
66 final ArrayList<String> workDone = new ArrayList<>();
67 final CompositeDisposable workers = new CompositeDisposable();
68 Worker worker = Schedulers.trampoline().createWorker();
69 try {
70 workers.add(worker);
71 worker.schedule(new Runnable() {
72
73 @Override
74 public void run() {
75 workers.add(doWorkOnNewTrampoline("A", workDone));
76 }
77
78 });
79
80 final Worker worker2 = Schedulers.trampoline().createWorker();
81 workers.add(worker2);
82 worker2.schedule(new Runnable() {
83
84 @Override
85 public void run() {
86 workers.add(doWorkOnNewTrampoline("B", workDone));
87 // we unsubscribe worker2 ... it should not affect work scheduled on a separate Trampline.Worker
88 worker2.dispose();
89 }
90
91 });
92
93 assertEquals(6, workDone.size());
94 assertEquals(Arrays.asList("A.1", "A.B.1", "A.B.2", "B.1", "B.B.1", "B.B.2"), workDone);
95 } finally {
96 workers.dispose();
97 }
98 }
99
100 /**
101 * This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
102 * action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
103 * sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
104 * trampoline with enqueue requests from multiple threads concurrently.
105 */
106 @Test
107 public void trampolineWorkerHandlesConcurrentScheduling() {
108 final Worker trampolineWorker = Schedulers.trampoline().createWorker();
109 final Subscriber<Object> subscriber = TestHelper.mockSubscriber();
110 final TestSubscriber<Disposable> ts = new TestSubscriber<>(subscriber);
111
112 // Spam the trampoline with actions.
113 Flowable.range(0, 50)
114 .flatMap(new Function<Integer, Publisher<Disposable>>() {
115 @Override
116 public Publisher<Disposable> apply(Integer count) {
117 return Flowable
118 .interval(1, TimeUnit.MICROSECONDS)
119 .map(new Function<Long, Disposable>() {
120 @Override
121 public Disposable apply(Long ount1) {
122 return trampolineWorker.schedule(Functions.EMPTY_RUNNABLE);
123 }
124 }).take(100);
125 }
126 })
127 .subscribeOn(Schedulers.computation())
128 .subscribe(ts);
129 ts.awaitDone(5, TimeUnit.SECONDS);
130 ts.assertNoErrors();
131 }
132
133 private static Worker doWorkOnNewTrampoline(final String key, final ArrayList<String> workDone) {
134 Worker worker = Schedulers.trampoline().createWorker();
135 worker.schedule(new Runnable() {
136
137 @Override
138 public void run() {
139 String msg = key + ".1";
140 workDone.add(msg);
141 System.out.println(msg);
142 Worker worker3 = Schedulers.trampoline().createWorker();
143 worker3.schedule(createPrintAction(key + ".B.1", workDone));
144 worker3.schedule(createPrintAction(key + ".B.2", workDone));
145 }
146
147 });
148 return worker;
149 }
150
151 private static Runnable createPrintAction(final String message, final ArrayList<String> workDone) {
152 return new Runnable() {
153
154 @Override
155 public void run() {
156 System.out.println(message);
157 workDone.add(message);
158 }
159
160 };
161 }
162 }