| 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 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subjects.*; |
| 34 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 35 |
|
| 36 |
public class MaybeAmbTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void ambLots() { |
| 40 |
List<Maybe<Integer>> ms = new ArrayList<>(); |
| 41 |
|
| 42 |
for (int i = 0; i < 32; i++) { |
| 43 |
ms.add(Maybe.<Integer>never()); |
| 44 |
} |
| 45 |
|
| 46 |
ms.add(Maybe.just(1)); |
| 47 |
|
| 48 |
Maybe.amb(ms) |
| 49 |
.test() |
| 50 |
.assertResult(1); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void ambFirstDone() { |
| 55 |
Maybe.amb(Arrays.asList(Maybe.just(1), Maybe.just(2))) |
| 56 |
.test() |
| 57 |
.assertResult(1); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void dispose() { |
| 62 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 63 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 64 |
|
| 65 |
TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp1.singleElement(), pp2.singleElement())) |
| 66 |
.test(); |
| 67 |
|
| 68 |
assertTrue(pp1.hasSubscribers()); |
| 69 |
assertTrue(pp2.hasSubscribers()); |
| 70 |
|
| 71 |
to.dispose(); |
| 72 |
|
| 73 |
assertFalse(pp1.hasSubscribers()); |
| 74 |
assertFalse(pp2.hasSubscribers()); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void innerErrorRace() { |
| 79 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 80 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 81 |
try { |
| 82 |
final PublishProcessor<Integer> pp0 = PublishProcessor.create(); |
| 83 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 84 |
|
| 85 |
final TestObserver<Integer> to = Maybe.amb(Arrays.asList(pp0.singleElement(), pp1.singleElement())) |
| 86 |
.test(); |
| 87 |
|
| 88 |
final TestException ex = new TestException(); |
| 89 |
|
| 90 |
Runnable r1 = new Runnable() { |
| 91 |
@Override |
| 92 |
public void run() { |
| 93 |
pp0.onError(ex); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
Runnable r2 = new Runnable() { |
| 98 |
@Override |
| 99 |
public void run() { |
| 100 |
pp1.onError(ex); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
TestHelper.race(r1, r2); |
| 105 |
|
| 106 |
to.assertFailure(TestException.class); |
| 107 |
|
| 108 |
if (!errors.isEmpty()) { |
| 109 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 110 |
} |
| 111 |
} finally { |
| 112 |
RxJavaPlugins.reset(); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void disposeNoFurtherSignals() { |
| 119 |
TestObserver<Integer> to = Maybe.ambArray(new Maybe<Integer>() { |
| 120 |
@Override |
| 121 |
protected void subscribeActual( |
| 122 |
MaybeObserver<? super Integer> observer) { |
| 123 |
observer.onSubscribe(Disposable.empty()); |
| 124 |
observer.onSuccess(1); |
| 125 |
observer.onSuccess(2); |
| 126 |
observer.onComplete(); |
| 127 |
} |
| 128 |
}, Maybe.<Integer>never()) |
| 129 |
.test(); |
| 130 |
|
| 131 |
to.dispose(); |
| 132 |
|
| 133 |
to.assertResult(1); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void noWinnerSuccessDispose() throws Exception { |
| 138 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 139 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 140 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 141 |
|
| 142 |
Maybe.ambArray( |
| 143 |
Maybe.just(1) |
| 144 |
.subscribeOn(Schedulers.single()) |
| 145 |
.observeOn(Schedulers.computation()), |
| 146 |
Maybe.never() |
| 147 |
) |
| 148 |
.subscribe(new Consumer<Object>() { |
| 149 |
@Override |
| 150 |
public void accept(Object v) throws Exception { |
| 151 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 152 |
cdl.countDown(); |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 157 |
assertFalse("Interrupted!", interrupted.get()); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void noWinnerErrorDispose() throws Exception { |
| 163 |
final TestException ex = new TestException(); |
| 164 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 165 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 166 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 167 |
|
| 168 |
Maybe.ambArray( |
| 169 |
Maybe.error(ex) |
| 170 |
.subscribeOn(Schedulers.single()) |
| 171 |
.observeOn(Schedulers.computation()), |
| 172 |
Maybe.never() |
| 173 |
) |
| 174 |
.subscribe(Functions.emptyConsumer(), new Consumer<Throwable>() { |
| 175 |
@Override |
| 176 |
public void accept(Throwable e) throws Exception { |
| 177 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 178 |
cdl.countDown(); |
| 179 |
} |
| 180 |
}); |
| 181 |
|
| 182 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 183 |
assertFalse("Interrupted!", interrupted.get()); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void noWinnerCompleteDispose() throws Exception { |
| 189 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 190 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 191 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 192 |
|
| 193 |
Maybe.ambArray( |
| 194 |
Maybe.empty() |
| 195 |
.subscribeOn(Schedulers.single()) |
| 196 |
.observeOn(Schedulers.computation()), |
| 197 |
Maybe.never() |
| 198 |
) |
| 199 |
.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer(), new Action() { |
| 200 |
@Override |
| 201 |
public void run() throws Exception { |
| 202 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 203 |
cdl.countDown(); |
| 204 |
} |
| 205 |
}); |
| 206 |
|
| 207 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 208 |
assertFalse("Interrupted!", interrupted.get()); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void nullSourceSuccessRace() { |
| 214 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 215 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 216 |
|
| 217 |
try { |
| 218 |
|
| 219 |
final Subject<Integer> ps = ReplaySubject.create(); |
| 220 |
ps.onNext(1); |
| 221 |
|
| 222 |
final Maybe<Integer> source = Maybe.ambArray(ps.singleElement(), |
| 223 |
Maybe.<Integer>never(), Maybe.<Integer>never(), null); |
| 224 |
|
| 225 |
Runnable r1 = new Runnable() { |
| 226 |
@Override |
| 227 |
public void run() { |
| 228 |
source.test(); |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
Runnable r2 = new Runnable() { |
| 233 |
@Override |
| 234 |
public void run() { |
| 235 |
ps.onComplete(); |
| 236 |
} |
| 237 |
}; |
| 238 |
|
| 239 |
TestHelper.race(r1, r2); |
| 240 |
|
| 241 |
if (!errors.isEmpty()) { |
| 242 |
TestHelper.assertError(errors, 0, NullPointerException.class); |
| 243 |
} |
| 244 |
} finally { |
| 245 |
RxJavaPlugins.reset(); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void maybeSourcesInIterable() { |
| 252 |
MaybeSource<Integer> source = new MaybeSource<Integer>() { |
| 253 |
@Override |
| 254 |
public void subscribe(MaybeObserver<? super Integer> observer) { |
| 255 |
Maybe.just(1).subscribe(observer); |
| 256 |
} |
| 257 |
}; |
| 258 |
|
| 259 |
Maybe.amb(Arrays.asList(source, source)) |
| 260 |
.test() |
| 261 |
.assertResult(1); |
| 262 |
} |
| 263 |
} |