| 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.maybe; |
| 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.Function; |
| 25 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 26 |
import io.reactivex.rxjava3.internal.util.CrashingMappedIterable; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 30 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 31 |
|
| 32 |
public class MaybeZipIterableTest extends RxJavaTest { |
| 33 |
|
| 34 |
final Function<Object[], Object> addString = new Function<Object[], Object>() { |
| 35 |
@Override |
| 36 |
public Object apply(Object[] a) throws Exception { |
| 37 |
return Arrays.toString(a); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
@Test |
| 42 |
public void firstError() { |
| 43 |
Maybe.zip(Arrays.asList(Maybe.error(new TestException()), Maybe.just(1)), addString) |
| 44 |
.test() |
| 45 |
.assertFailure(TestException.class); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void secondError() { |
| 50 |
Maybe.zip(Arrays.asList(Maybe.just(1), Maybe.<Integer>error(new TestException())), addString) |
| 51 |
.test() |
| 52 |
.assertFailure(TestException.class); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void dispose() { |
| 57 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 58 |
|
| 59 |
TestObserver<Object> to = Maybe.zip(Arrays.asList(pp.singleElement(), pp.singleElement()), addString) |
| 60 |
.test(); |
| 61 |
|
| 62 |
assertTrue(pp.hasSubscribers()); |
| 63 |
|
| 64 |
to.dispose(); |
| 65 |
|
| 66 |
assertFalse(pp.hasSubscribers()); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void zipperThrows() { |
| 71 |
Maybe.zip(Arrays.asList(Maybe.just(1), Maybe.just(2)), new Function<Object[], Object>() { |
| 72 |
@Override |
| 73 |
public Object apply(Object[] b) throws Exception { |
| 74 |
throw new TestException(); |
| 75 |
} |
| 76 |
}) |
| 77 |
.test() |
| 78 |
.assertFailure(TestException.class); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void zipperReturnsNull() { |
| 83 |
Maybe.zip(Arrays.asList(Maybe.just(1), Maybe.just(2)), new Function<Object[], Object>() { |
| 84 |
@Override |
| 85 |
public Object apply(Object[] a) throws Exception { |
| 86 |
return null; |
| 87 |
} |
| 88 |
}) |
| 89 |
.test() |
| 90 |
.assertFailure(NullPointerException.class); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void middleError() { |
| 95 |
PublishProcessor<Integer> pp0 = PublishProcessor.create(); |
| 96 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 97 |
|
| 98 |
TestObserver<Object> to = Maybe.zip( |
| 99 |
Arrays.asList(pp0.singleElement(), pp1.singleElement(), pp0.singleElement()), addString) |
| 100 |
.test(); |
| 101 |
|
| 102 |
pp1.onError(new TestException()); |
| 103 |
|
| 104 |
assertFalse(pp0.hasSubscribers()); |
| 105 |
|
| 106 |
to.assertFailure(TestException.class); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void innerErrorRace() { |
| 111 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 112 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 113 |
try { |
| 114 |
final PublishProcessor<Integer> pp0 = PublishProcessor.create(); |
| 115 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 116 |
|
| 117 |
final TestObserver<Object> to = Maybe.zip( |
| 118 |
Arrays.asList(pp0.singleElement(), pp1.singleElement()), addString) |
| 119 |
.test(); |
| 120 |
|
| 121 |
final TestException ex = new TestException(); |
| 122 |
|
| 123 |
Runnable r1 = new Runnable() { |
| 124 |
@Override |
| 125 |
public void run() { |
| 126 |
pp0.onError(ex); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
Runnable r2 = new Runnable() { |
| 131 |
@Override |
| 132 |
public void run() { |
| 133 |
pp1.onError(ex); |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
TestHelper.race(r1, r2); |
| 138 |
|
| 139 |
to.assertFailure(TestException.class); |
| 140 |
|
| 141 |
if (!errors.isEmpty()) { |
| 142 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 143 |
} |
| 144 |
} finally { |
| 145 |
RxJavaPlugins.reset(); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void iteratorThrows() { |
| 152 |
Maybe.zip(new CrashingMappedIterable<>(1, 100, 100, new Function<Integer, Maybe<Integer>>() { |
| 153 |
@Override |
| 154 |
public Maybe<Integer> apply(Integer v) throws Exception { |
| 155 |
return Maybe.just(v); |
| 156 |
} |
| 157 |
}), addString) |
| 158 |
.to(TestHelper.<Object>testConsumer()) |
| 159 |
.assertFailureAndMessage(TestException.class, "iterator()"); |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void hasNextThrows() { |
| 164 |
Maybe.zip(new CrashingMappedIterable<>(100, 20, 100, new Function<Integer, Maybe<Integer>>() { |
| 165 |
@Override |
| 166 |
public Maybe<Integer> apply(Integer v) throws Exception { |
| 167 |
return Maybe.just(v); |
| 168 |
} |
| 169 |
}), addString) |
| 170 |
.to(TestHelper.<Object>testConsumer()) |
| 171 |
.assertFailureAndMessage(TestException.class, "hasNext()"); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void nextThrows() { |
| 176 |
Maybe.zip(new CrashingMappedIterable<>(100, 100, 5, new Function<Integer, Maybe<Integer>>() { |
| 177 |
@Override |
| 178 |
public Maybe<Integer> apply(Integer v) throws Exception { |
| 179 |
return Maybe.just(v); |
| 180 |
} |
| 181 |
}), addString) |
| 182 |
.to(TestHelper.<Object>testConsumer()) |
| 183 |
.assertFailureAndMessage(TestException.class, "next()"); |
| 184 |
} |
| 185 |
|
| 186 |
@Test(expected = NullPointerException.class) |
| 187 |
public void zipIterableOneIsNull() { |
| 188 |
Maybe.zip(Arrays.asList(null, Maybe.just(1)), new Function<Object[], Object>() { |
| 189 |
@Override |
| 190 |
public Object apply(Object[] v) { |
| 191 |
return 1; |
| 192 |
} |
| 193 |
}) |
| 194 |
.blockingGet(); |
| 195 |
} |
| 196 |
|
| 197 |
@Test(expected = NullPointerException.class) |
| 198 |
public void zipIterableTwoIsNull() { |
| 199 |
Maybe.zip(Arrays.asList(Maybe.just(1), null), new Function<Object[], Object>() { |
| 200 |
@Override |
| 201 |
public Object apply(Object[] v) { |
| 202 |
return 1; |
| 203 |
} |
| 204 |
}) |
| 205 |
.blockingGet(); |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void singleSourceZipperReturnsNull() { |
| 210 |
Maybe.zipArray(Functions.justFunction(null), Maybe.just(1)) |
| 211 |
.to(TestHelper.<Object>testConsumer()) |
| 212 |
.assertFailureAndMessage(NullPointerException.class, "The zipper returned a null value"); |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void maybeSourcesInIterable() { |
| 217 |
MaybeSource<Integer> source = new MaybeSource<Integer>() { |
| 218 |
@Override |
| 219 |
public void subscribe(MaybeObserver<? super Integer> observer) { |
| 220 |
Maybe.just(1).subscribe(observer); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
Maybe.zip(Arrays.asList(source, source), new Function<Object[], Integer>() { |
| 225 |
@Override |
| 226 |
public Integer apply(Object[] t) throws Throwable { |
| 227 |
return 2; |
| 228 |
} |
| 229 |
}) |
| 230 |
.test() |
| 231 |
.assertResult(2); |
| 232 |
} |
| 233 |
} |