Debian Codegraph @

src/test/java/io/reactivex/rxjava3/core/SchedulerTest.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.core;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19
20 import org.junit.After;
21 import org.junit.Test;
22
23 import java.util.concurrent.TimeUnit;
24
25 public class SchedulerTest {
26 private static final String DRIFT_USE_NANOTIME = "rx3.scheduler.use-nanotime";
27
28 @After
29 public void cleanup() {
30 // reset value to default in order to not influence other tests
31 Scheduler.IS_DRIFT_USE_NANOTIME = false;
32 }
33
34 @Test
35 public void driftUseNanoTimeNotSetByDefault() {
36 assertFalse(Scheduler.IS_DRIFT_USE_NANOTIME);
37 assertFalse(Boolean.getBoolean(DRIFT_USE_NANOTIME));
38 }
39
40 @Test
41 public void computeNow_currentTimeMillis() {
42 TimeUnit unit = TimeUnit.MILLISECONDS;
43 assertTrue(isInRange(System.currentTimeMillis(), Scheduler.computeNow(unit), unit, 250, TimeUnit.MILLISECONDS));
44 }
45
46 @Test
47 public void computeNow_nanoTime() {
48 TimeUnit unit = TimeUnit.NANOSECONDS;
49 Scheduler.IS_DRIFT_USE_NANOTIME = true;
50
51 assertFalse(isInRange(System.currentTimeMillis(), Scheduler.computeNow(unit), unit, 250, TimeUnit.MILLISECONDS));
52 assertTrue(isInRange(System.nanoTime(), Scheduler.computeNow(unit), TimeUnit.NANOSECONDS, 250, TimeUnit.MILLISECONDS));
53 }
54
55 private boolean isInRange(long start, long stop, TimeUnit source, long maxDiff, TimeUnit diffUnit) {
56 long diff = Math.abs(stop - start);
57 return diffUnit.convert(diff, source) <= maxDiff;
58 }
59
60 @Test
61 public void clockDriftCalculation() {
62 assertEquals(100_000_000L, Scheduler.computeClockDrift(100, "milliseconds"));
63
64 assertEquals(2_000_000_000L, Scheduler.computeClockDrift(2, "seconds"));
65
66 assertEquals(180_000_000_000L, Scheduler.computeClockDrift(3, "minutes"));
67
68 assertEquals(240_000_000_000L, Scheduler.computeClockDrift(4, "random"));
69
70 assertEquals(300_000_000_000L, Scheduler.computeClockDrift(5, null));
71 }
72
73 }