Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableTimeIntervalTest.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.internal.operators.observable;
15
16 import static org.mockito.Mockito.*;
17
18 import java.util.concurrent.TimeUnit;
19
20 import org.junit.*;
21 import org.mockito.InOrder;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Function;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.schedulers.*;
28 import io.reactivex.rxjava3.subjects.PublishSubject;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class ObservableTimeIntervalTest extends RxJavaTest {
32
33 private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
34
35 private Observer<Timed<Integer>> observer;
36
37 private TestScheduler testScheduler;
38 private PublishSubject<Integer> subject;
39 private Observable<Timed<Integer>> observable;
40
41 @Before
42 public void setUp() {
43 observer = TestHelper.mockObserver();
44 testScheduler = new TestScheduler();
45 subject = PublishSubject.create();
46 observable = subject.timeInterval(testScheduler);
47 }
48
49 @Test
50 public void timeInterval() {
51 InOrder inOrder = inOrder(observer);
52 observable.subscribe(observer);
53
54 testScheduler.advanceTimeBy(1000, TIME_UNIT);
55 subject.onNext(1);
56 testScheduler.advanceTimeBy(2000, TIME_UNIT);
57 subject.onNext(2);
58 testScheduler.advanceTimeBy(3000, TIME_UNIT);
59 subject.onNext(3);
60 subject.onComplete();
61
62 inOrder.verify(observer, times(1)).onNext(
63 new Timed<>(1, 1000, TIME_UNIT));
64 inOrder.verify(observer, times(1)).onNext(
65 new Timed<>(2, 2000, TIME_UNIT));
66 inOrder.verify(observer, times(1)).onNext(
67 new Timed<>(3, 3000, TIME_UNIT));
68 inOrder.verify(observer, times(1)).onComplete();
69 inOrder.verifyNoMoreInteractions();
70 }
71
72 @Test
73 public void timeIntervalDefault() {
74 final TestScheduler scheduler = new TestScheduler();
75
76 RxJavaPlugins.setComputationSchedulerHandler(new Function<Scheduler, Scheduler>() {
77 @Override
78 public Scheduler apply(Scheduler v) throws Exception {
79 return scheduler;
80 }
81 });
82
83 try {
84 Observable.range(1, 5)
85 .timeInterval()
86 .map(new Function<Timed<Integer>, Long>() {
87 @Override
88 public Long apply(Timed<Integer> v) throws Exception {
89 return v.time();
90 }
91 })
92 .test()
93 .assertResult(0L, 0L, 0L, 0L, 0L);
94 } finally {
95 RxJavaPlugins.reset();
96 }
97 }
98
99 @Test
100 public void timeIntervalDefaultSchedulerCustomUnit() {
101 final TestScheduler scheduler = new TestScheduler();
102
103 RxJavaPlugins.setComputationSchedulerHandler(new Function<Scheduler, Scheduler>() {
104 @Override
105 public Scheduler apply(Scheduler v) throws Exception {
106 return scheduler;
107 }
108 });
109
110 try {
111 Observable.range(1, 5)
112 .timeInterval(TimeUnit.SECONDS)
113 .map(new Function<Timed<Integer>, Long>() {
114 @Override
115 public Long apply(Timed<Integer> v) throws Exception {
116 return v.time();
117 }
118 })
119 .test()
120 .assertResult(0L, 0L, 0L, 0L, 0L);
121 } finally {
122 RxJavaPlugins.reset();
123 }
124 }
125
126 @Test
127 public void dispose() {
128 TestHelper.checkDisposed(Observable.just(1).timeInterval());
129 }
130
131 @Test
132 public void error() {
133 Observable.error(new TestException())
134 .timeInterval()
135 .test()
136 .assertFailure(TestException.class);
137 }
138
139 @Test
140 public void doubleOnSubscribe() {
141 TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, Observable<Timed<Object>>>() {
142 @Override
143 public Observable<Timed<Object>> apply(Observable<Object> f)
144 throws Exception {
145 return f.timeInterval();
146 }
147 });
148 }
149 }