| 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.assertTrue; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.disposables.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class MaybeDoOnEventTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void dispose() { |
| 34 |
TestHelper.checkDisposed(PublishSubject.<Integer>create().singleElement().doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 35 |
@Override |
| 36 |
public void accept(Integer v, Throwable e) throws Exception { |
| 37 |
// irrelevant |
| 38 |
} |
| 39 |
})); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void doubleOnSubscribe() { |
| 44 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, MaybeSource<Integer>>() { |
| 45 |
@Override |
| 46 |
public MaybeSource<Integer> apply(Maybe<Integer> m) throws Exception { |
| 47 |
return m.doOnEvent(new BiConsumer<Integer, Throwable>() { |
| 48 |
@Override |
| 49 |
public void accept(Integer v, Throwable e) throws Exception { |
| 50 |
// irrelevant |
| 51 |
} |
| 52 |
}); |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void onSubscribeCrash() { |
| 59 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 60 |
try { |
| 61 |
final Disposable bs = Disposable.empty(); |
| 62 |
|
| 63 |
new Maybe<Integer>() { |
| 64 |
@Override |
| 65 |
protected void subscribeActual(MaybeObserver<? super Integer> observer) { |
| 66 |
observer.onSubscribe(bs); |
| 67 |
observer.onError(new TestException("Second")); |
| 68 |
observer.onComplete(); |
| 69 |
observer.onSuccess(1); |
| 70 |
} |
| 71 |
} |
| 72 |
.doOnSubscribe(new Consumer<Disposable>() { |
| 73 |
@Override |
| 74 |
public void accept(Disposable d) throws Exception { |
| 75 |
throw new TestException("First"); |
| 76 |
} |
| 77 |
}) |
| 78 |
.to(TestHelper.<Integer>testConsumer()) |
| 79 |
.assertFailureAndMessage(TestException.class, "First"); |
| 80 |
|
| 81 |
assertTrue(bs.isDisposed()); |
| 82 |
|
| 83 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 84 |
} finally { |
| 85 |
RxJavaPlugins.reset(); |
| 86 |
} |
| 87 |
} |
| 88 |
} |