| 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.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.subscribers.DefaultSubscriber; |
| 27 |
import io.reactivex.rxjava3.testsupport.*; |
| 28 |
|
| 29 |
public class FlowableTakeLastOneTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void lastOfManyReturnsLast() { |
| 33 |
TestSubscriberEx<Integer> s = new TestSubscriberEx<>(); |
| 34 |
Flowable.range(1, 10).takeLast(1).subscribe(s); |
| 35 |
s.assertValue(10); |
| 36 |
s.assertNoErrors(); |
| 37 |
s.assertTerminated(); |
| 38 |
// NO longer assertable |
| 39 |
// s.assertUnsubscribed(); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void lastOfEmptyReturnsEmpty() { |
| 44 |
TestSubscriberEx<Object> s = new TestSubscriberEx<>(); |
| 45 |
Flowable.empty().takeLast(1).subscribe(s); |
| 46 |
s.assertNoValues(); |
| 47 |
s.assertNoErrors(); |
| 48 |
s.assertTerminated(); |
| 49 |
// NO longer assertable |
| 50 |
// s.assertUnsubscribed(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void lastOfOneReturnsLast() { |
| 55 |
TestSubscriberEx<Integer> s = new TestSubscriberEx<>(); |
| 56 |
Flowable.just(1).takeLast(1).subscribe(s); |
| 57 |
s.assertValue(1); |
| 58 |
s.assertNoErrors(); |
| 59 |
s.assertTerminated(); |
| 60 |
// NO longer assertable |
| 61 |
// s.assertUnsubscribed(); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void unsubscribesFromUpstream() { |
| 66 |
final AtomicBoolean unsubscribed = new AtomicBoolean(false); |
| 67 |
Action unsubscribeAction = new Action() { |
| 68 |
@Override |
| 69 |
public void run() { |
| 70 |
unsubscribed.set(true); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
Flowable.just(1).concatWith(Flowable.<Integer>never()) |
| 75 |
.doOnCancel(unsubscribeAction) |
| 76 |
.takeLast(1) |
| 77 |
.subscribe().dispose(); |
| 78 |
|
| 79 |
assertTrue(unsubscribed.get()); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void lastWithBackpressure() { |
| 84 |
MySubscriber<Integer> s = new MySubscriber<>(0); |
| 85 |
Flowable.just(1).takeLast(1).subscribe(s); |
| 86 |
assertEquals(0, s.list.size()); |
| 87 |
s.requestMore(1); |
| 88 |
assertEquals(1, s.list.size()); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void takeLastZeroProcessesAllItemsButIgnoresThem() { |
| 93 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 94 |
final int num = 10; |
| 95 |
long count = Flowable.range(1, num).doOnNext(new Consumer<Integer>() { |
| 96 |
|
| 97 |
@Override |
| 98 |
public void accept(Integer t) { |
| 99 |
upstreamCount.incrementAndGet(); |
| 100 |
}}) |
| 101 |
.takeLast(0).count().blockingGet(); |
| 102 |
assertEquals(num, upstreamCount.get()); |
| 103 |
assertEquals(0L, count); |
| 104 |
} |
| 105 |
|
| 106 |
private static class MySubscriber<T> extends DefaultSubscriber<T> { |
| 107 |
|
| 108 |
private long initialRequest; |
| 109 |
|
| 110 |
MySubscriber(long initialRequest) { |
| 111 |
this.initialRequest = initialRequest; |
| 112 |
} |
| 113 |
|
| 114 |
final List<T> list = new ArrayList<>(); |
| 115 |
|
| 116 |
public void requestMore(long n) { |
| 117 |
request(n); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void onStart() { |
| 122 |
if (initialRequest > 0) { |
| 123 |
request(initialRequest); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void onComplete() { |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void onError(Throwable e) { |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onNext(T t) { |
| 139 |
list.add(t); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void dispose() { |
| 146 |
TestHelper.checkDisposed(Flowable.just(1).takeLast(1)); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void doubleOnSubscribe() { |
| 151 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 152 |
@Override |
| 153 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 154 |
return f.takeLast(1); |
| 155 |
} |
| 156 |
}); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void error() { |
| 161 |
Flowable.error(new TestException()) |
| 162 |
.takeLast(1) |
| 163 |
.test() |
| 164 |
.assertFailure(TestException.class); |
| 165 |
} |
| 166 |
} |