| 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.parallel; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.Subscription; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.testsupport.*; |
| 30 |
|
| 31 |
public class ParallelPeekTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void subscriberCount() { |
| 35 |
ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel() |
| 36 |
.doOnNext(Functions.emptyConsumer())); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
@SuppressUndeliverable |
| 41 |
public void onSubscribeCrash() { |
| 42 |
Flowable.range(1, 5) |
| 43 |
.parallel() |
| 44 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 45 |
@Override |
| 46 |
public void accept(Subscription s) throws Exception { |
| 47 |
throw new TestException(); |
| 48 |
} |
| 49 |
}) |
| 50 |
.sequential() |
| 51 |
.test() |
| 52 |
.assertFailure(TestException.class); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void doubleError() { |
| 57 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 58 |
try { |
| 59 |
new ParallelInvalid() |
| 60 |
.doOnNext(Functions.emptyConsumer()) |
| 61 |
.sequential() |
| 62 |
.test() |
| 63 |
.assertFailure(TestException.class); |
| 64 |
|
| 65 |
assertFalse(errors.isEmpty()); |
| 66 |
for (Throwable ex : errors) { |
| 67 |
assertTrue(ex.toString(), ex.getCause() instanceof TestException); |
| 68 |
} |
| 69 |
} finally { |
| 70 |
RxJavaPlugins.reset(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void requestCrash() { |
| 76 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 77 |
|
| 78 |
try { |
| 79 |
Flowable.range(1, 5) |
| 80 |
.parallel() |
| 81 |
.doOnRequest(new LongConsumer() { |
| 82 |
@Override |
| 83 |
public void accept(long n) throws Exception { |
| 84 |
throw new TestException(); |
| 85 |
} |
| 86 |
}) |
| 87 |
.sequential() |
| 88 |
.test() |
| 89 |
.assertResult(1, 2, 3, 4, 5); |
| 90 |
|
| 91 |
assertFalse(errors.isEmpty()); |
| 92 |
|
| 93 |
for (Throwable ex : errors) { |
| 94 |
assertTrue(ex.toString(), ex.getCause() instanceof TestException); |
| 95 |
} |
| 96 |
} finally { |
| 97 |
RxJavaPlugins.reset(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void cancelCrash() { |
| 103 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 104 |
|
| 105 |
try { |
| 106 |
Flowable.<Integer>never() |
| 107 |
.parallel() |
| 108 |
.doOnCancel(new Action() { |
| 109 |
@Override |
| 110 |
public void run() throws Exception { |
| 111 |
throw new TestException(); |
| 112 |
} |
| 113 |
}) |
| 114 |
.sequential() |
| 115 |
.test() |
| 116 |
.cancel(); |
| 117 |
|
| 118 |
assertFalse(errors.isEmpty()); |
| 119 |
|
| 120 |
for (Throwable ex : errors) { |
| 121 |
assertTrue(ex.toString(), ex.getCause() instanceof TestException); |
| 122 |
} |
| 123 |
} finally { |
| 124 |
RxJavaPlugins.reset(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
@SuppressUndeliverable |
| 130 |
public void onCompleteCrash() { |
| 131 |
Flowable.just(1) |
| 132 |
.parallel() |
| 133 |
.doOnComplete(new Action() { |
| 134 |
@Override |
| 135 |
public void run() throws Exception { |
| 136 |
throw new TestException(); |
| 137 |
} |
| 138 |
}) |
| 139 |
.sequential() |
| 140 |
.test() |
| 141 |
.assertFailure(TestException.class, 1); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void onAfterTerminatedCrash() { |
| 146 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 147 |
|
| 148 |
try { |
| 149 |
Flowable.just(1) |
| 150 |
.parallel() |
| 151 |
.doAfterTerminated(new Action() { |
| 152 |
@Override |
| 153 |
public void run() throws Exception { |
| 154 |
throw new TestException(); |
| 155 |
} |
| 156 |
}) |
| 157 |
.sequential() |
| 158 |
.test() |
| 159 |
.assertResult(1); |
| 160 |
|
| 161 |
assertFalse(errors.isEmpty()); |
| 162 |
|
| 163 |
for (Throwable ex : errors) { |
| 164 |
assertTrue(ex.toString(), ex.getCause() instanceof TestException); |
| 165 |
} |
| 166 |
} finally { |
| 167 |
RxJavaPlugins.reset(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void onAfterTerminatedCrash2() { |
| 173 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 174 |
|
| 175 |
try { |
| 176 |
Flowable.<Integer>error(new IOException()) |
| 177 |
.parallel() |
| 178 |
.doAfterTerminated(new Action() { |
| 179 |
@Override |
| 180 |
public void run() throws Exception { |
| 181 |
throw new TestException(); |
| 182 |
} |
| 183 |
}) |
| 184 |
.sequential() |
| 185 |
.test() |
| 186 |
.assertFailure(IOException.class); |
| 187 |
|
| 188 |
assertFalse(errors.isEmpty()); |
| 189 |
|
| 190 |
for (Throwable ex : errors) { |
| 191 |
Throwable exc = ex.getCause(); |
| 192 |
assertTrue(ex.toString(), exc instanceof TestException |
| 193 |
|| exc instanceof IOException); |
| 194 |
} |
| 195 |
} finally { |
| 196 |
RxJavaPlugins.reset(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void doubleOnSubscribe() { |
| 202 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> |
| 203 |
ParallelFlowable.fromArray(f) |
| 204 |
.doOnComplete(() -> { }) |
| 205 |
.sequential() |
| 206 |
); |
| 207 |
} |
| 208 |
} |