| 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.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.Consumer; |
| 25 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 26 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 27 |
import io.reactivex.rxjava3.processors.UnicastProcessor; |
| 28 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 29 |
import io.reactivex.rxjava3.testsupport.*; |
| 30 |
|
| 31 |
public class FlowableDoAfterNextTest extends RxJavaTest { |
| 32 |
|
| 33 |
final List<Integer> values = new ArrayList<>(); |
| 34 |
|
| 35 |
final Consumer<Integer> afterNext = new Consumer<Integer>() { |
| 36 |
@Override |
| 37 |
public void accept(Integer e) throws Exception { |
| 38 |
values.add(-e); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
final TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 43 |
@Override |
| 44 |
public void onNext(Integer t) { |
| 45 |
super.onNext(t); |
| 46 |
FlowableDoAfterNextTest.this.values.add(t); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
@Test |
| 51 |
public void just() { |
| 52 |
Flowable.just(1) |
| 53 |
.doAfterNext(afterNext) |
| 54 |
.subscribeWith(ts) |
| 55 |
.assertResult(1); |
| 56 |
|
| 57 |
assertEquals(Arrays.asList(1, -1), values); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void range() { |
| 62 |
Flowable.range(1, 5) |
| 63 |
.doAfterNext(afterNext) |
| 64 |
.subscribeWith(ts) |
| 65 |
.assertResult(1, 2, 3, 4, 5); |
| 66 |
|
| 67 |
assertEquals(Arrays.asList(1, -1, 2, -2, 3, -3, 4, -4, 5, -5), values); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void error() { |
| 72 |
Flowable.<Integer>error(new TestException()) |
| 73 |
.doAfterNext(afterNext) |
| 74 |
.subscribeWith(ts) |
| 75 |
.assertFailure(TestException.class); |
| 76 |
|
| 77 |
assertTrue(values.isEmpty()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void empty() { |
| 82 |
Flowable.<Integer>empty() |
| 83 |
.doAfterNext(afterNext) |
| 84 |
.subscribeWith(ts) |
| 85 |
.assertResult(); |
| 86 |
|
| 87 |
assertTrue(values.isEmpty()); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void syncFused() { |
| 92 |
TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC); |
| 93 |
|
| 94 |
Flowable.range(1, 5) |
| 95 |
.doAfterNext(afterNext) |
| 96 |
.subscribe(ts0); |
| 97 |
|
| 98 |
ts0.assertFusionMode(QueueFuseable.SYNC) |
| 99 |
.assertResult(1, 2, 3, 4, 5); |
| 100 |
|
| 101 |
assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void asyncFusedRejected() { |
| 106 |
TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC); |
| 107 |
|
| 108 |
Flowable.range(1, 5) |
| 109 |
.doAfterNext(afterNext) |
| 110 |
.subscribe(ts0); |
| 111 |
|
| 112 |
ts0.assertFusionMode(QueueFuseable.NONE) |
| 113 |
.assertResult(1, 2, 3, 4, 5); |
| 114 |
|
| 115 |
assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void asyncFused() { |
| 120 |
TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC); |
| 121 |
|
| 122 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 123 |
|
| 124 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 125 |
|
| 126 |
up |
| 127 |
.doAfterNext(afterNext) |
| 128 |
.subscribe(ts0); |
| 129 |
|
| 130 |
ts0.assertFusionMode(QueueFuseable.ASYNC) |
| 131 |
.assertResult(1, 2, 3, 4, 5); |
| 132 |
|
| 133 |
assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void justConditional() { |
| 138 |
Flowable.just(1) |
| 139 |
.doAfterNext(afterNext) |
| 140 |
.filter(Functions.alwaysTrue()) |
| 141 |
.subscribeWith(ts) |
| 142 |
.assertResult(1); |
| 143 |
|
| 144 |
assertEquals(Arrays.asList(1, -1), values); |
| 145 |
} |
| 146 |
|
| 147 |
@Test |
| 148 |
public void rangeConditional() { |
| 149 |
Flowable.range(1, 5) |
| 150 |
.doAfterNext(afterNext) |
| 151 |
.filter(Functions.alwaysTrue()) |
| 152 |
.subscribeWith(ts) |
| 153 |
.assertResult(1, 2, 3, 4, 5); |
| 154 |
|
| 155 |
assertEquals(Arrays.asList(1, -1, 2, -2, 3, -3, 4, -4, 5, -5), values); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void errorConditional() { |
| 160 |
Flowable.<Integer>error(new TestException()) |
| 161 |
.doAfterNext(afterNext) |
| 162 |
.filter(Functions.alwaysTrue()) |
| 163 |
.subscribeWith(ts) |
| 164 |
.assertFailure(TestException.class); |
| 165 |
|
| 166 |
assertTrue(values.isEmpty()); |
| 167 |
} |
| 168 |
|
| 169 |
@Test |
| 170 |
public void emptyConditional() { |
| 171 |
Flowable.<Integer>empty() |
| 172 |
.doAfterNext(afterNext) |
| 173 |
.filter(Functions.alwaysTrue()) |
| 174 |
.subscribeWith(ts) |
| 175 |
.assertResult(); |
| 176 |
|
| 177 |
assertTrue(values.isEmpty()); |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
public void syncFusedConditional() { |
| 182 |
TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC); |
| 183 |
|
| 184 |
Flowable.range(1, 5) |
| 185 |
.doAfterNext(afterNext) |
| 186 |
.filter(Functions.alwaysTrue()) |
| 187 |
.subscribe(ts0); |
| 188 |
|
| 189 |
ts0.assertFusionMode(QueueFuseable.SYNC) |
| 190 |
.assertResult(1, 2, 3, 4, 5); |
| 191 |
|
| 192 |
assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); |
| 193 |
} |
| 194 |
|
| 195 |
@Test |
| 196 |
public void asyncFusedRejectedConditional() { |
| 197 |
TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC); |
| 198 |
|
| 199 |
Flowable.range(1, 5) |
| 200 |
.doAfterNext(afterNext) |
| 201 |
.filter(Functions.alwaysTrue()) |
| 202 |
.subscribe(ts0); |
| 203 |
|
| 204 |
ts0.assertFusionMode(QueueFuseable.NONE) |
| 205 |
.assertResult(1, 2, 3, 4, 5); |
| 206 |
|
| 207 |
assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void asyncFusedConditional() { |
| 212 |
TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC); |
| 213 |
|
| 214 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 215 |
|
| 216 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 217 |
|
| 218 |
up |
| 219 |
.doAfterNext(afterNext) |
| 220 |
.filter(Functions.alwaysTrue()) |
| 221 |
.subscribe(ts0); |
| 222 |
|
| 223 |
ts0.assertFusionMode(QueueFuseable.ASYNC) |
| 224 |
.assertResult(1, 2, 3, 4, 5); |
| 225 |
|
| 226 |
assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); |
| 227 |
} |
| 228 |
|
| 229 |
@Test |
| 230 |
public void consumerThrows() { |
| 231 |
Flowable.just(1, 2) |
| 232 |
.doAfterNext(new Consumer<Integer>() { |
| 233 |
@Override |
| 234 |
public void accept(Integer e) throws Exception { |
| 235 |
throw new TestException(); |
| 236 |
} |
| 237 |
}) |
| 238 |
.test() |
| 239 |
.assertFailure(TestException.class, 1); |
| 240 |
} |
| 241 |
|
| 242 |
@Test |
| 243 |
public void consumerThrowsConditional() { |
| 244 |
Flowable.just(1, 2) |
| 245 |
.doAfterNext(new Consumer<Integer>() { |
| 246 |
@Override |
| 247 |
public void accept(Integer e) throws Exception { |
| 248 |
throw new TestException(); |
| 249 |
} |
| 250 |
}) |
| 251 |
.filter(Functions.alwaysTrue()) |
| 252 |
.test() |
| 253 |
.assertFailure(TestException.class, 1); |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void consumerThrowsConditional2() { |
| 258 |
Flowable.just(1, 2).hide() |
| 259 |
.doAfterNext(new Consumer<Integer>() { |
| 260 |
@Override |
| 261 |
public void accept(Integer e) throws Exception { |
| 262 |
throw new TestException(); |
| 263 |
} |
| 264 |
}) |
| 265 |
.filter(Functions.alwaysTrue()) |
| 266 |
.test() |
| 267 |
.assertFailure(TestException.class, 1); |
| 268 |
} |
| 269 |
} |