| 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.ArgumentMatchers.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.mockito.InOrder; |
| 23 |
import org.reactivestreams.Subscriber; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.Function; |
| 28 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 29 |
import io.reactivex.rxjava3.schedulers.*; |
| 30 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 31 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 32 |
|
| 33 |
public class FlowableSkipLastTimedTest extends RxJavaTest { |
| 34 |
|
| 35 |
@Test |
| 36 |
public void skipLastTimed() { |
| 37 |
TestScheduler scheduler = new TestScheduler(); |
| 38 |
|
| 39 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 40 |
|
| 41 |
// FIXME the timeunit now matters due to rounding |
| 42 |
Flowable<Integer> result = source.skipLast(1000, TimeUnit.MILLISECONDS, scheduler); |
| 43 |
|
| 44 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 45 |
|
| 46 |
result.subscribe(subscriber); |
| 47 |
|
| 48 |
source.onNext(1); |
| 49 |
source.onNext(2); |
| 50 |
source.onNext(3); |
| 51 |
|
| 52 |
scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS); |
| 53 |
|
| 54 |
source.onNext(4); |
| 55 |
source.onNext(5); |
| 56 |
source.onNext(6); |
| 57 |
|
| 58 |
scheduler.advanceTimeBy(950, TimeUnit.MILLISECONDS); |
| 59 |
source.onComplete(); |
| 60 |
|
| 61 |
InOrder inOrder = inOrder(subscriber); |
| 62 |
inOrder.verify(subscriber).onNext(1); |
| 63 |
inOrder.verify(subscriber).onNext(2); |
| 64 |
inOrder.verify(subscriber).onNext(3); |
| 65 |
inOrder.verify(subscriber, never()).onNext(4); |
| 66 |
inOrder.verify(subscriber, never()).onNext(5); |
| 67 |
inOrder.verify(subscriber, never()).onNext(6); |
| 68 |
inOrder.verify(subscriber).onComplete(); |
| 69 |
inOrder.verifyNoMoreInteractions(); |
| 70 |
|
| 71 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void skipLastTimedErrorBeforeTime() { |
| 76 |
TestScheduler scheduler = new TestScheduler(); |
| 77 |
|
| 78 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 79 |
|
| 80 |
Flowable<Integer> result = source.skipLast(1, TimeUnit.SECONDS, scheduler); |
| 81 |
|
| 82 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 83 |
|
| 84 |
result.subscribe(subscriber); |
| 85 |
|
| 86 |
source.onNext(1); |
| 87 |
source.onNext(2); |
| 88 |
source.onNext(3); |
| 89 |
source.onError(new TestException()); |
| 90 |
|
| 91 |
scheduler.advanceTimeBy(1050, TimeUnit.MILLISECONDS); |
| 92 |
|
| 93 |
verify(subscriber).onError(any(TestException.class)); |
| 94 |
|
| 95 |
verify(subscriber, never()).onComplete(); |
| 96 |
verify(subscriber, never()).onNext(any()); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void skipLastTimedCompleteBeforeTime() { |
| 101 |
TestScheduler scheduler = new TestScheduler(); |
| 102 |
|
| 103 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 104 |
|
| 105 |
Flowable<Integer> result = source.skipLast(1, TimeUnit.SECONDS, scheduler); |
| 106 |
|
| 107 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 108 |
|
| 109 |
result.subscribe(subscriber); |
| 110 |
|
| 111 |
source.onNext(1); |
| 112 |
source.onNext(2); |
| 113 |
source.onNext(3); |
| 114 |
|
| 115 |
scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS); |
| 116 |
|
| 117 |
source.onComplete(); |
| 118 |
|
| 119 |
InOrder inOrder = inOrder(subscriber); |
| 120 |
inOrder.verify(subscriber).onComplete(); |
| 121 |
inOrder.verifyNoMoreInteractions(); |
| 122 |
|
| 123 |
verify(subscriber, never()).onNext(any()); |
| 124 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void skipLastTimedWhenAllElementsAreValid() { |
| 129 |
TestScheduler scheduler = new TestScheduler(); |
| 130 |
|
| 131 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 132 |
|
| 133 |
Flowable<Integer> result = source.skipLast(1, TimeUnit.MILLISECONDS, scheduler); |
| 134 |
|
| 135 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 136 |
|
| 137 |
result.subscribe(subscriber); |
| 138 |
|
| 139 |
source.onNext(1); |
| 140 |
source.onNext(2); |
| 141 |
source.onNext(3); |
| 142 |
|
| 143 |
scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS); |
| 144 |
|
| 145 |
source.onComplete(); |
| 146 |
|
| 147 |
InOrder inOrder = inOrder(subscriber); |
| 148 |
inOrder.verify(subscriber).onNext(1); |
| 149 |
inOrder.verify(subscriber).onNext(2); |
| 150 |
inOrder.verify(subscriber).onNext(3); |
| 151 |
inOrder.verify(subscriber).onComplete(); |
| 152 |
inOrder.verifyNoMoreInteractions(); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void skipLastTimedDefaultScheduler() { |
| 157 |
Flowable.just(1).concatWith(Flowable.just(2).delay(500, TimeUnit.MILLISECONDS)) |
| 158 |
.skipLast(300, TimeUnit.MILLISECONDS) |
| 159 |
.test() |
| 160 |
.awaitDone(5, TimeUnit.SECONDS) |
| 161 |
.assertResult(1); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void skipLastTimedDefaultSchedulerDelayError() { |
| 166 |
Flowable.just(1).concatWith(Flowable.just(2).delay(500, TimeUnit.MILLISECONDS)) |
| 167 |
.skipLast(300, TimeUnit.MILLISECONDS, true) |
| 168 |
.test() |
| 169 |
.awaitDone(5, TimeUnit.SECONDS) |
| 170 |
.assertResult(1); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void skipLastTimedCustomSchedulerDelayError() { |
| 175 |
Flowable.just(1).concatWith(Flowable.just(2).delay(500, TimeUnit.MILLISECONDS)) |
| 176 |
.skipLast(300, TimeUnit.MILLISECONDS, Schedulers.io(), true) |
| 177 |
.test() |
| 178 |
.awaitDone(5, TimeUnit.SECONDS) |
| 179 |
.assertResult(1); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void dispose() { |
| 184 |
TestHelper.checkDisposed(PublishProcessor.create().skipLast(1, TimeUnit.DAYS)); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void doubleOnSubscribe() { |
| 189 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 190 |
@Override |
| 191 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 192 |
return f.skipLast(1, TimeUnit.DAYS); |
| 193 |
} |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void onNextDisposeRace() { |
| 199 |
TestScheduler scheduler = new TestScheduler(); |
| 200 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 201 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 202 |
|
| 203 |
final TestSubscriber<Integer> ts = pp.skipLast(1, TimeUnit.DAYS, scheduler).test(); |
| 204 |
|
| 205 |
Runnable r1 = new Runnable() { |
| 206 |
@Override |
| 207 |
public void run() { |
| 208 |
pp.onComplete(); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
Runnable r2 = new Runnable() { |
| 213 |
@Override |
| 214 |
public void run() { |
| 215 |
ts.cancel(); |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
TestHelper.race(r1, r2); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void errorDelayed() { |
| 225 |
Flowable.error(new TestException()) |
| 226 |
.skipLast(1, TimeUnit.DAYS, new TestScheduler(), true) |
| 227 |
.test() |
| 228 |
.assertFailure(TestException.class); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void take() { |
| 233 |
Flowable.just(1) |
| 234 |
.skipLast(0, TimeUnit.SECONDS) |
| 235 |
.take(1) |
| 236 |
.test() |
| 237 |
.awaitDone(5, TimeUnit.SECONDS) |
| 238 |
.assertResult(1); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void observeOn() { |
| 243 |
Flowable.range(1, 1000) |
| 244 |
.skipLast(0, TimeUnit.SECONDS) |
| 245 |
.observeOn(Schedulers.single(), false, 16) |
| 246 |
.to(TestHelper.<Integer>testConsumer()) |
| 247 |
.awaitDone(5, TimeUnit.SECONDS) |
| 248 |
.assertSubscribed() |
| 249 |
.assertValueCount(1000) |
| 250 |
.assertComplete() |
| 251 |
.assertNoErrors(); |
| 252 |
} |
| 253 |
|
| 254 |
@Test |
| 255 |
public void badRequest() { |
| 256 |
TestHelper.assertBadRequestReported(Flowable.never().skipLast(1, TimeUnit.MINUTES)); |
| 257 |
} |
| 258 |
|
| 259 |
@Test |
| 260 |
public void delayErrorMoreWork() { |
| 261 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 262 |
|
| 263 |
TestSubscriber<Integer> ts = pp.skipLast(0, TimeUnit.MILLISECONDS, true) |
| 264 |
.doOnNext(v -> { |
| 265 |
if (v == 1) { |
| 266 |
pp.onNext(1); |
| 267 |
pp.onComplete(); |
| 268 |
} |
| 269 |
}) |
| 270 |
.test(); |
| 271 |
|
| 272 |
pp.onNext(1); |
| 273 |
|
| 274 |
ts.assertComplete(); |
| 275 |
} |
| 276 |
} |