| 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.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 28 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class ParallelReduceFullTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void cancel() { |
| 35 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 36 |
|
| 37 |
TestSubscriber<Integer> ts = pp |
| 38 |
.parallel() |
| 39 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 40 |
@Override |
| 41 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 42 |
return a + b; |
| 43 |
} |
| 44 |
}) |
| 45 |
.test(); |
| 46 |
|
| 47 |
assertTrue(pp.hasSubscribers()); |
| 48 |
|
| 49 |
ts.cancel(); |
| 50 |
|
| 51 |
assertFalse(pp.hasSubscribers()); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void error() { |
| 56 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 57 |
|
| 58 |
try { |
| 59 |
Flowable.<Integer>error(new TestException()) |
| 60 |
.parallel() |
| 61 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 62 |
@Override |
| 63 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 64 |
return a + b; |
| 65 |
} |
| 66 |
}) |
| 67 |
.test() |
| 68 |
.assertFailure(TestException.class); |
| 69 |
|
| 70 |
assertTrue(errors.isEmpty()); |
| 71 |
} finally { |
| 72 |
RxJavaPlugins.reset(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void error2() { |
| 78 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 79 |
|
| 80 |
try { |
| 81 |
ParallelFlowable.fromArray(Flowable.<Integer>error(new IOException()), Flowable.<Integer>error(new TestException())) |
| 82 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 83 |
@Override |
| 84 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 85 |
return a + b; |
| 86 |
} |
| 87 |
}) |
| 88 |
.test() |
| 89 |
.assertFailure(IOException.class); |
| 90 |
|
| 91 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 92 |
} finally { |
| 93 |
RxJavaPlugins.reset(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Test |
| 98 |
public void empty() { |
| 99 |
Flowable.<Integer>empty() |
| 100 |
.parallel() |
| 101 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 102 |
@Override |
| 103 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 104 |
return a + b; |
| 105 |
} |
| 106 |
}) |
| 107 |
.test() |
| 108 |
.assertResult(); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void doubleError() { |
| 113 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 114 |
try { |
| 115 |
new ParallelInvalid() |
| 116 |
.reduce(new BiFunction<Object, Object, Object>() { |
| 117 |
@Override |
| 118 |
public Object apply(Object a, Object b) throws Exception { |
| 119 |
return "" + a + b; |
| 120 |
} |
| 121 |
}) |
| 122 |
.test() |
| 123 |
.assertFailure(TestException.class); |
| 124 |
|
| 125 |
assertFalse(errors.isEmpty()); |
| 126 |
for (Throwable ex : errors) { |
| 127 |
assertTrue(ex.toString(), ex.getCause() instanceof TestException); |
| 128 |
} |
| 129 |
} finally { |
| 130 |
RxJavaPlugins.reset(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void reducerCrash() { |
| 136 |
Flowable.range(1, 4) |
| 137 |
.parallel(2) |
| 138 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 139 |
@Override |
| 140 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 141 |
if (b == 3) { |
| 142 |
throw new TestException(); |
| 143 |
} |
| 144 |
return a + b; |
| 145 |
} |
| 146 |
}) |
| 147 |
.test() |
| 148 |
.assertFailure(TestException.class); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void reducerCrash2() { |
| 153 |
Flowable.range(1, 4) |
| 154 |
.parallel(2) |
| 155 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 156 |
@Override |
| 157 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 158 |
if (a == 1 + 3) { |
| 159 |
throw new TestException(); |
| 160 |
} |
| 161 |
return a + b; |
| 162 |
} |
| 163 |
}) |
| 164 |
.test() |
| 165 |
.assertFailure(TestException.class); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void doubleOnSubscribe() { |
| 170 |
TestHelper.checkDoubleOnSubscribeParallelToFlowable( |
| 171 |
pf -> pf.reduce((a, b) -> a) |
| 172 |
); |
| 173 |
} |
| 174 |
} |