| 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 io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 30 |
import io.reactivex.rxjava3.testsupport.*; |
| 31 |
|
| 32 |
public class MaybePeekTest extends RxJavaTest { |
| 33 |
|
| 34 |
@Test |
| 35 |
public void disposed() { |
| 36 |
TestHelper.checkDisposed(PublishProcessor.create().singleElement().doOnSuccess(Functions.emptyConsumer())); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void doubleOnSubscribe() { |
| 41 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 42 |
@Override |
| 43 |
public MaybeSource<Object> apply(Maybe<Object> m) throws Exception { |
| 44 |
return m.doOnSuccess(Functions.emptyConsumer()); |
| 45 |
} |
| 46 |
}); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void doubleError() { |
| 51 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 52 |
|
| 53 |
final Throwable[] err = { null }; |
| 54 |
|
| 55 |
try { |
| 56 |
TestObserverEx<Integer> to = new Maybe<Integer>() { |
| 57 |
@Override |
| 58 |
protected void subscribeActual(MaybeObserver<? super Integer> observer) { |
| 59 |
observer.onSubscribe(Disposable.empty()); |
| 60 |
observer.onError(new TestException("First")); |
| 61 |
observer.onError(new TestException("Second")); |
| 62 |
} |
| 63 |
} |
| 64 |
.doOnError(new Consumer<Throwable>() { |
| 65 |
@Override |
| 66 |
public void accept(Throwable e) throws Exception { |
| 67 |
err[0] = e; |
| 68 |
} |
| 69 |
}) |
| 70 |
.to(TestHelper.<Integer>testConsumer()); |
| 71 |
|
| 72 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 73 |
|
| 74 |
assertTrue("" + err, err[0] instanceof TestException); |
| 75 |
assertEquals("First", err[0].getMessage()); |
| 76 |
|
| 77 |
to.assertFailureAndMessage(TestException.class, "First"); |
| 78 |
} finally { |
| 79 |
RxJavaPlugins.reset(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void doubleComplete() { |
| 85 |
final int[] compl = { 0 }; |
| 86 |
|
| 87 |
TestObserver<Integer> to = new Maybe<Integer>() { |
| 88 |
@Override |
| 89 |
protected void subscribeActual(MaybeObserver<? super Integer> observer) { |
| 90 |
observer.onSubscribe(Disposable.empty()); |
| 91 |
observer.onComplete(); |
| 92 |
observer.onComplete(); |
| 93 |
} |
| 94 |
} |
| 95 |
.doOnComplete(new Action() { |
| 96 |
@Override |
| 97 |
public void run() throws Exception { |
| 98 |
compl[0]++; |
| 99 |
} |
| 100 |
}) |
| 101 |
.test(); |
| 102 |
|
| 103 |
assertEquals(1, compl[0]); |
| 104 |
|
| 105 |
to.assertResult(); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void doOnErrorThrows() { |
| 110 |
TestObserverEx<Object> to = Maybe.error(new TestException("Main")) |
| 111 |
.doOnError(new Consumer<Object>() { |
| 112 |
@Override |
| 113 |
public void accept(Object t) throws Exception { |
| 114 |
throw new TestException("Inner"); |
| 115 |
} |
| 116 |
}) |
| 117 |
.to(TestHelper.<Object>testConsumer()); |
| 118 |
|
| 119 |
to.assertFailure(CompositeException.class); |
| 120 |
|
| 121 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 122 |
|
| 123 |
TestHelper.assertError(errors, 0, TestException.class, "Main"); |
| 124 |
TestHelper.assertError(errors, 1, TestException.class, "Inner"); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void afterTerminateThrows() { |
| 129 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 130 |
|
| 131 |
try { |
| 132 |
|
| 133 |
Maybe.just(1) |
| 134 |
.doAfterTerminate(new Action() { |
| 135 |
@Override |
| 136 |
public void run() throws Exception { |
| 137 |
throw new TestException(); |
| 138 |
} |
| 139 |
}) |
| 140 |
.test() |
| 141 |
.assertResult(1); |
| 142 |
|
| 143 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 144 |
} finally { |
| 145 |
RxJavaPlugins.reset(); |
| 146 |
} |
| 147 |
} |
| 148 |
} |