| 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.flowable; |
| 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.RxJavaTest; |
| 23 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowablePublishMulticast.*; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 25 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.processors.UnicastProcessor; |
| 28 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class FlowablePublishMulticastTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void asyncFusedInput() { |
| 35 |
MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 36 |
|
| 37 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 38 |
|
| 39 |
up.subscribe(mp); |
| 40 |
|
| 41 |
TestSubscriber<Integer> ts1 = mp.test(); |
| 42 |
TestSubscriber<Integer> ts2 = mp.take(1).test(); |
| 43 |
|
| 44 |
up.onNext(1); |
| 45 |
up.onNext(2); |
| 46 |
up.onComplete(); |
| 47 |
|
| 48 |
ts1.assertResult(1, 2); |
| 49 |
ts2.assertResult(1); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void fusionRejectedInput() { |
| 54 |
MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 55 |
|
| 56 |
mp.onSubscribe(new QueueSubscription<Integer>() { |
| 57 |
|
| 58 |
@Override |
| 59 |
public int requestFusion(int mode) { |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public boolean offer(Integer value) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean offer(Integer v1, Integer v2) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Integer poll() throws Exception { |
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public boolean isEmpty() { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void clear() { |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void request(long n) { |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void cancel() { |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
TestSubscriber<Integer> ts = mp.test(); |
| 97 |
|
| 98 |
mp.onNext(1); |
| 99 |
mp.onNext(2); |
| 100 |
mp.onComplete(); |
| 101 |
|
| 102 |
ts.assertResult(1, 2); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void addRemoveRace() { |
| 107 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 108 |
|
| 109 |
final MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 110 |
|
| 111 |
final MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp); |
| 112 |
final MulticastSubscription<Integer> ms2 = new MulticastSubscription<>(null, mp); |
| 113 |
|
| 114 |
assertTrue(mp.add(ms1)); |
| 115 |
|
| 116 |
Runnable r1 = new Runnable() { |
| 117 |
@Override |
| 118 |
public void run() { |
| 119 |
mp.remove(ms1); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
Runnable r2 = new Runnable() { |
| 124 |
@Override |
| 125 |
public void run() { |
| 126 |
mp.add(ms2); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
TestHelper.race(r1, r2); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void removeNotFound() { |
| 136 |
MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 137 |
|
| 138 |
MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp); |
| 139 |
assertTrue(mp.add(ms1)); |
| 140 |
|
| 141 |
mp.remove(null); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void errorAllCancelled() { |
| 146 |
MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 147 |
|
| 148 |
MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp); |
| 149 |
assertTrue(mp.add(ms1)); |
| 150 |
|
| 151 |
ms1.set(Long.MIN_VALUE); |
| 152 |
|
| 153 |
mp.errorAll(null); |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void completeAllCancelled() { |
| 158 |
MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 159 |
|
| 160 |
MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp); |
| 161 |
assertTrue(mp.add(ms1)); |
| 162 |
|
| 163 |
ms1.set(Long.MIN_VALUE); |
| 164 |
|
| 165 |
mp.completeAll(); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void cancelledWhileFindingRequests() { |
| 170 |
final MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 171 |
|
| 172 |
final MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp); |
| 173 |
|
| 174 |
assertTrue(mp.add(ms1)); |
| 175 |
|
| 176 |
mp.onSubscribe(new BooleanSubscription()); |
| 177 |
|
| 178 |
ms1.set(Long.MIN_VALUE); |
| 179 |
|
| 180 |
mp.drain(); |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void negativeRequest() { |
| 185 |
final MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 186 |
|
| 187 |
final MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp); |
| 188 |
|
| 189 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 190 |
try { |
| 191 |
ms1.request(-1); |
| 192 |
|
| 193 |
TestHelper.assertError(errors, 0, IllegalArgumentException.class); |
| 194 |
} finally { |
| 195 |
RxJavaPlugins.reset(); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
public void outputCancellerDoubleOnSubscribe() { |
| 201 |
TestHelper.doubleOnSubscribe(new OutputCanceller<>(new TestSubscriber<>(), null)); |
| 202 |
} |
| 203 |
|
| 204 |
@Test |
| 205 |
public void dontDropItemsWhenNoReadyConsumers() { |
| 206 |
final MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true); |
| 207 |
|
| 208 |
mp.onSubscribe(new BooleanSubscription()); |
| 209 |
|
| 210 |
mp.onNext(1); |
| 211 |
|
| 212 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 213 |
final MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(ts, mp); |
| 214 |
ts.onSubscribe(ms1); |
| 215 |
|
| 216 |
assertTrue(mp.add(ms1)); |
| 217 |
|
| 218 |
ms1.set(Long.MIN_VALUE); |
| 219 |
|
| 220 |
mp.drain(); |
| 221 |
|
| 222 |
assertFalse(mp.queue.isEmpty()); |
| 223 |
} |
| 224 |
} |