| 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.junit.Assert.assertEquals; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.Observable; |
| 24 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class ObservableFlattenIterableTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void dispose() { |
| 34 |
TestHelper.checkDisposed(PublishSubject.create().flatMapIterable(new Function<Object, Iterable<Integer>>() { |
| 35 |
@Override |
| 36 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 37 |
return Arrays.asList(10, 20); |
| 38 |
} |
| 39 |
})); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void badSource() { |
| 44 |
TestHelper.checkBadSourceObservable(new Function<Observable<Integer>, Object>() { |
| 45 |
@Override |
| 46 |
public Object apply(Observable<Integer> o) throws Exception { |
| 47 |
return o.flatMapIterable(new Function<Object, Iterable<Integer>>() { |
| 48 |
@Override |
| 49 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 50 |
return Arrays.asList(10, 20); |
| 51 |
} |
| 52 |
}); |
| 53 |
} |
| 54 |
}, false, 1, 1, 10, 20); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void failingInnerCancelsSource() { |
| 59 |
final AtomicInteger counter = new AtomicInteger(); |
| 60 |
Observable.range(1, 5) |
| 61 |
.doOnNext(new Consumer<Integer>() { |
| 62 |
@Override |
| 63 |
public void accept(Integer v) throws Exception { |
| 64 |
counter.getAndIncrement(); |
| 65 |
} |
| 66 |
}) |
| 67 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 68 |
@Override |
| 69 |
public Iterable<Integer> apply(Integer v) |
| 70 |
throws Exception { |
| 71 |
return new Iterable<Integer>() { |
| 72 |
@Override |
| 73 |
public Iterator<Integer> iterator() { |
| 74 |
return new Iterator<Integer>() { |
| 75 |
@Override |
| 76 |
public boolean hasNext() { |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public Integer next() { |
| 82 |
throw new TestException(); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void remove() { |
| 87 |
throw new UnsupportedOperationException(); |
| 88 |
} |
| 89 |
}; |
| 90 |
} |
| 91 |
}; |
| 92 |
} |
| 93 |
}) |
| 94 |
.test() |
| 95 |
.assertFailure(TestException.class); |
| 96 |
|
| 97 |
assertEquals(1, counter.get()); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void doubleOnSubscribe() { |
| 102 |
TestHelper.checkDoubleOnSubscribeObservable(o -> o.flatMapIterable(v -> Collections.singletonList(v))); |
| 103 |
} |
| 104 |
} |