| 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.mockito.ArgumentMatchers.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 30 |
import io.reactivex.rxjava3.testsupport.*; |
| 31 |
; |
| 32 |
|
| 33 |
public class ObservableTakeUntilPredicateTest extends RxJavaTest { |
| 34 |
@Test |
| 35 |
public void takeEmpty() { |
| 36 |
Observer<Object> o = TestHelper.mockObserver(); |
| 37 |
|
| 38 |
Observable.empty().takeUntil(new Predicate<Object>() { |
| 39 |
@Override |
| 40 |
public boolean test(Object v) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
}).subscribe(o); |
| 44 |
|
| 45 |
verify(o, never()).onNext(any()); |
| 46 |
verify(o, never()).onError(any(Throwable.class)); |
| 47 |
verify(o).onComplete(); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void takeAll() { |
| 52 |
Observer<Object> o = TestHelper.mockObserver(); |
| 53 |
|
| 54 |
Observable.just(1, 2).takeUntil(new Predicate<Integer>() { |
| 55 |
@Override |
| 56 |
public boolean test(Integer v) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
}).subscribe(o); |
| 60 |
|
| 61 |
verify(o).onNext(1); |
| 62 |
verify(o).onNext(2); |
| 63 |
verify(o, never()).onError(any(Throwable.class)); |
| 64 |
verify(o).onComplete(); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void takeFirst() { |
| 69 |
Observer<Object> o = TestHelper.mockObserver(); |
| 70 |
|
| 71 |
Observable.just(1, 2).takeUntil(new Predicate<Integer>() { |
| 72 |
@Override |
| 73 |
public boolean test(Integer v) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
}).subscribe(o); |
| 77 |
|
| 78 |
verify(o).onNext(1); |
| 79 |
verify(o, never()).onNext(2); |
| 80 |
verify(o, never()).onError(any(Throwable.class)); |
| 81 |
verify(o).onComplete(); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void takeSome() { |
| 86 |
Observer<Object> o = TestHelper.mockObserver(); |
| 87 |
|
| 88 |
Observable.just(1, 2, 3).takeUntil(new Predicate<Integer>() { |
| 89 |
@Override |
| 90 |
public boolean test(Integer t1) { |
| 91 |
return t1 == 2; |
| 92 |
} |
| 93 |
}) |
| 94 |
.subscribe(o); |
| 95 |
|
| 96 |
verify(o).onNext(1); |
| 97 |
verify(o).onNext(2); |
| 98 |
verify(o, never()).onNext(3); |
| 99 |
verify(o, never()).onError(any(Throwable.class)); |
| 100 |
verify(o).onComplete(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void functionThrows() { |
| 105 |
Observer<Object> o = TestHelper.mockObserver(); |
| 106 |
|
| 107 |
Predicate<Integer> predicate = (new Predicate<Integer>() { |
| 108 |
@Override |
| 109 |
public boolean test(Integer t1) { |
| 110 |
throw new TestException("Forced failure"); |
| 111 |
} |
| 112 |
}); |
| 113 |
Observable.just(1, 2, 3).takeUntil(predicate).subscribe(o); |
| 114 |
|
| 115 |
verify(o).onNext(1); |
| 116 |
verify(o, never()).onNext(2); |
| 117 |
verify(o, never()).onNext(3); |
| 118 |
verify(o).onError(any(TestException.class)); |
| 119 |
verify(o, never()).onComplete(); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void sourceThrows() { |
| 124 |
Observer<Object> o = TestHelper.mockObserver(); |
| 125 |
|
| 126 |
Observable.just(1) |
| 127 |
.concatWith(Observable.<Integer>error(new TestException())) |
| 128 |
.concatWith(Observable.just(2)) |
| 129 |
.takeUntil(new Predicate<Integer>() { |
| 130 |
@Override |
| 131 |
public boolean test(Integer v) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
}).subscribe(o); |
| 135 |
|
| 136 |
verify(o).onNext(1); |
| 137 |
verify(o, never()).onNext(2); |
| 138 |
verify(o).onError(any(TestException.class)); |
| 139 |
verify(o, never()).onComplete(); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void errorIncludesLastValueAsCause() { |
| 144 |
TestObserverEx<String> to = new TestObserverEx<>(); |
| 145 |
final TestException e = new TestException("Forced failure"); |
| 146 |
Predicate<String> predicate = (new Predicate<String>() { |
| 147 |
@Override |
| 148 |
public boolean test(String t) { |
| 149 |
throw e; |
| 150 |
} |
| 151 |
}); |
| 152 |
Observable.just("abc").takeUntil(predicate).subscribe(to); |
| 153 |
|
| 154 |
to.assertTerminated(); |
| 155 |
to.assertNotComplete(); |
| 156 |
to.assertError(TestException.class); |
| 157 |
// FIXME last cause value is not saved |
| 158 |
// assertTrue(ts.errors().get(0).getCause().getMessage().contains("abc")); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void dispose() { |
| 163 |
TestHelper.checkDisposed(PublishSubject.create().takeUntil(Functions.alwaysFalse())); |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void doubleOnSubscribe() { |
| 168 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 169 |
@Override |
| 170 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 171 |
return o.takeUntil(Functions.alwaysFalse()); |
| 172 |
} |
| 173 |
}); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void badSource() { |
| 178 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 179 |
try { |
| 180 |
new Observable<Integer>() { |
| 181 |
@Override |
| 182 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 183 |
observer.onSubscribe(Disposable.empty()); |
| 184 |
observer.onComplete(); |
| 185 |
observer.onNext(1); |
| 186 |
observer.onError(new TestException()); |
| 187 |
observer.onComplete(); |
| 188 |
} |
| 189 |
} |
| 190 |
.takeUntil(Functions.alwaysFalse()) |
| 191 |
.test() |
| 192 |
.assertResult(); |
| 193 |
|
| 194 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 195 |
} finally { |
| 196 |
RxJavaPlugins.reset(); |
| 197 |
} |
| 198 |
} |
| 199 |
} |