Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSkipLastTest.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 static org.junit.Assert.assertEquals;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.Arrays;
21 import java.util.concurrent.TimeUnit;
22
23 import org.junit.Test;
24 import org.mockito.InOrder;
25 import org.reactivestreams.Subscriber;
26
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.exceptions.TestException;
29 import io.reactivex.rxjava3.functions.Function;
30 import io.reactivex.rxjava3.schedulers.Schedulers;
31 import io.reactivex.rxjava3.subscribers.TestSubscriber;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class FlowableSkipLastTest extends RxJavaTest {
35
36 @Test
37 public void skipLastEmpty() {
38 Flowable<String> flowable = Flowable.<String> empty().skipLast(2);
39
40 Subscriber<String> subscriber = TestHelper.mockSubscriber();
41 flowable.subscribe(subscriber);
42
43 verify(subscriber, never()).onNext(any(String.class));
44 verify(subscriber, never()).onError(any(Throwable.class));
45 verify(subscriber, times(1)).onComplete();
46 }
47
48 @Test
49 public void skipLast1() {
50 Flowable<String> flowable = Flowable.fromIterable(Arrays.asList("one", "two", "three")).skipLast(2);
51
52 Subscriber<String> subscriber = TestHelper.mockSubscriber();
53 InOrder inOrder = inOrder(subscriber);
54 flowable.subscribe(subscriber);
55
56 inOrder.verify(subscriber, never()).onNext("two");
57 inOrder.verify(subscriber, never()).onNext("three");
58 verify(subscriber, times(1)).onNext("one");
59 verify(subscriber, never()).onError(any(Throwable.class));
60 verify(subscriber, times(1)).onComplete();
61 }
62
63 @Test
64 public void skipLast2() {
65 Flowable<String> flowable = Flowable.fromIterable(Arrays.asList("one", "two")).skipLast(2);
66
67 Subscriber<String> subscriber = TestHelper.mockSubscriber();
68 flowable.subscribe(subscriber);
69
70 verify(subscriber, never()).onNext(any(String.class));
71 verify(subscriber, never()).onError(any(Throwable.class));
72 verify(subscriber, times(1)).onComplete();
73 }
74
75 @Test
76 public void skipLastWithZeroCount() {
77 Flowable<String> w = Flowable.just("one", "two");
78 Flowable<String> flowable = w.skipLast(0);
79
80 Subscriber<String> subscriber = TestHelper.mockSubscriber();
81 flowable.subscribe(subscriber);
82
83 verify(subscriber, times(1)).onNext("one");
84 verify(subscriber, times(1)).onNext("two");
85 verify(subscriber, never()).onError(any(Throwable.class));
86 verify(subscriber, times(1)).onComplete();
87 }
88
89 @Test
90 public void skipLastWithBackpressure() {
91 Flowable<Integer> f = Flowable.range(0, Flowable.bufferSize() * 2).skipLast(Flowable.bufferSize() + 10);
92 TestSubscriber<Integer> ts = new TestSubscriber<>();
93 f.observeOn(Schedulers.computation()).subscribe(ts);
94 ts.awaitDone(5, TimeUnit.SECONDS);
95 ts.assertNoErrors();
96 assertEquals((Flowable.bufferSize()) - 10, ts.values().size());
97
98 }
99
100 @Test(expected = IllegalArgumentException.class)
101 public void skipLastWithNegativeCount() {
102 Flowable.just("one").skipLast(-1);
103 }
104
105 @Test
106 public void dispose() {
107 TestHelper.checkDisposed(Flowable.just(1).skipLast(1));
108 }
109
110 @Test
111 public void error() {
112 Flowable.error(new TestException())
113 .skipLast(1)
114 .test()
115 .assertFailure(TestException.class);
116 }
117
118 @Test
119 public void doubleOnSubscribe() {
120 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() {
121 @Override
122 public Flowable<Object> apply(Flowable<Object> f)
123 throws Exception {
124 return f.skipLast(1);
125 }
126 });
127 }
128
129 }