| 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.ArgumentMatchers.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.*; |
| 22 |
import org.mockito.InOrder; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 28 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class ObservableThrottleFirstTest extends RxJavaTest { |
| 32 |
|
| 33 |
private TestScheduler scheduler; |
| 34 |
private Scheduler.Worker innerScheduler; |
| 35 |
private Observer<String> observer; |
| 36 |
|
| 37 |
@Before |
| 38 |
public void before() { |
| 39 |
scheduler = new TestScheduler(); |
| 40 |
innerScheduler = scheduler.createWorker(); |
| 41 |
observer = TestHelper.mockObserver(); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void throttlingWithCompleted() { |
| 46 |
Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() { |
| 47 |
@Override |
| 48 |
public void subscribe(Observer<? super String> innerObserver) { |
| 49 |
innerObserver.onSubscribe(Disposable.empty()); |
| 50 |
publishNext(innerObserver, 100, "one"); // publish as it's first |
| 51 |
publishNext(innerObserver, 300, "two"); // skip as it's last within the first 400 |
| 52 |
publishNext(innerObserver, 900, "three"); // publish |
| 53 |
publishNext(innerObserver, 905, "four"); // skip |
| 54 |
publishCompleted(innerObserver, 1000); // Should be published as soon as the timeout expires. |
| 55 |
} |
| 56 |
}); |
| 57 |
|
| 58 |
Observable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler); |
| 59 |
sampled.subscribe(observer); |
| 60 |
|
| 61 |
InOrder inOrder = inOrder(observer); |
| 62 |
|
| 63 |
scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS); |
| 64 |
inOrder.verify(observer, times(1)).onNext("one"); |
| 65 |
inOrder.verify(observer, times(0)).onNext("two"); |
| 66 |
inOrder.verify(observer, times(1)).onNext("three"); |
| 67 |
inOrder.verify(observer, times(0)).onNext("four"); |
| 68 |
inOrder.verify(observer, times(1)).onComplete(); |
| 69 |
inOrder.verifyNoMoreInteractions(); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void throttlingWithError() { |
| 74 |
Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() { |
| 75 |
@Override |
| 76 |
public void subscribe(Observer<? super String> innerObserver) { |
| 77 |
innerObserver.onSubscribe(Disposable.empty()); |
| 78 |
Exception error = new TestException(); |
| 79 |
publishNext(innerObserver, 100, "one"); // Should be published since it is first |
| 80 |
publishNext(innerObserver, 200, "two"); // Should be skipped since onError will arrive before the timeout expires |
| 81 |
publishError(innerObserver, 300, error); // Should be published as soon as the timeout expires. |
| 82 |
} |
| 83 |
}); |
| 84 |
|
| 85 |
Observable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler); |
| 86 |
sampled.subscribe(observer); |
| 87 |
|
| 88 |
InOrder inOrder = inOrder(observer); |
| 89 |
|
| 90 |
scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS); |
| 91 |
inOrder.verify(observer).onNext("one"); |
| 92 |
inOrder.verify(observer).onError(any(TestException.class)); |
| 93 |
inOrder.verifyNoMoreInteractions(); |
| 94 |
} |
| 95 |
|
| 96 |
private <T> void publishCompleted(final Observer<T> innerObserver, long delay) { |
| 97 |
innerScheduler.schedule(new Runnable() { |
| 98 |
@Override |
| 99 |
public void run() { |
| 100 |
innerObserver.onComplete(); |
| 101 |
} |
| 102 |
}, delay, TimeUnit.MILLISECONDS); |
| 103 |
} |
| 104 |
|
| 105 |
private <T> void publishError(final Observer<T> innerObserver, long delay, final Exception error) { |
| 106 |
innerScheduler.schedule(new Runnable() { |
| 107 |
@Override |
| 108 |
public void run() { |
| 109 |
innerObserver.onError(error); |
| 110 |
} |
| 111 |
}, delay, TimeUnit.MILLISECONDS); |
| 112 |
} |
| 113 |
|
| 114 |
private <T> void publishNext(final Observer<T> innerObserver, long delay, final T value) { |
| 115 |
innerScheduler.schedule(new Runnable() { |
| 116 |
@Override |
| 117 |
public void run() { |
| 118 |
innerObserver.onNext(value); |
| 119 |
} |
| 120 |
}, delay, TimeUnit.MILLISECONDS); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void throttle() { |
| 125 |
Observer<Integer> observer = TestHelper.mockObserver(); |
| 126 |
TestScheduler s = new TestScheduler(); |
| 127 |
PublishSubject<Integer> o = PublishSubject.create(); |
| 128 |
o.throttleFirst(500, TimeUnit.MILLISECONDS, s).subscribe(observer); |
| 129 |
|
| 130 |
// send events with simulated time increments |
| 131 |
s.advanceTimeTo(0, TimeUnit.MILLISECONDS); |
| 132 |
o.onNext(1); // deliver |
| 133 |
o.onNext(2); // skip |
| 134 |
s.advanceTimeTo(501, TimeUnit.MILLISECONDS); |
| 135 |
o.onNext(3); // deliver |
| 136 |
s.advanceTimeTo(600, TimeUnit.MILLISECONDS); |
| 137 |
o.onNext(4); // skip |
| 138 |
s.advanceTimeTo(700, TimeUnit.MILLISECONDS); |
| 139 |
o.onNext(5); // skip |
| 140 |
o.onNext(6); // skip |
| 141 |
s.advanceTimeTo(1001, TimeUnit.MILLISECONDS); |
| 142 |
o.onNext(7); // deliver |
| 143 |
s.advanceTimeTo(1501, TimeUnit.MILLISECONDS); |
| 144 |
o.onComplete(); |
| 145 |
|
| 146 |
InOrder inOrder = inOrder(observer); |
| 147 |
inOrder.verify(observer).onNext(1); |
| 148 |
inOrder.verify(observer).onNext(3); |
| 149 |
inOrder.verify(observer).onNext(7); |
| 150 |
inOrder.verify(observer).onComplete(); |
| 151 |
inOrder.verifyNoMoreInteractions(); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void throttleFirstDefaultScheduler() { |
| 156 |
Observable.just(1).throttleFirst(100, TimeUnit.MILLISECONDS) |
| 157 |
.test() |
| 158 |
.awaitDone(5, TimeUnit.SECONDS) |
| 159 |
.assertResult(1); |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void dispose() { |
| 164 |
TestHelper.checkDisposed(Observable.just(1).throttleFirst(1, TimeUnit.DAYS)); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void doubleOnSubscribe() { |
| 169 |
TestHelper.checkDoubleOnSubscribeObservable(o -> o.throttleFirst(1, TimeUnit.SECONDS)); |
| 170 |
} |
| 171 |
} |