| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 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.schedulers.Schedulers; |
| 28 |
|
| 29 |
public class SingleMiscTest extends RxJavaTest { |
| 30 |
@Test |
| 31 |
public void never() { |
| 32 |
Single.never() |
| 33 |
.test() |
| 34 |
.assertNoValues() |
| 35 |
.assertNoErrors() |
| 36 |
.assertNotComplete(); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void timer() throws Exception { |
| 41 |
Single.timer(100, TimeUnit.MILLISECONDS) |
| 42 |
.test() |
| 43 |
.awaitDone(5, TimeUnit.SECONDS) |
| 44 |
.assertResult(0L); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void wrap() { |
| 49 |
assertSame(Single.never(), Single.wrap(Single.never())); |
| 50 |
|
| 51 |
Single.wrap(new SingleSource<Object>() { |
| 52 |
@Override |
| 53 |
public void subscribe(SingleObserver<? super Object> observer) { |
| 54 |
observer.onSubscribe(Disposable.empty()); |
| 55 |
observer.onSuccess(1); |
| 56 |
} |
| 57 |
}) |
| 58 |
.test() |
| 59 |
.assertResult(1); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void cast() { |
| 64 |
Single<Number> source = Single.just(1d) |
| 65 |
.cast(Number.class); |
| 66 |
source.test() |
| 67 |
.assertResult((Number)1d); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void contains() { |
| 72 |
Single.just(1).contains(1).test().assertResult(true); |
| 73 |
|
| 74 |
Single.just(2).contains(1).test().assertResult(false); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void compose() { |
| 79 |
|
| 80 |
Single.just(1) |
| 81 |
.compose(new SingleTransformer<Integer, Object>() { |
| 82 |
@Override |
| 83 |
public SingleSource<Object> apply(Single<Integer> f) { |
| 84 |
return f.map(new Function<Integer, Object>() { |
| 85 |
@Override |
| 86 |
public Object apply(Integer v) throws Exception { |
| 87 |
return v + 1; |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
}) |
| 92 |
.test() |
| 93 |
.assertResult(2); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void hide() { |
| 98 |
assertNotSame(Single.never(), Single.never().hide()); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void onErrorResumeWith() { |
| 103 |
Single.<Integer>error(new TestException()) |
| 104 |
.onErrorResumeWith(Single.just(1)) |
| 105 |
.test() |
| 106 |
.assertResult(1); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void onErrorReturnValue() { |
| 111 |
Single.<Integer>error(new TestException()) |
| 112 |
.onErrorReturnItem(1) |
| 113 |
.test() |
| 114 |
.assertResult(1); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void repeat() { |
| 119 |
Single.just(1).repeat().take(5) |
| 120 |
.test() |
| 121 |
.assertResult(1, 1, 1, 1, 1); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void repeatTimes() { |
| 126 |
Single.just(1).repeat(5) |
| 127 |
.test() |
| 128 |
.assertResult(1, 1, 1, 1, 1); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void repeatUntil() { |
| 133 |
final AtomicBoolean flag = new AtomicBoolean(); |
| 134 |
|
| 135 |
Single.just(1) |
| 136 |
.doOnSuccess(new Consumer<Integer>() { |
| 137 |
int c; |
| 138 |
@Override |
| 139 |
public void accept(Integer v) throws Exception { |
| 140 |
if (++c == 5) { |
| 141 |
flag.set(true); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
}) |
| 146 |
.repeatUntil(new BooleanSupplier() { |
| 147 |
@Override |
| 148 |
public boolean getAsBoolean() throws Exception { |
| 149 |
return flag.get(); |
| 150 |
} |
| 151 |
}) |
| 152 |
.test() |
| 153 |
.assertResult(1, 1, 1, 1, 1); |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void retry() { |
| 158 |
Single.fromCallable(new Callable<Object>() { |
| 159 |
int c; |
| 160 |
@Override |
| 161 |
public Object call() throws Exception { |
| 162 |
if (++c != 5) { |
| 163 |
throw new TestException(); |
| 164 |
} |
| 165 |
return 1; |
| 166 |
} |
| 167 |
}) |
| 168 |
.retry() |
| 169 |
.test() |
| 170 |
.assertResult(1); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void retryBiPredicate() { |
| 175 |
Single.fromCallable(new Callable<Object>() { |
| 176 |
int c; |
| 177 |
@Override |
| 178 |
public Object call() throws Exception { |
| 179 |
if (++c != 5) { |
| 180 |
throw new TestException(); |
| 181 |
} |
| 182 |
return 1; |
| 183 |
} |
| 184 |
}) |
| 185 |
.retry(new BiPredicate<Integer, Throwable>() { |
| 186 |
@Override |
| 187 |
public boolean test(Integer i, Throwable e) throws Exception { |
| 188 |
return true; |
| 189 |
} |
| 190 |
}) |
| 191 |
.test() |
| 192 |
.assertResult(1); |
| 193 |
} |
| 194 |
|
| 195 |
@Test |
| 196 |
public void retryTimes() { |
| 197 |
final AtomicInteger calls = new AtomicInteger(); |
| 198 |
|
| 199 |
Single.fromCallable(new Callable<Object>() { |
| 200 |
|
| 201 |
@Override |
| 202 |
public Object call() throws Exception { |
| 203 |
if (calls.incrementAndGet() != 6) { |
| 204 |
throw new TestException(); |
| 205 |
} |
| 206 |
return 1; |
| 207 |
} |
| 208 |
}) |
| 209 |
.retry(5) |
| 210 |
.test() |
| 211 |
.assertResult(1); |
| 212 |
|
| 213 |
assertEquals(6, calls.get()); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void retryPredicate() { |
| 218 |
Single.fromCallable(new Callable<Object>() { |
| 219 |
int c; |
| 220 |
@Override |
| 221 |
public Object call() throws Exception { |
| 222 |
if (++c != 5) { |
| 223 |
throw new TestException(); |
| 224 |
} |
| 225 |
return 1; |
| 226 |
} |
| 227 |
}) |
| 228 |
.retry(new Predicate<Throwable>() { |
| 229 |
@Override |
| 230 |
public boolean test(Throwable e) throws Exception { |
| 231 |
return true; |
| 232 |
} |
| 233 |
}) |
| 234 |
.test() |
| 235 |
.assertResult(1); |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void timeout() throws Exception { |
| 240 |
Single.never().timeout(100, TimeUnit.MILLISECONDS, Schedulers.io()) |
| 241 |
.test() |
| 242 |
.awaitDone(5, TimeUnit.SECONDS) |
| 243 |
.assertFailure(TimeoutException.class); |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void timeoutOther() throws Exception { |
| 248 |
Single.never() |
| 249 |
.timeout(100, TimeUnit.MILLISECONDS, Schedulers.io(), Single.just(1)) |
| 250 |
.test() |
| 251 |
.awaitDone(5, TimeUnit.SECONDS) |
| 252 |
.assertResult(1); |
| 253 |
} |
| 254 |
|
| 255 |
@Test |
| 256 |
public void ignoreElement() { |
| 257 |
Single.just(1) |
| 258 |
.ignoreElement() |
| 259 |
.test() |
| 260 |
.assertResult(); |
| 261 |
|
| 262 |
Single.error(new TestException()) |
| 263 |
.ignoreElement() |
| 264 |
.test() |
| 265 |
.assertFailure(TestException.class); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void toObservable() { |
| 270 |
Single.just(1) |
| 271 |
.toObservable() |
| 272 |
.test() |
| 273 |
.assertResult(1); |
| 274 |
|
| 275 |
Single.error(new TestException()) |
| 276 |
.toObservable() |
| 277 |
.test() |
| 278 |
.assertFailure(TestException.class); |
| 279 |
} |
| 280 |
|
| 281 |
@Test |
| 282 |
public void equals() { |
| 283 |
Single.sequenceEqual(Single.just(1), Single.just(1).hide()) |
| 284 |
.test() |
| 285 |
.assertResult(true); |
| 286 |
|
| 287 |
Single.sequenceEqual(Single.just(1), Single.just(2)) |
| 288 |
.test() |
| 289 |
.assertResult(false); |
| 290 |
} |
| 291 |
|
| 292 |
} |