| 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.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.Subscriber; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.Function; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 29 |
import io.reactivex.rxjava3.testsupport.*; |
| 30 |
|
| 31 |
public class MaybeDelayOtherTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void justWithOnNext() { |
| 35 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 36 |
|
| 37 |
TestObserver<Integer> to = Maybe.just(1) |
| 38 |
.delay(pp).test(); |
| 39 |
|
| 40 |
to.assertEmpty(); |
| 41 |
|
| 42 |
assertTrue(pp.hasSubscribers()); |
| 43 |
|
| 44 |
pp.onNext(1); |
| 45 |
|
| 46 |
assertFalse(pp.hasSubscribers()); |
| 47 |
|
| 48 |
to.assertResult(1); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void justWithOnComplete() { |
| 53 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 54 |
|
| 55 |
TestObserver<Integer> to = Maybe.just(1) |
| 56 |
.delay(pp).test(); |
| 57 |
|
| 58 |
to.assertEmpty(); |
| 59 |
|
| 60 |
assertTrue(pp.hasSubscribers()); |
| 61 |
|
| 62 |
pp.onComplete(); |
| 63 |
|
| 64 |
assertFalse(pp.hasSubscribers()); |
| 65 |
|
| 66 |
to.assertResult(1); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void justWithOnError() { |
| 71 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 72 |
|
| 73 |
TestObserverEx<Integer> to = Maybe.just(1) |
| 74 |
.delay(pp).to(TestHelper.<Integer>testConsumer()); |
| 75 |
|
| 76 |
to.assertEmpty(); |
| 77 |
|
| 78 |
assertTrue(pp.hasSubscribers()); |
| 79 |
|
| 80 |
pp.onError(new TestException("Other")); |
| 81 |
|
| 82 |
assertFalse(pp.hasSubscribers()); |
| 83 |
|
| 84 |
to.assertFailureAndMessage(TestException.class, "Other"); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void emptyWithOnNext() { |
| 89 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 90 |
|
| 91 |
TestObserver<Integer> to = Maybe.<Integer>empty() |
| 92 |
.delay(pp).test(); |
| 93 |
|
| 94 |
to.assertEmpty(); |
| 95 |
|
| 96 |
assertTrue(pp.hasSubscribers()); |
| 97 |
|
| 98 |
pp.onNext(1); |
| 99 |
|
| 100 |
assertFalse(pp.hasSubscribers()); |
| 101 |
|
| 102 |
to.assertResult(); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void emptyWithOnComplete() { |
| 107 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 108 |
|
| 109 |
TestObserver<Integer> to = Maybe.<Integer>empty() |
| 110 |
.delay(pp).test(); |
| 111 |
|
| 112 |
to.assertEmpty(); |
| 113 |
|
| 114 |
assertTrue(pp.hasSubscribers()); |
| 115 |
|
| 116 |
pp.onComplete(); |
| 117 |
|
| 118 |
assertFalse(pp.hasSubscribers()); |
| 119 |
|
| 120 |
to.assertResult(); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void emptyWithOnError() { |
| 125 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 126 |
|
| 127 |
TestObserverEx<Integer> to = Maybe.<Integer>empty() |
| 128 |
.delay(pp).to(TestHelper.<Integer>testConsumer()); |
| 129 |
|
| 130 |
to.assertEmpty(); |
| 131 |
|
| 132 |
assertTrue(pp.hasSubscribers()); |
| 133 |
|
| 134 |
pp.onError(new TestException("Other")); |
| 135 |
|
| 136 |
assertFalse(pp.hasSubscribers()); |
| 137 |
|
| 138 |
to.assertFailureAndMessage(TestException.class, "Other"); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void errorWithOnNext() { |
| 143 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 144 |
|
| 145 |
TestObserverEx<Integer> to = Maybe.<Integer>error(new TestException("Main")) |
| 146 |
.delay(pp).to(TestHelper.<Integer>testConsumer()); |
| 147 |
|
| 148 |
to.assertEmpty(); |
| 149 |
|
| 150 |
assertTrue(pp.hasSubscribers()); |
| 151 |
|
| 152 |
pp.onNext(1); |
| 153 |
|
| 154 |
assertFalse(pp.hasSubscribers()); |
| 155 |
|
| 156 |
to.assertFailureAndMessage(TestException.class, "Main"); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void errorWithOnComplete() { |
| 161 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 162 |
|
| 163 |
TestObserverEx<Integer> to = Maybe.<Integer>error(new TestException("Main")) |
| 164 |
.delay(pp).to(TestHelper.<Integer>testConsumer()); |
| 165 |
|
| 166 |
to.assertEmpty(); |
| 167 |
|
| 168 |
assertTrue(pp.hasSubscribers()); |
| 169 |
|
| 170 |
pp.onComplete(); |
| 171 |
|
| 172 |
assertFalse(pp.hasSubscribers()); |
| 173 |
|
| 174 |
to.assertFailureAndMessage(TestException.class, "Main"); |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void errorWithOnError() { |
| 179 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 180 |
|
| 181 |
TestObserverEx<Integer> to = Maybe.<Integer>error(new TestException("Main")) |
| 182 |
.delay(pp).to(TestHelper.<Integer>testConsumer()); |
| 183 |
|
| 184 |
to.assertEmpty(); |
| 185 |
|
| 186 |
assertTrue(pp.hasSubscribers()); |
| 187 |
|
| 188 |
pp.onError(new TestException("Other")); |
| 189 |
|
| 190 |
assertFalse(pp.hasSubscribers()); |
| 191 |
|
| 192 |
to.assertFailure(CompositeException.class); |
| 193 |
|
| 194 |
List<Throwable> list = TestHelper.compositeList(to.errors().get(0)); |
| 195 |
assertEquals(2, list.size()); |
| 196 |
|
| 197 |
TestHelper.assertError(list, 0, TestException.class, "Main"); |
| 198 |
TestHelper.assertError(list, 1, TestException.class, "Other"); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void withCompletableDispose() { |
| 203 |
TestHelper.checkDisposed(Completable.complete().andThen(Maybe.just(1))); |
| 204 |
} |
| 205 |
|
| 206 |
@Test |
| 207 |
public void withCompletableDoubleOnSubscribe() { |
| 208 |
TestHelper.checkDoubleOnSubscribeCompletableToMaybe(new Function<Completable, MaybeSource<Integer>>() { |
| 209 |
@Override |
| 210 |
public MaybeSource<Integer> apply(Completable c) throws Exception { |
| 211 |
return c.andThen(Maybe.just(1)); |
| 212 |
} |
| 213 |
}); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void withOtherPublisherDispose() { |
| 218 |
TestHelper.checkDisposed(Maybe.just(1).delay(Flowable.just(1))); |
| 219 |
} |
| 220 |
|
| 221 |
@Test |
| 222 |
public void withOtherPublisherDoubleOnSubscribe() { |
| 223 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, MaybeSource<Integer>>() { |
| 224 |
@Override |
| 225 |
public MaybeSource<Integer> apply(Maybe<Integer> c) throws Exception { |
| 226 |
return c.delay(Flowable.never()); |
| 227 |
} |
| 228 |
}); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void otherPublisherNextSlipsThrough() { |
| 233 |
Maybe.just(1).delay(new Flowable<Integer>() { |
| 234 |
@Override |
| 235 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 236 |
s.onSubscribe(new BooleanSubscription()); |
| 237 |
s.onNext(1); |
| 238 |
s.onNext(2); |
| 239 |
} |
| 240 |
}) |
| 241 |
.test() |
| 242 |
.assertResult(1); |
| 243 |
} |
| 244 |
} |