Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/BufferUntilSubscriberTest.java

raw
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 java.util.List;
17 import java.util.concurrent.*;
18 import java.util.concurrent.atomic.*;
19
20 import org.junit.*;
21 import org.reactivestreams.Publisher;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.processors.PublishProcessor;
26 import io.reactivex.rxjava3.schedulers.Schedulers;
27
28 public class BufferUntilSubscriberTest extends RxJavaTest {
29
30 @Test
31 public void issue1677() throws InterruptedException {
32 final AtomicLong counter = new AtomicLong();
33 final Integer[] numbers = new Integer[5000];
34 for (int i = 0; i < numbers.length; i++) {
35 numbers[i] = i + 1;
36 }
37 final int NITERS = 250;
38 final CountDownLatch latch = new CountDownLatch(NITERS);
39 for (int iters = 0; iters < NITERS; iters++) {
40 final CountDownLatch innerLatch = new CountDownLatch(1);
41 final PublishProcessor<Void> s = PublishProcessor.create();
42 final AtomicBoolean completed = new AtomicBoolean();
43 Flowable.fromArray(numbers)
44 .takeUntil(s)
45 .window(50)
46 .flatMap(new Function<Flowable<Integer>, Publisher<Object>>() {
47 @Override
48 public Publisher<Object> apply(Flowable<Integer> integerObservable) {
49 return integerObservable
50 .subscribeOn(Schedulers.computation())
51 .map(new Function<Integer, Object>() {
52 @Override
53 public Object apply(Integer integer) {
54 if (integer >= 5 && completed.compareAndSet(false, true)) {
55 s.onComplete();
56 }
57 // do some work
58 Math.pow(Math.random(), Math.random());
59 return integer * 2;
60 }
61 });
62 }
63 })
64 .toList()
65 .doOnSuccess(new Consumer<List<Object>>() {
66 @Override
67 public void accept(List<Object> integers) {
68 counter.incrementAndGet();
69 latch.countDown();
70 innerLatch.countDown();
71 }
72 })
73 .subscribe();
74 if (!innerLatch.await(30, TimeUnit.SECONDS)) {
75 Assert.fail("Failed inner latch wait, iteration " + iters);
76 }
77 }
78 if (!latch.await(30, TimeUnit.SECONDS)) {
79 Assert.fail("Incomplete! Went through " + latch.getCount() + " iterations");
80 } else {
81 Assert.assertEquals(NITERS, counter.get());
82 }
83 }
84 }