| 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.testsupport; |
| 15 |
|
| 16 |
import java.util.concurrent.TimeUnit; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.Scheduler; |
| 19 |
import io.reactivex.rxjava3.disposables.*; |
| 20 |
|
| 21 |
/** |
| 22 |
* Basic scheduler that produces an ever increasing {@link #now(TimeUnit)} value. |
| 23 |
* Use this scheduler only as a time source! |
| 24 |
*/ |
| 25 |
public final class TimesteppingScheduler extends Scheduler { |
| 26 |
|
| 27 |
final class TimesteppingWorker extends Worker { |
| 28 |
@Override |
| 29 |
public void dispose() { |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public boolean isDisposed() { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public Disposable schedule(Runnable run, long delay, TimeUnit unit) { |
| 39 |
run.run(); |
| 40 |
return Disposable.disposed(); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public long now(TimeUnit unit) { |
| 45 |
return TimesteppingScheduler.this.now(unit); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public long time; |
| 50 |
|
| 51 |
public boolean stepEnabled = true; |
| 52 |
|
| 53 |
@Override |
| 54 |
public Worker createWorker() { |
| 55 |
return new TimesteppingWorker(); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public long now(TimeUnit unit) { |
| 60 |
if (stepEnabled) { |
| 61 |
return time++; |
| 62 |
} |
| 63 |
return time; |
| 64 |
} |
| 65 |
} |