| 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 static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.testsupport.*; |
| 26 |
|
| 27 |
public class ObservableTakeLastOneTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void lastOfManyReturnsLast() { |
| 31 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 32 |
Observable.range(1, 10).takeLast(1).subscribe(to); |
| 33 |
to.assertValue(10); |
| 34 |
to.assertNoErrors(); |
| 35 |
to.assertTerminated(); |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void lastOfEmptyReturnsEmpty() { |
| 40 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 41 |
Observable.empty().takeLast(1).subscribe(to); |
| 42 |
to.assertNoValues(); |
| 43 |
to.assertNoErrors(); |
| 44 |
to.assertTerminated(); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void lastOfOneReturnsLast() { |
| 49 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 50 |
Observable.just(1).takeLast(1).subscribe(to); |
| 51 |
to.assertValue(1); |
| 52 |
to.assertNoErrors(); |
| 53 |
to.assertTerminated(); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void unsubscribesFromUpstream() { |
| 58 |
final AtomicBoolean unsubscribed = new AtomicBoolean(false); |
| 59 |
Action unsubscribeAction = new Action() { |
| 60 |
@Override |
| 61 |
public void run() { |
| 62 |
unsubscribed.set(true); |
| 63 |
} |
| 64 |
}; |
| 65 |
Observable.just(1) |
| 66 |
.concatWith(Observable.<Integer>never()) |
| 67 |
.doOnDispose(unsubscribeAction) |
| 68 |
.takeLast(1) |
| 69 |
.subscribe() |
| 70 |
.dispose(); |
| 71 |
|
| 72 |
assertTrue(unsubscribed.get()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void takeLastZeroProcessesAllItemsButIgnoresThem() { |
| 77 |
final AtomicInteger upstreamCount = new AtomicInteger(); |
| 78 |
final int num = 10; |
| 79 |
long count = Observable.range(1, num).doOnNext(new Consumer<Integer>() { |
| 80 |
|
| 81 |
@Override |
| 82 |
public void accept(Integer t) { |
| 83 |
upstreamCount.incrementAndGet(); |
| 84 |
}}) |
| 85 |
.takeLast(0).count().blockingGet(); |
| 86 |
assertEquals(num, upstreamCount.get()); |
| 87 |
assertEquals(0L, count); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void dispose() { |
| 92 |
TestHelper.checkDisposed(Observable.just(1).takeLast(1)); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void doubleOnSubscribe() { |
| 97 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 98 |
@Override |
| 99 |
public ObservableSource<Object> apply(Observable<Object> f) throws Exception { |
| 100 |
return f.takeLast(1); |
| 101 |
} |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void error() { |
| 107 |
Observable.error(new TestException()) |
| 108 |
.takeLast(1) |
| 109 |
.test() |
| 110 |
.assertFailure(TestException.class); |
| 111 |
} |
| 112 |
} |