Debian Codegraph @

src/test/java/io/reactivex/rxjava3/flowable/FlowableWindowTests.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.flowable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
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.TestScheduler;
27 import io.reactivex.rxjava3.subscribers.TestSubscriber;
28
29 public class FlowableWindowTests extends RxJavaTest {
30
31 @Test
32 public void window() {
33 final ArrayList<List<Integer>> lists = new ArrayList<>();
34
35 Flowable.concat(
36 Flowable.just(1, 2, 3, 4, 5, 6)
37 .window(3)
38 .map(new Function<Flowable<Integer>, Flowable<List<Integer>>>() {
39 @Override
40 public Flowable<List<Integer>> apply(Flowable<Integer> xs) {
41 return xs.toList().toFlowable();
42 }
43 })
44 )
45 .blockingForEach(new Consumer<List<Integer>>() {
46 @Override
47 public void accept(List<Integer> xs) {
48 lists.add(xs);
49 }
50 });
51
52 assertArrayEquals(lists.get(0).toArray(new Integer[3]), new Integer[] { 1, 2, 3 });
53 assertArrayEquals(lists.get(1).toArray(new Integer[3]), new Integer[] { 4, 5, 6 });
54 assertEquals(2, lists.size());
55
56 }
57
58 @Test
59 public void timeSizeWindowAlternatingBounds() {
60 TestScheduler scheduler = new TestScheduler();
61 PublishProcessor<Integer> pp = PublishProcessor.create();
62
63 TestSubscriber<List<Integer>> ts = pp.window(5, TimeUnit.SECONDS, scheduler, 2)
64 .flatMapSingle(new Function<Flowable<Integer>, SingleSource<List<Integer>>>() {
65 @Override
66 public SingleSource<List<Integer>> apply(Flowable<Integer> v) throws Throwable {
67 return v.toList();
68 }
69 })
70 .test();
71
72 pp.onNext(1);
73 pp.onNext(2);
74 ts.assertValueCount(1); // size bound hit
75
76 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
77 pp.onNext(3);
78 scheduler.advanceTimeBy(6, TimeUnit.SECONDS);
79 ts.assertValueCount(2); // time bound hit
80
81 pp.onNext(4);
82 pp.onNext(5);
83
84 ts.assertValueCount(3); // size bound hit again
85
86 pp.onNext(4);
87
88 scheduler.advanceTimeBy(6, TimeUnit.SECONDS);
89
90 ts.assertValueCount(4)
91 .assertNoErrors()
92 .assertNotComplete();
93
94 ts.cancel();
95 }
96 }