| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.TimeUnit; |
| 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.Function; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 27 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class MaybeDelayTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void success() { |
| 34 |
Maybe.just(1).delay(100, TimeUnit.MILLISECONDS) |
| 35 |
.test() |
| 36 |
.awaitDone(5, TimeUnit.SECONDS) |
| 37 |
.assertResult(1); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void error() { |
| 42 |
Maybe.error(new TestException()).delay(100, TimeUnit.MILLISECONDS) |
| 43 |
.test() |
| 44 |
.awaitDone(5, TimeUnit.SECONDS) |
| 45 |
.assertFailure(TestException.class); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void complete() { |
| 50 |
Maybe.empty().delay(100, TimeUnit.MILLISECONDS) |
| 51 |
.test() |
| 52 |
.awaitDone(5, TimeUnit.SECONDS) |
| 53 |
.assertResult(); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void disposeDuringDelay() { |
| 58 |
TestScheduler scheduler = new TestScheduler(); |
| 59 |
|
| 60 |
TestObserver<Integer> to = Maybe.just(1).delay(100, TimeUnit.MILLISECONDS, scheduler) |
| 61 |
.test(); |
| 62 |
|
| 63 |
to.dispose(); |
| 64 |
|
| 65 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 66 |
|
| 67 |
to.assertEmpty(); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void dispose() { |
| 72 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 73 |
|
| 74 |
TestObserver<Integer> to = pp.singleElement().delay(100, TimeUnit.MILLISECONDS).test(); |
| 75 |
|
| 76 |
assertTrue(pp.hasSubscribers()); |
| 77 |
|
| 78 |
to.dispose(); |
| 79 |
|
| 80 |
assertFalse(pp.hasSubscribers()); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void isDisposed() { |
| 85 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 86 |
|
| 87 |
TestHelper.checkDisposed(pp.singleElement().delay(100, TimeUnit.MILLISECONDS)); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void doubleOnSubscribe() { |
| 92 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, Maybe<Object>>() { |
| 93 |
@Override |
| 94 |
public Maybe<Object> apply(Maybe<Object> f) throws Exception { |
| 95 |
return f.delay(100, TimeUnit.MILLISECONDS); |
| 96 |
} |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void delayedErrorOnSuccess() { |
| 102 |
final TestScheduler scheduler = new TestScheduler(); |
| 103 |
final TestObserver<Integer> observer = Maybe.just(1) |
| 104 |
.delay(5, TimeUnit.SECONDS, scheduler, true) |
| 105 |
.test(); |
| 106 |
|
| 107 |
scheduler.advanceTimeTo(2, TimeUnit.SECONDS); |
| 108 |
observer.assertNoValues(); |
| 109 |
|
| 110 |
scheduler.advanceTimeTo(5, TimeUnit.SECONDS); |
| 111 |
observer.assertValue(1); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void delayedErrorOnError() { |
| 116 |
final TestScheduler scheduler = new TestScheduler(); |
| 117 |
final TestObserver<?> observer = Maybe.error(new TestException()) |
| 118 |
.delay(5, TimeUnit.SECONDS, scheduler, true) |
| 119 |
.test(); |
| 120 |
|
| 121 |
scheduler.advanceTimeTo(2, TimeUnit.SECONDS); |
| 122 |
observer.assertNoErrors(); |
| 123 |
|
| 124 |
scheduler.advanceTimeTo(5, TimeUnit.SECONDS); |
| 125 |
observer.assertError(TestException.class); |
| 126 |
} |
| 127 |
} |