| 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 io.reactivex.rxjava3.core.BackpressureOverflowStrategy.*; |
| 17 |
import static io.reactivex.rxjava3.internal.functions.Functions.EMPTY_ACTION; |
| 18 |
import static org.junit.Assert.assertEquals; |
| 19 |
|
| 20 |
import java.util.concurrent.TimeUnit; |
| 21 |
import java.util.concurrent.atomic.AtomicInteger; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 30 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 31 |
import io.reactivex.rxjava3.subscribers.*; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class FlowableOnBackpressureBufferStrategyTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void backpressureWithBufferDropOldest() throws InterruptedException { |
| 38 |
int bufferSize = 3; |
| 39 |
final AtomicInteger droppedCount = new AtomicInteger(0); |
| 40 |
Action incrementOnDrop = new Action() { |
| 41 |
@Override |
| 42 |
public void run() throws Exception { |
| 43 |
droppedCount.incrementAndGet(); |
| 44 |
} |
| 45 |
}; |
| 46 |
TestSubscriber<Long> ts = createTestSubscriber(); |
| 47 |
Flowable.fromPublisher(send500ValuesAndComplete.onBackpressureBuffer(bufferSize, incrementOnDrop, DROP_OLDEST)) |
| 48 |
.subscribe(ts); |
| 49 |
// we request 10 but only 3 should come from the buffer |
| 50 |
ts.request(10); |
| 51 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 52 |
assertEquals(bufferSize, ts.values().size()); |
| 53 |
ts.assertNoErrors(); |
| 54 |
assertEquals(497, ts.values().get(0).intValue()); |
| 55 |
assertEquals(498, ts.values().get(1).intValue()); |
| 56 |
assertEquals(499, ts.values().get(2).intValue()); |
| 57 |
assertEquals(droppedCount.get(), 500 - bufferSize); |
| 58 |
} |
| 59 |
|
| 60 |
private TestSubscriber<Long> createTestSubscriber() { |
| 61 |
return new TestSubscriber<>(new DefaultSubscriber<Long>() { |
| 62 |
|
| 63 |
@Override |
| 64 |
protected void onStart() { |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void onComplete() { |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onError(Throwable e) { |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onNext(Long t) { |
| 77 |
} |
| 78 |
|
| 79 |
}, 0L); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void backpressureWithBufferDropLatest() throws InterruptedException { |
| 84 |
int bufferSize = 3; |
| 85 |
final AtomicInteger droppedCount = new AtomicInteger(0); |
| 86 |
Action incrementOnDrop = new Action() { |
| 87 |
@Override |
| 88 |
public void run() throws Exception { |
| 89 |
droppedCount.incrementAndGet(); |
| 90 |
} |
| 91 |
}; |
| 92 |
TestSubscriber<Long> ts = createTestSubscriber(); |
| 93 |
Flowable.fromPublisher(send500ValuesAndComplete.onBackpressureBuffer(bufferSize, incrementOnDrop, DROP_LATEST)) |
| 94 |
.subscribe(ts); |
| 95 |
// we request 10 but only 3 should come from the buffer |
| 96 |
ts.request(10); |
| 97 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 98 |
assertEquals(bufferSize, ts.values().size()); |
| 99 |
ts.assertNoErrors(); |
| 100 |
assertEquals(0, ts.values().get(0).intValue()); |
| 101 |
assertEquals(1, ts.values().get(1).intValue()); |
| 102 |
assertEquals(499, ts.values().get(2).intValue()); |
| 103 |
assertEquals(droppedCount.get(), 500 - bufferSize); |
| 104 |
} |
| 105 |
|
| 106 |
private static final Flowable<Long> send500ValuesAndComplete = Flowable.unsafeCreate(new Publisher<Long>() { |
| 107 |
@Override |
| 108 |
public void subscribe(Subscriber<? super Long> s) { |
| 109 |
BooleanSubscription bs = new BooleanSubscription(); |
| 110 |
s.onSubscribe(bs); |
| 111 |
long i = 0; |
| 112 |
while (!bs.isCancelled() && i < 500) { |
| 113 |
s.onNext(i++); |
| 114 |
} |
| 115 |
if (!bs.isCancelled()) { |
| 116 |
s.onComplete(); |
| 117 |
} |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
@Test(expected = IllegalArgumentException.class) |
| 122 |
public void backpressureBufferNegativeCapacity() throws InterruptedException { |
| 123 |
Flowable.empty().onBackpressureBuffer(-1, EMPTY_ACTION , DROP_OLDEST); |
| 124 |
} |
| 125 |
|
| 126 |
@Test(expected = IllegalArgumentException.class) |
| 127 |
public void backpressureBufferZeroCapacity() throws InterruptedException { |
| 128 |
Flowable.empty().onBackpressureBuffer(0, EMPTY_ACTION , DROP_OLDEST); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void dispose() { |
| 133 |
TestHelper.checkDisposed(Flowable.just(1) |
| 134 |
.onBackpressureBuffer(16, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR)); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void error() { |
| 139 |
Flowable |
| 140 |
.error(new TestException()) |
| 141 |
.onBackpressureBuffer(16, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR) |
| 142 |
.test() |
| 143 |
.assertFailure(TestException.class); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void overflowError() { |
| 148 |
Flowable.range(1, 20) |
| 149 |
.onBackpressureBuffer(8, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR) |
| 150 |
.test(0L) |
| 151 |
.assertFailure(MissingBackpressureException.class); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void badSource() { |
| 156 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 157 |
@Override |
| 158 |
public Object apply(Flowable<Object> f) throws Exception { |
| 159 |
return f.onBackpressureBuffer(8, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR); |
| 160 |
} |
| 161 |
}, false, 1, 1, 1); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void doubleOnSubscribe() { |
| 166 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 167 |
@Override |
| 168 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 169 |
return f.onBackpressureBuffer(8, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR); |
| 170 |
} |
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void overflowCrashes() { |
| 176 |
Flowable.range(1, 20) |
| 177 |
.onBackpressureBuffer(8, new Action() { |
| 178 |
@Override |
| 179 |
public void run() throws Exception { |
| 180 |
throw new TestException(); |
| 181 |
} |
| 182 |
}, BackpressureOverflowStrategy.DROP_OLDEST) |
| 183 |
.test(0L) |
| 184 |
.assertFailure(TestException.class); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void badRequest() { |
| 189 |
TestHelper.assertBadRequestReported(Flowable.just(1) |
| 190 |
.onBackpressureBuffer(16, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR)); |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void empty() { |
| 195 |
Flowable.empty() |
| 196 |
.onBackpressureBuffer(16, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR) |
| 197 |
.test(0L) |
| 198 |
.assertResult(); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void justTake() { |
| 203 |
Flowable.just(1) |
| 204 |
.onBackpressureBuffer(16, Functions.EMPTY_ACTION, BackpressureOverflowStrategy.ERROR) |
| 205 |
.take(1) |
| 206 |
.test() |
| 207 |
.assertResult(1); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void overflowNullAction() { |
| 212 |
Flowable.range(1, 5) |
| 213 |
.onBackpressureBuffer(1, null, BackpressureOverflowStrategy.DROP_OLDEST) |
| 214 |
.test(0L) |
| 215 |
.assertEmpty(); |
| 216 |
} |
| 217 |
|
| 218 |
@Test |
| 219 |
public void cancelOnDrain() { |
| 220 |
Flowable.range(1, 5) |
| 221 |
.onBackpressureBuffer(10, null, BackpressureOverflowStrategy.DROP_OLDEST) |
| 222 |
.takeUntil(v -> true) |
| 223 |
.test(0L) |
| 224 |
.assertEmpty() |
| 225 |
.requestMore(10) |
| 226 |
.assertResult(1); |
| 227 |
} |
| 228 |
} |