| 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 |
|
| 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.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class MaybeTakeUntilPublisherTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void disposed() { |
| 34 |
TestHelper.checkDisposed(PublishProcessor.create().singleElement().takeUntil(Flowable.never())); |
| 35 |
} |
| 36 |
|
| 37 |
@Test |
| 38 |
public void doubleOnSubscribe() { |
| 39 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 40 |
@Override |
| 41 |
public MaybeSource<Object> apply(Maybe<Object> m) throws Exception { |
| 42 |
return m.takeUntil(Flowable.never()); |
| 43 |
} |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void mainErrors() { |
| 49 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 50 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 51 |
|
| 52 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2).test(); |
| 53 |
|
| 54 |
assertTrue(pp1.hasSubscribers()); |
| 55 |
assertTrue(pp2.hasSubscribers()); |
| 56 |
|
| 57 |
pp1.onError(new TestException()); |
| 58 |
|
| 59 |
assertFalse(pp1.hasSubscribers()); |
| 60 |
assertFalse(pp2.hasSubscribers()); |
| 61 |
|
| 62 |
to.assertFailure(TestException.class); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void otherErrors() { |
| 67 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 68 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 69 |
|
| 70 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2).test(); |
| 71 |
|
| 72 |
assertTrue(pp1.hasSubscribers()); |
| 73 |
assertTrue(pp2.hasSubscribers()); |
| 74 |
|
| 75 |
pp2.onError(new TestException()); |
| 76 |
|
| 77 |
assertFalse(pp1.hasSubscribers()); |
| 78 |
assertFalse(pp2.hasSubscribers()); |
| 79 |
|
| 80 |
to.assertFailure(TestException.class); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void mainCompletes() { |
| 85 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 86 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 87 |
|
| 88 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2).test(); |
| 89 |
|
| 90 |
assertTrue(pp1.hasSubscribers()); |
| 91 |
assertTrue(pp2.hasSubscribers()); |
| 92 |
|
| 93 |
pp1.onComplete(); |
| 94 |
|
| 95 |
assertFalse(pp1.hasSubscribers()); |
| 96 |
assertFalse(pp2.hasSubscribers()); |
| 97 |
|
| 98 |
to.assertResult(); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void otherCompletes() { |
| 103 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 104 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 105 |
|
| 106 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2).test(); |
| 107 |
|
| 108 |
assertTrue(pp1.hasSubscribers()); |
| 109 |
assertTrue(pp2.hasSubscribers()); |
| 110 |
|
| 111 |
pp2.onComplete(); |
| 112 |
|
| 113 |
assertFalse(pp1.hasSubscribers()); |
| 114 |
assertFalse(pp2.hasSubscribers()); |
| 115 |
|
| 116 |
to.assertResult(); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void onErrorRace() { |
| 121 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 122 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 123 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 124 |
|
| 125 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2).test(); |
| 126 |
|
| 127 |
final TestException ex1 = new TestException(); |
| 128 |
final TestException ex2 = new TestException(); |
| 129 |
|
| 130 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 131 |
try { |
| 132 |
|
| 133 |
Runnable r1 = new Runnable() { |
| 134 |
@Override |
| 135 |
public void run() { |
| 136 |
pp1.onError(ex1); |
| 137 |
} |
| 138 |
}; |
| 139 |
Runnable r2 = new Runnable() { |
| 140 |
@Override |
| 141 |
public void run() { |
| 142 |
pp2.onError(ex2); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
TestHelper.race(r1, r2); |
| 147 |
|
| 148 |
to.assertFailure(TestException.class); |
| 149 |
|
| 150 |
if (!errors.isEmpty()) { |
| 151 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 152 |
} |
| 153 |
|
| 154 |
} finally { |
| 155 |
RxJavaPlugins.reset(); |
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void onCompleteRace() { |
| 163 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 164 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 165 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 166 |
|
| 167 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2).test(); |
| 168 |
|
| 169 |
Runnable r1 = new Runnable() { |
| 170 |
@Override |
| 171 |
public void run() { |
| 172 |
pp1.onComplete(); |
| 173 |
} |
| 174 |
}; |
| 175 |
Runnable r2 = new Runnable() { |
| 176 |
@Override |
| 177 |
public void run() { |
| 178 |
pp2.onComplete(); |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
TestHelper.race(r1, r2); |
| 183 |
|
| 184 |
to.assertResult(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void otherSignalsAndCompletes() { |
| 190 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 191 |
try { |
| 192 |
Maybe.just(1).takeUntil(Flowable.just(1).take(1)) |
| 193 |
.test() |
| 194 |
.assertResult(); |
| 195 |
|
| 196 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 197 |
} finally { |
| 198 |
RxJavaPlugins.reset(); |
| 199 |
} |
| 200 |
} |
| 201 |
} |