| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.core.Observer; |
| 25 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.Function; |
| 28 |
import io.reactivex.rxjava3.internal.util.CrashingIterable; |
| 29 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 30 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 31 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class SingleFlatMapIterableObservableTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void normal() { |
| 38 |
|
| 39 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 40 |
@Override |
| 41 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 42 |
return Arrays.asList(v, v + 1); |
| 43 |
} |
| 44 |
}) |
| 45 |
.test() |
| 46 |
.assertResult(1, 2); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void emptyIterable() { |
| 51 |
|
| 52 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 53 |
@Override |
| 54 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 55 |
return Collections.<Integer>emptyList(); |
| 56 |
} |
| 57 |
}) |
| 58 |
.test() |
| 59 |
.assertResult(); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void error() { |
| 64 |
|
| 65 |
Single.<Integer>error(new TestException()).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 66 |
@Override |
| 67 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 68 |
return Arrays.asList(v, v + 1); |
| 69 |
} |
| 70 |
}) |
| 71 |
.test() |
| 72 |
.assertFailure(TestException.class); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void take() { |
| 77 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 78 |
@Override |
| 79 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 80 |
return Arrays.asList(v, v + 1); |
| 81 |
} |
| 82 |
}) |
| 83 |
.take(1) |
| 84 |
.test() |
| 85 |
.assertResult(1); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void fused() { |
| 90 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY); |
| 91 |
|
| 92 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 93 |
@Override |
| 94 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 95 |
return Arrays.asList(v, v + 1); |
| 96 |
} |
| 97 |
}) |
| 98 |
.subscribe(to); |
| 99 |
|
| 100 |
to.assertFuseable() |
| 101 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 102 |
.assertResult(1, 2); |
| 103 |
; |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void fusedNoSync() { |
| 108 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC); |
| 109 |
|
| 110 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 111 |
@Override |
| 112 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 113 |
return Arrays.asList(v, v + 1); |
| 114 |
} |
| 115 |
}) |
| 116 |
.subscribe(to); |
| 117 |
|
| 118 |
to.assertFuseable() |
| 119 |
.assertFusionMode(QueueFuseable.NONE) |
| 120 |
.assertResult(1, 2); |
| 121 |
; |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void iteratorCrash() { |
| 126 |
|
| 127 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 128 |
@Override |
| 129 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 130 |
return new CrashingIterable(1, 100, 100); |
| 131 |
} |
| 132 |
}) |
| 133 |
.to(TestHelper.<Integer>testConsumer()) |
| 134 |
.assertFailureAndMessage(TestException.class, "iterator()"); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void hasNextCrash() { |
| 139 |
|
| 140 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 141 |
@Override |
| 142 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 143 |
return new CrashingIterable(100, 1, 100); |
| 144 |
} |
| 145 |
}) |
| 146 |
.to(TestHelper.<Integer>testConsumer()) |
| 147 |
.assertFailureAndMessage(TestException.class, "hasNext()"); |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void nextCrash() { |
| 152 |
|
| 153 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 154 |
@Override |
| 155 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 156 |
return new CrashingIterable(100, 100, 1); |
| 157 |
} |
| 158 |
}) |
| 159 |
.to(TestHelper.<Integer>testConsumer()) |
| 160 |
.assertFailureAndMessage(TestException.class, "next()"); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void hasNextCrash2() { |
| 165 |
|
| 166 |
Single.just(1).flattenAsObservable(new Function<Integer, Iterable<Integer>>() { |
| 167 |
@Override |
| 168 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 169 |
return new CrashingIterable(100, 2, 100); |
| 170 |
} |
| 171 |
}) |
| 172 |
.to(TestHelper.<Integer>testConsumer()) |
| 173 |
.assertFailureAndMessage(TestException.class, "hasNext()", 0); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void doubleOnSubscribe() { |
| 178 |
TestHelper.checkDoubleOnSubscribeSingleToObservable(new Function<Single<Object>, ObservableSource<Integer>>() { |
| 179 |
@Override |
| 180 |
public ObservableSource<Integer> apply(Single<Object> o) throws Exception { |
| 181 |
return o.flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 182 |
@Override |
| 183 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 184 |
return Collections.singleton(1); |
| 185 |
} |
| 186 |
}); |
| 187 |
} |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void dispose() { |
| 193 |
TestHelper.checkDisposed(Single.just(1).flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 194 |
@Override |
| 195 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 196 |
return Collections.singleton(1); |
| 197 |
} |
| 198 |
})); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void async1() { |
| 203 |
Single.just(1) |
| 204 |
.flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 205 |
@Override |
| 206 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 207 |
Integer[] array = new Integer[1000 * 1000]; |
| 208 |
Arrays.fill(array, 1); |
| 209 |
return Arrays.asList(array); |
| 210 |
} |
| 211 |
}) |
| 212 |
.hide() |
| 213 |
.observeOn(Schedulers.single()) |
| 214 |
.to(TestHelper.<Integer>testConsumer()) |
| 215 |
.awaitDone(5, TimeUnit.SECONDS) |
| 216 |
.assertSubscribed() |
| 217 |
.assertValueCount(1000 * 1000) |
| 218 |
.assertNoErrors() |
| 219 |
.assertComplete(); |
| 220 |
} |
| 221 |
|
| 222 |
@Test |
| 223 |
public void async2() { |
| 224 |
Single.just(1) |
| 225 |
.flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 226 |
@Override |
| 227 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 228 |
Integer[] array = new Integer[1000 * 1000]; |
| 229 |
Arrays.fill(array, 1); |
| 230 |
return Arrays.asList(array); |
| 231 |
} |
| 232 |
}) |
| 233 |
.observeOn(Schedulers.single()) |
| 234 |
.to(TestHelper.<Integer>testConsumer()) |
| 235 |
.awaitDone(5, TimeUnit.SECONDS) |
| 236 |
.assertSubscribed() |
| 237 |
.assertValueCount(1000 * 1000) |
| 238 |
.assertNoErrors() |
| 239 |
.assertComplete(); |
| 240 |
} |
| 241 |
|
| 242 |
@Test |
| 243 |
public void async3() { |
| 244 |
Single.just(1) |
| 245 |
.flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 246 |
@Override |
| 247 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 248 |
Integer[] array = new Integer[1000 * 1000]; |
| 249 |
Arrays.fill(array, 1); |
| 250 |
return Arrays.asList(array); |
| 251 |
} |
| 252 |
}) |
| 253 |
.take(500 * 1000) |
| 254 |
.observeOn(Schedulers.single()) |
| 255 |
.to(TestHelper.<Integer>testConsumer()) |
| 256 |
.awaitDone(5, TimeUnit.SECONDS) |
| 257 |
.assertSubscribed() |
| 258 |
.assertValueCount(500 * 1000) |
| 259 |
.assertNoErrors() |
| 260 |
.assertComplete(); |
| 261 |
} |
| 262 |
|
| 263 |
@Test |
| 264 |
public void async4() { |
| 265 |
Single.just(1) |
| 266 |
.flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 267 |
@Override |
| 268 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 269 |
Integer[] array = new Integer[1000 * 1000]; |
| 270 |
Arrays.fill(array, 1); |
| 271 |
return Arrays.asList(array); |
| 272 |
} |
| 273 |
}) |
| 274 |
.observeOn(Schedulers.single()) |
| 275 |
.take(500 * 1000) |
| 276 |
.to(TestHelper.<Integer>testConsumer()) |
| 277 |
.awaitDone(5, TimeUnit.SECONDS) |
| 278 |
.assertSubscribed() |
| 279 |
.assertValueCount(500 * 1000) |
| 280 |
.assertNoErrors() |
| 281 |
.assertComplete(); |
| 282 |
} |
| 283 |
|
| 284 |
@Test |
| 285 |
public void fusedEmptyCheck() { |
| 286 |
Single.just(1) |
| 287 |
.flattenAsObservable(new Function<Object, Iterable<Integer>>() { |
| 288 |
@Override |
| 289 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 290 |
return Arrays.asList(1, 2, 3); |
| 291 |
} |
| 292 |
}).subscribe(new Observer<Integer>() { |
| 293 |
QueueDisposable<Integer> qd; |
| 294 |
@SuppressWarnings("unchecked") |
| 295 |
@Override |
| 296 |
public void onSubscribe(Disposable d) { |
| 297 |
qd = (QueueDisposable<Integer>)d; |
| 298 |
|
| 299 |
assertEquals(QueueFuseable.ASYNC, qd.requestFusion(QueueFuseable.ANY)); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public void onNext(Integer value) { |
| 304 |
assertFalse(qd.isEmpty()); |
| 305 |
|
| 306 |
qd.clear(); |
| 307 |
|
| 308 |
assertTrue(qd.isEmpty()); |
| 309 |
|
| 310 |
qd.dispose(); |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public void onError(Throwable e) { |
| 315 |
} |
| 316 |
|
| 317 |
@Override |
| 318 |
public void onComplete() { |
| 319 |
} |
| 320 |
}); |
| 321 |
} |
| 322 |
} |