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