Debian Codegraph @

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