-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathTestScheduler.java
201 lines (172 loc) · 6.2 KB
/
TestScheduler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.schedulers;
import java.util.Queue;
import java.util.concurrent.*;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
/**
* A special, non thread-safe scheduler for testing operators that require
* a scheduler without introducing real concurrency and allows manually advancing
* a virtual time.
*/
public final class TestScheduler extends Scheduler {
/** The ordered queue for the runnable tasks. */
final Queue<TimedRunnable> queue = new PriorityBlockingQueue<TimedRunnable>(11);
/** The per-scheduler global order counter. */
long counter;
// Storing time in nanoseconds internally.
volatile long time;
/**
* Creates a new TestScheduler with initial virtual time of zero.
*/
public TestScheduler() {
// No-op.
}
/**
* Creates a new TestScheduler with the specified initial virtual time.
*
* @param delayTime
* the point in time to move the Scheduler's clock to
* @param unit
* the units of time that {@code delayTime} is expressed in
*/
public TestScheduler(long delayTime, TimeUnit unit) {
time = unit.toNanos(delayTime);
}
static final class TimedRunnable implements Comparable<TimedRunnable> {
final long time;
final Runnable run;
final TestWorker scheduler;
final long count; // for differentiating tasks at same time
TimedRunnable(TestWorker scheduler, long time, Runnable run, long count) {
this.time = time;
this.run = run;
this.scheduler = scheduler;
this.count = count;
}
@Override
public String toString() {
return String.format("TimedRunnable(time = %d, run = %s)", time, run.toString());
}
@Override
public int compareTo(TimedRunnable o) {
if (time == o.time) {
return Long.compare(count, o.count);
}
return Long.compare(time, o.time);
}
}
@Override
public long now(@NonNull TimeUnit unit) {
return unit.convert(time, TimeUnit.NANOSECONDS);
}
/**
* Moves the Scheduler's clock forward by a specified amount of time.
*
* @param delayTime
* the amount of time to move the Scheduler's clock forward
* @param unit
* the units of time that {@code delayTime} is expressed in
*/
public void advanceTimeBy(long delayTime, TimeUnit unit) {
advanceTimeTo(time + unit.toNanos(delayTime), TimeUnit.NANOSECONDS);
}
/**
* Moves the Scheduler's clock to a particular moment in time.
*
* @param delayTime
* the point in time to move the Scheduler's clock to
* @param unit
* the units of time that {@code delayTime} is expressed in
*/
public void advanceTimeTo(long delayTime, TimeUnit unit) {
long targetTime = unit.toNanos(delayTime);
triggerActions(targetTime);
}
/**
* Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or
* before this Scheduler's present time.
*/
public void triggerActions() {
triggerActions(time);
}
private void triggerActions(long targetTimeInNanoseconds) {
for (;;) {
TimedRunnable current = queue.peek();
if (current == null || current.time > targetTimeInNanoseconds) {
break;
}
// if scheduled time is 0 (immediate) use current virtual time
time = current.time == 0 ? time : current.time;
queue.remove(current);
// Only execute if not unsubscribed
if (!current.scheduler.disposed) {
current.run.run();
}
}
time = targetTimeInNanoseconds;
}
@NonNull
@Override
public Worker createWorker() {
return new TestWorker();
}
final class TestWorker extends Worker {
volatile boolean disposed;
@Override
public void dispose() {
disposed = true;
}
@Override
public boolean isDisposed() {
return disposed;
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable run, long delayTime, @NonNull TimeUnit unit) {
if (disposed) {
return EmptyDisposable.INSTANCE;
}
final TimedRunnable timedAction = new TimedRunnable(this, time + unit.toNanos(delayTime), run, counter++);
queue.add(timedAction);
return Disposables.fromRunnable(new QueueRemove(timedAction));
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable run) {
if (disposed) {
return EmptyDisposable.INSTANCE;
}
final TimedRunnable timedAction = new TimedRunnable(this, 0, run, counter++);
queue.add(timedAction);
return Disposables.fromRunnable(new QueueRemove(timedAction));
}
@Override
public long now(@NonNull TimeUnit unit) {
return TestScheduler.this.now(unit);
}
final class QueueRemove implements Runnable {
final TimedRunnable timedAction;
QueueRemove(TimedRunnable timedAction) {
this.timedAction = timedAction;
}
@Override
public void run() {
queue.remove(timedAction);
}
}
}
}