Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observable/ObservableWindowTests.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.observable;
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.core.Observable;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.schedulers.*;
28 import io.reactivex.rxjava3.subjects.PublishSubject;
29
30 public class ObservableWindowTests extends RxJavaTest {
31
32 @Test
33 public void window() {
34 final ArrayList<List<Integer>> lists = new ArrayList<>();
35
36 Observable.concat(
37 Observable.just(1, 2, 3, 4, 5, 6)
38 .window(3)
39 .map(new Function<Observable<Integer>, Observable<List<Integer>>>() {
40 @Override
41 public Observable<List<Integer>> apply(Observable<Integer> xs) {
42 return xs.toList().toObservable();
43 }
44 })
45 )
46 .blockingForEach(new Consumer<List<Integer>>() {
47 @Override
48 public void accept(List<Integer> xs) {
49 lists.add(xs);
50 }
51 });
52
53 assertArrayEquals(lists.get(0).toArray(new Integer[3]), new Integer[] { 1, 2, 3 });
54 assertArrayEquals(lists.get(1).toArray(new Integer[3]), new Integer[] { 4, 5, 6 });
55 assertEquals(2, lists.size());
56
57 }
58
59 @Test
60 public void timeSizeWindowAlternatingBounds() {
61 TestScheduler scheduler = new TestScheduler();
62 PublishSubject<Integer> ps = PublishSubject.create();
63
64 TestObserver<List<Integer>> to = ps.window(5, TimeUnit.SECONDS, scheduler, 2)
65 .flatMapSingle(new Function<Observable<Integer>, SingleSource<List<Integer>>>() {
66 @Override
67 public SingleSource<List<Integer>> apply(Observable<Integer> v) throws Throwable {
68 return v.toList();
69 }
70 })
71 .test();
72
73 ps.onNext(1);
74 ps.onNext(2);
75 to.assertValueCount(1); // size bound hit
76
77 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
78 ps.onNext(3);
79 scheduler.advanceTimeBy(6, TimeUnit.SECONDS);
80 to.assertValueCount(2); // time bound hit
81
82 ps.onNext(4);
83 ps.onNext(5);
84
85 to.assertValueCount(3); // size bound hit again
86
87 ps.onNext(4);
88
89 scheduler.advanceTimeBy(6, TimeUnit.SECONDS);
90
91 to.assertValueCount(4)
92 .assertNoErrors()
93 .assertNotComplete();
94
95 to.dispose();
96 }
97 }