| 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 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.disposables.SerialDisposable; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.internal.operators.flowable.BlockingFlowableNext.NextSubscriber; |
| 29 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.*; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 34 |
|
| 35 |
public class BlockingFlowableNextTest extends RxJavaTest { |
| 36 |
|
| 37 |
private void fireOnNextInNewThread(final FlowableProcessor<String> o, final String value) { |
| 38 |
new Thread() { |
| 39 |
@Override |
| 40 |
public void run() { |
| 41 |
try { |
| 42 |
Thread.sleep(500); |
| 43 |
} catch (InterruptedException e) { |
| 44 |
// ignore |
| 45 |
} |
| 46 |
o.onNext(value); |
| 47 |
} |
| 48 |
}.start(); |
| 49 |
} |
| 50 |
|
| 51 |
private void fireOnErrorInNewThread(final FlowableProcessor<String> o) { |
| 52 |
new Thread() { |
| 53 |
@Override |
| 54 |
public void run() { |
| 55 |
try { |
| 56 |
Thread.sleep(500); |
| 57 |
} catch (InterruptedException e) { |
| 58 |
// ignore |
| 59 |
} |
| 60 |
o.onError(new TestException()); |
| 61 |
} |
| 62 |
}.start(); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void next() { |
| 67 |
FlowableProcessor<String> obs = PublishProcessor.create(); |
| 68 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 69 |
fireOnNextInNewThread(obs, "one"); |
| 70 |
assertTrue(it.hasNext()); |
| 71 |
assertEquals("one", it.next()); |
| 72 |
|
| 73 |
fireOnNextInNewThread(obs, "two"); |
| 74 |
assertTrue(it.hasNext()); |
| 75 |
assertEquals("two", it.next()); |
| 76 |
|
| 77 |
fireOnNextInNewThread(obs, "three"); |
| 78 |
try { |
| 79 |
assertEquals("three", it.next()); |
| 80 |
} catch (NoSuchElementException e) { |
| 81 |
fail("Calling next() without hasNext() should wait for next fire"); |
| 82 |
} |
| 83 |
|
| 84 |
obs.onComplete(); |
| 85 |
assertFalse(it.hasNext()); |
| 86 |
try { |
| 87 |
it.next(); |
| 88 |
fail("At the end of an iterator should throw a NoSuchElementException"); |
| 89 |
} catch (NoSuchElementException e) { |
| 90 |
} |
| 91 |
|
| 92 |
// If the observable is completed, hasNext always returns false and next always throw a NoSuchElementException. |
| 93 |
assertFalse(it.hasNext()); |
| 94 |
try { |
| 95 |
it.next(); |
| 96 |
fail("At the end of an iterator should throw a NoSuchElementException"); |
| 97 |
} catch (NoSuchElementException e) { |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void nextWithError() { |
| 103 |
FlowableProcessor<String> obs = PublishProcessor.create(); |
| 104 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 105 |
fireOnNextInNewThread(obs, "one"); |
| 106 |
assertTrue(it.hasNext()); |
| 107 |
assertEquals("one", it.next()); |
| 108 |
|
| 109 |
fireOnErrorInNewThread(obs); |
| 110 |
try { |
| 111 |
it.hasNext(); |
| 112 |
fail("Expected an TestException"); |
| 113 |
} catch (TestException e) { |
| 114 |
} |
| 115 |
|
| 116 |
assertErrorAfterObservableFail(it); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void nextWithEmpty() { |
| 121 |
Flowable<String> obs = Flowable.<String> empty().observeOn(Schedulers.newThread()); |
| 122 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 123 |
|
| 124 |
assertFalse(it.hasNext()); |
| 125 |
try { |
| 126 |
it.next(); |
| 127 |
fail("At the end of an iterator should throw a NoSuchElementException"); |
| 128 |
} catch (NoSuchElementException e) { |
| 129 |
} |
| 130 |
|
| 131 |
// If the observable is completed, hasNext always returns false and next always throw a NoSuchElementException. |
| 132 |
assertFalse(it.hasNext()); |
| 133 |
try { |
| 134 |
it.next(); |
| 135 |
fail("At the end of an iterator should throw a NoSuchElementException"); |
| 136 |
} catch (NoSuchElementException e) { |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@Test |
| 141 |
public void onError() throws Throwable { |
| 142 |
FlowableProcessor<String> obs = PublishProcessor.create(); |
| 143 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 144 |
|
| 145 |
obs.onError(new TestException()); |
| 146 |
try { |
| 147 |
it.hasNext(); |
| 148 |
fail("Expected an TestException"); |
| 149 |
} catch (TestException e) { |
| 150 |
// successful |
| 151 |
} |
| 152 |
|
| 153 |
assertErrorAfterObservableFail(it); |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void onErrorInNewThread() { |
| 158 |
FlowableProcessor<String> obs = PublishProcessor.create(); |
| 159 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 160 |
|
| 161 |
fireOnErrorInNewThread(obs); |
| 162 |
|
| 163 |
try { |
| 164 |
it.hasNext(); |
| 165 |
fail("Expected an TestException"); |
| 166 |
} catch (TestException e) { |
| 167 |
// successful |
| 168 |
} |
| 169 |
|
| 170 |
assertErrorAfterObservableFail(it); |
| 171 |
} |
| 172 |
|
| 173 |
private void assertErrorAfterObservableFail(Iterator<String> it) { |
| 174 |
// After the observable fails, hasNext and next always throw the exception. |
| 175 |
try { |
| 176 |
it.hasNext(); |
| 177 |
fail("hasNext should throw a TestException"); |
| 178 |
} catch (TestException e) { |
| 179 |
} |
| 180 |
try { |
| 181 |
it.next(); |
| 182 |
fail("next should throw a TestException"); |
| 183 |
} catch (TestException e) { |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void nextWithOnlyUsingNextMethod() { |
| 189 |
FlowableProcessor<String> obs = PublishProcessor.create(); |
| 190 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 191 |
fireOnNextInNewThread(obs, "one"); |
| 192 |
assertEquals("one", it.next()); |
| 193 |
|
| 194 |
fireOnNextInNewThread(obs, "two"); |
| 195 |
assertEquals("two", it.next()); |
| 196 |
|
| 197 |
obs.onComplete(); |
| 198 |
try { |
| 199 |
it.next(); |
| 200 |
fail("At the end of an iterator should throw a NoSuchElementException"); |
| 201 |
} catch (NoSuchElementException e) { |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void nextWithCallingHasNextMultipleTimes() { |
| 207 |
FlowableProcessor<String> obs = PublishProcessor.create(); |
| 208 |
Iterator<String> it = obs.blockingNext().iterator(); |
| 209 |
fireOnNextInNewThread(obs, "one"); |
| 210 |
assertTrue(it.hasNext()); |
| 211 |
assertTrue(it.hasNext()); |
| 212 |
assertTrue(it.hasNext()); |
| 213 |
assertTrue(it.hasNext()); |
| 214 |
assertEquals("one", it.next()); |
| 215 |
|
| 216 |
obs.onComplete(); |
| 217 |
try { |
| 218 |
it.next(); |
| 219 |
fail("At the end of an iterator should throw a NoSuchElementException"); |
| 220 |
} catch (NoSuchElementException e) { |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Confirm that no buffering or blocking of the Observable onNext calls occurs and it just grabs the next emitted value. |
| 226 |
* <p> |
| 227 |
* This results in output such as {@code => a: 1 b: 2 c: 89} |
| 228 |
* |
| 229 |
* @throws Throwable some method call is declared throws |
| 230 |
*/ |
| 231 |
@Test |
| 232 |
public void noBufferingOrBlockingOfSequence() throws Throwable { |
| 233 |
int repeat = 0; |
| 234 |
for (;;) { |
| 235 |
final SerialDisposable task = new SerialDisposable(); |
| 236 |
try { |
| 237 |
final CountDownLatch finished = new CountDownLatch(1); |
| 238 |
final int COUNT = 30; |
| 239 |
final CountDownLatch timeHasPassed = new CountDownLatch(COUNT); |
| 240 |
final AtomicBoolean running = new AtomicBoolean(true); |
| 241 |
final AtomicInteger count = new AtomicInteger(0); |
| 242 |
final Flowable<Integer> obs = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 243 |
|
| 244 |
@Override |
| 245 |
public void subscribe(final Subscriber<? super Integer> subscriber) { |
| 246 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 247 |
task.replace(Schedulers.single().scheduleDirect(new Runnable() { |
| 248 |
|
| 249 |
@Override |
| 250 |
public void run() { |
| 251 |
try { |
| 252 |
while (running.get() && !task.isDisposed()) { |
| 253 |
subscriber.onNext(count.incrementAndGet()); |
| 254 |
timeHasPassed.countDown(); |
| 255 |
} |
| 256 |
subscriber.onComplete(); |
| 257 |
} catch (Throwable e) { |
| 258 |
subscriber.onError(e); |
| 259 |
} finally { |
| 260 |
finished.countDown(); |
| 261 |
} |
| 262 |
} |
| 263 |
})); |
| 264 |
} |
| 265 |
|
| 266 |
}); |
| 267 |
|
| 268 |
Iterator<Integer> it = obs.blockingNext().iterator(); |
| 269 |
|
| 270 |
assertTrue(it.hasNext()); |
| 271 |
int a = it.next(); |
| 272 |
assertTrue(it.hasNext()); |
| 273 |
int b = it.next(); |
| 274 |
// we should have a different value |
| 275 |
assertTrue("a and b should be different", a != b); |
| 276 |
|
| 277 |
// wait for some time (if times out we are blocked somewhere so fail ... set very high for very slow, constrained machines) |
| 278 |
timeHasPassed.await(8000, TimeUnit.MILLISECONDS); |
| 279 |
|
| 280 |
assertTrue(it.hasNext()); |
| 281 |
int c = it.next(); |
| 282 |
|
| 283 |
assertTrue("c should not just be the next in sequence", c != (b + 1)); |
| 284 |
assertTrue("expected that c [" + c + "] is higher than or equal to " + COUNT, c >= COUNT); |
| 285 |
|
| 286 |
assertTrue(it.hasNext()); |
| 287 |
int d = it.next(); |
| 288 |
assertTrue(d > c); |
| 289 |
|
| 290 |
// shut down the thread |
| 291 |
running.set(false); |
| 292 |
|
| 293 |
finished.await(); |
| 294 |
|
| 295 |
assertFalse(it.hasNext()); |
| 296 |
|
| 297 |
System.out.println("a: " + a + " b: " + b + " c: " + c); |
| 298 |
break; |
| 299 |
} catch (AssertionError ex) { |
| 300 |
if (++repeat == 3) { |
| 301 |
throw ex; |
| 302 |
} |
| 303 |
Thread.sleep((int)(1000 * Math.pow(2, repeat - 1))); |
| 304 |
} finally { |
| 305 |
task.dispose(); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void singleSourceManyIterators() throws InterruptedException { |
| 312 |
Flowable<Long> f = Flowable.interval(250, TimeUnit.MILLISECONDS); |
| 313 |
PublishProcessor<Integer> terminal = PublishProcessor.create(); |
| 314 |
Flowable<Long> source = f.takeUntil(terminal); |
| 315 |
|
| 316 |
Iterable<Long> iter = source.blockingNext(); |
| 317 |
|
| 318 |
for (int j = 0; j < 3; j++) { |
| 319 |
BlockingFlowableNext.NextIterator<Long> it = (BlockingFlowableNext.NextIterator<Long>)iter.iterator(); |
| 320 |
|
| 321 |
for (long i = 0; i < 10; i++) { |
| 322 |
Assert.assertTrue(it.hasNext()); |
| 323 |
Assert.assertEquals(j + "th iteration next", Long.valueOf(i), it.next()); |
| 324 |
} |
| 325 |
terminal.onNext(1); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
@Test |
| 330 |
public void synchronousNext() { |
| 331 |
assertEquals(1, BehaviorProcessor.createDefault(1).take(1).blockingSingle().intValue()); |
| 332 |
assertEquals(2, BehaviorProcessor.createDefault(2).blockingIterable().iterator().next().intValue()); |
| 333 |
assertEquals(3, BehaviorProcessor.createDefault(3).blockingNext().iterator().next().intValue()); |
| 334 |
} |
| 335 |
|
| 336 |
@Test(expected = UnsupportedOperationException.class) |
| 337 |
public void remove() { |
| 338 |
Flowable.never().blockingNext().iterator().remove(); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void interrupt() { |
| 343 |
Iterator<Object> it = Flowable.never().blockingNext().iterator(); |
| 344 |
|
| 345 |
try { |
| 346 |
Thread.currentThread().interrupt(); |
| 347 |
it.next(); |
| 348 |
} catch (RuntimeException ex) { |
| 349 |
assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
@Test |
| 354 |
public void nextObserverError() { |
| 355 |
NextSubscriber<Integer> no = new NextSubscriber<>(); |
| 356 |
|
| 357 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 358 |
try { |
| 359 |
no.onError(new TestException()); |
| 360 |
|
| 361 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 362 |
} finally { |
| 363 |
RxJavaPlugins.reset(); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
@Test |
| 368 |
public void nextObserverOnNext() throws Exception { |
| 369 |
NextSubscriber<Integer> no = new NextSubscriber<>(); |
| 370 |
|
| 371 |
no.setWaiting(); |
| 372 |
no.onNext(Notification.createOnNext(1)); |
| 373 |
|
| 374 |
no.setWaiting(); |
| 375 |
no.onNext(Notification.createOnNext(1)); |
| 376 |
|
| 377 |
assertEquals(1, no.takeNext().getValue().intValue()); |
| 378 |
} |
| 379 |
|
| 380 |
@Test |
| 381 |
public void nextObserverOnCompleteOnNext() throws Exception { |
| 382 |
NextSubscriber<Integer> no = new NextSubscriber<>(); |
| 383 |
|
| 384 |
no.setWaiting(); |
| 385 |
no.onNext(Notification.<Integer>createOnComplete()); |
| 386 |
|
| 387 |
no.setWaiting(); |
| 388 |
no.onNext(Notification.createOnNext(1)); |
| 389 |
|
| 390 |
assertTrue(no.takeNext().isOnComplete()); |
| 391 |
} |
| 392 |
|
| 393 |
} |