| 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 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.time.Duration; |
| 22 |
import java.util.*; |
| 23 |
import java.util.concurrent.*; |
| 24 |
import java.util.concurrent.atomic.*; |
| 25 |
|
| 26 |
import org.junit.Test; |
| 27 |
import org.mockito.Mockito; |
| 28 |
import org.reactivestreams.*; |
| 29 |
|
| 30 |
import com.google.common.base.Ticker; |
| 31 |
import com.google.common.cache.*; |
| 32 |
|
| 33 |
import io.reactivex.rxjava3.core.*; |
| 34 |
import io.reactivex.rxjava3.exceptions.*; |
| 35 |
import io.reactivex.rxjava3.flowables.GroupedFlowable; |
| 36 |
import io.reactivex.rxjava3.functions.*; |
| 37 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 38 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 39 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 40 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 41 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 42 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 43 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 44 |
import io.reactivex.rxjava3.subscribers.*; |
| 45 |
import io.reactivex.rxjava3.testsupport.*; |
| 46 |
|
| 47 |
public class FlowableGroupByTest extends RxJavaTest { |
| 48 |
|
| 49 |
static Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>> FLATTEN_INTEGER = new Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>>() { |
| 50 |
|
| 51 |
@Override |
| 52 |
public Flowable<Integer> apply(GroupedFlowable<Integer, Integer> t) { |
| 53 |
return t; |
| 54 |
} |
| 55 |
|
| 56 |
}; |
| 57 |
|
| 58 |
final Function<String, Integer> length = new Function<String, Integer>() { |
| 59 |
@Override |
| 60 |
public Integer apply(String s) { |
| 61 |
return s.length(); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
@Test |
| 66 |
public void groupBy() { |
| 67 |
Flowable<String> source = Flowable.just("one", "two", "three", "four", "five", "six"); |
| 68 |
Flowable<GroupedFlowable<Integer, String>> grouped = source.groupBy(length); |
| 69 |
|
| 70 |
Map<Integer, Collection<String>> map = toMap(grouped); |
| 71 |
|
| 72 |
assertEquals(3, map.size()); |
| 73 |
assertArrayEquals(Arrays.asList("one", "two", "six").toArray(), map.get(3).toArray()); |
| 74 |
assertArrayEquals(Arrays.asList("four", "five").toArray(), map.get(4).toArray()); |
| 75 |
assertArrayEquals(Arrays.asList("three").toArray(), map.get(5).toArray()); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void groupByWithElementSelector() { |
| 80 |
Flowable<String> source = Flowable.just("one", "two", "three", "four", "five", "six"); |
| 81 |
Flowable<GroupedFlowable<Integer, Integer>> grouped = source.groupBy(length, length); |
| 82 |
|
| 83 |
Map<Integer, Collection<Integer>> map = toMap(grouped); |
| 84 |
|
| 85 |
assertEquals(3, map.size()); |
| 86 |
assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray()); |
| 87 |
assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray()); |
| 88 |
assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray()); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void groupByWithElementSelector2() { |
| 93 |
Flowable<String> source = Flowable.just("one", "two", "three", "four", "five", "six"); |
| 94 |
Flowable<GroupedFlowable<Integer, Integer>> grouped = source.groupBy(length, length); |
| 95 |
|
| 96 |
Map<Integer, Collection<Integer>> map = toMap(grouped); |
| 97 |
|
| 98 |
assertEquals(3, map.size()); |
| 99 |
assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray()); |
| 100 |
assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray()); |
| 101 |
assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray()); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void empty() { |
| 106 |
Flowable<String> source = Flowable.empty(); |
| 107 |
Flowable<GroupedFlowable<Integer, String>> grouped = source.groupBy(length); |
| 108 |
|
| 109 |
Map<Integer, Collection<String>> map = toMap(grouped); |
| 110 |
|
| 111 |
assertTrue(map.isEmpty()); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
@SuppressUndeliverable |
| 116 |
public void error() { |
| 117 |
Flowable<String> sourceStrings = Flowable.just("one", "two", "three", "four", "five", "six"); |
| 118 |
Flowable<String> errorSource = Flowable.error(new TestException("forced failure")); |
| 119 |
Flowable<String> source = Flowable.concat(sourceStrings, errorSource); |
| 120 |
|
| 121 |
Flowable<GroupedFlowable<Integer, String>> grouped = source.groupBy(length); |
| 122 |
|
| 123 |
final AtomicInteger groupCounter = new AtomicInteger(); |
| 124 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 125 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 126 |
|
| 127 |
grouped.flatMap(new Function<GroupedFlowable<Integer, String>, Flowable<String>>() { |
| 128 |
|
| 129 |
@Override |
| 130 |
public Flowable<String> apply(final GroupedFlowable<Integer, String> f) { |
| 131 |
groupCounter.incrementAndGet(); |
| 132 |
return f.map(new Function<String, String>() { |
| 133 |
|
| 134 |
@Override |
| 135 |
public String apply(String v) { |
| 136 |
return "Event => key: " + f.getKey() + " value: " + v; |
| 137 |
} |
| 138 |
}); |
| 139 |
} |
| 140 |
}).subscribe(new DefaultSubscriber<String>() { |
| 141 |
|
| 142 |
@Override |
| 143 |
public void onComplete() { |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void onError(Throwable e) { |
| 149 |
// e.printStackTrace(); |
| 150 |
error.set(e); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public void onNext(String v) { |
| 155 |
eventCounter.incrementAndGet(); |
| 156 |
System.out.println(v); |
| 157 |
|
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
assertEquals(3, groupCounter.get()); |
| 162 |
assertEquals(6, eventCounter.get()); |
| 163 |
assertNotNull(error.get()); |
| 164 |
assertTrue("" + error.get(), error.get() instanceof TestException); |
| 165 |
assertEquals(error.get().getMessage(), "forced failure"); |
| 166 |
} |
| 167 |
|
| 168 |
private static <K, V> Map<K, Collection<V>> toMap(Flowable<GroupedFlowable<K, V>> flowable) { |
| 169 |
|
| 170 |
final ConcurrentHashMap<K, Collection<V>> result = new ConcurrentHashMap<>(); |
| 171 |
|
| 172 |
flowable.doOnNext(new Consumer<GroupedFlowable<K, V>>() { |
| 173 |
|
| 174 |
@Override |
| 175 |
public void accept(final GroupedFlowable<K, V> f) { |
| 176 |
result.put(f.getKey(), new ConcurrentLinkedQueue<>()); |
| 177 |
f.subscribe(new Consumer<V>() { |
| 178 |
|
| 179 |
@Override |
| 180 |
public void accept(V v) { |
| 181 |
result.get(f.getKey()).add(v); |
| 182 |
} |
| 183 |
|
| 184 |
}); |
| 185 |
} |
| 186 |
}).blockingSubscribe(); |
| 187 |
|
| 188 |
return result; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Assert that only a single subscription to a stream occurs and that all events are received. |
| 193 |
* |
| 194 |
* @throws Throwable some method call is declared throws |
| 195 |
*/ |
| 196 |
@Test |
| 197 |
public void groupedEventStream() throws Throwable { |
| 198 |
|
| 199 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 200 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 201 |
final AtomicInteger groupCounter = new AtomicInteger(); |
| 202 |
final CountDownLatch latch = new CountDownLatch(1); |
| 203 |
final int count = 100; |
| 204 |
final int groupCount = 2; |
| 205 |
|
| 206 |
Flowable<Event> es = Flowable.unsafeCreate(new Publisher<Event>() { |
| 207 |
|
| 208 |
@Override |
| 209 |
public void subscribe(final Subscriber<? super Event> subscriber) { |
| 210 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 211 |
System.out.println("*** Subscribing to EventStream ***"); |
| 212 |
subscribeCounter.incrementAndGet(); |
| 213 |
new Thread(new Runnable() { |
| 214 |
|
| 215 |
@Override |
| 216 |
public void run() { |
| 217 |
for (int i = 0; i < count; i++) { |
| 218 |
Event e = new Event(); |
| 219 |
e.source = i % groupCount; |
| 220 |
e.message = "Event-" + i; |
| 221 |
subscriber.onNext(e); |
| 222 |
} |
| 223 |
subscriber.onComplete(); |
| 224 |
} |
| 225 |
|
| 226 |
}).start(); |
| 227 |
} |
| 228 |
|
| 229 |
}); |
| 230 |
|
| 231 |
es.groupBy(new Function<Event, Integer>() { |
| 232 |
|
| 233 |
@Override |
| 234 |
public Integer apply(Event e) { |
| 235 |
return e.source; |
| 236 |
} |
| 237 |
}).flatMap(new Function<GroupedFlowable<Integer, Event>, Flowable<String>>() { |
| 238 |
|
| 239 |
@Override |
| 240 |
public Flowable<String> apply(GroupedFlowable<Integer, Event> eventGroupedFlowable) { |
| 241 |
System.out.println("GroupedFlowable Key: " + eventGroupedFlowable.getKey()); |
| 242 |
groupCounter.incrementAndGet(); |
| 243 |
|
| 244 |
return eventGroupedFlowable.map(new Function<Event, String>() { |
| 245 |
|
| 246 |
@Override |
| 247 |
public String apply(Event event) { |
| 248 |
return "Source: " + event.source + " Message: " + event.message; |
| 249 |
} |
| 250 |
}); |
| 251 |
|
| 252 |
} |
| 253 |
}).subscribe(new DefaultSubscriber<String>() { |
| 254 |
|
| 255 |
@Override |
| 256 |
public void onComplete() { |
| 257 |
latch.countDown(); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public void onError(Throwable e) { |
| 262 |
e.printStackTrace(); |
| 263 |
latch.countDown(); |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public void onNext(String outputMessage) { |
| 268 |
System.out.println(outputMessage); |
| 269 |
eventCounter.incrementAndGet(); |
| 270 |
} |
| 271 |
}); |
| 272 |
|
| 273 |
latch.await(5000, TimeUnit.MILLISECONDS); |
| 274 |
assertEquals(1, subscribeCounter.get()); |
| 275 |
assertEquals(groupCount, groupCounter.get()); |
| 276 |
assertEquals(count, eventCounter.get()); |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* We will only take 1 group with 20 events from it and then unsubscribe. |
| 282 |
*/ |
| 283 |
@Test |
| 284 |
public void unsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException { |
| 285 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 286 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 287 |
doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); |
| 288 |
Thread.sleep(500); |
| 289 |
assertEquals(39, sentEventCounter.get()); |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
* We will only take 1 group with 20 events from it and then unsubscribe. |
| 294 |
*/ |
| 295 |
@Test |
| 296 |
public void unsubscribeOnNestedTakeAndAsyncInfiniteStream() throws InterruptedException { |
| 297 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 298 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 299 |
doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(ASYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); |
| 300 |
Thread.sleep(500); |
| 301 |
assertEquals(39, sentEventCounter.get()); |
| 302 |
} |
| 303 |
|
| 304 |
private void doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(Flowable<Event> es, AtomicInteger subscribeCounter) throws InterruptedException { |
| 305 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 306 |
final AtomicInteger groupCounter = new AtomicInteger(); |
| 307 |
final CountDownLatch latch = new CountDownLatch(1); |
| 308 |
|
| 309 |
es.groupBy(new Function<Event, Integer>() { |
| 310 |
|
| 311 |
@Override |
| 312 |
public Integer apply(Event e) { |
| 313 |
return e.source; |
| 314 |
} |
| 315 |
}) |
| 316 |
.take(1) // we want only the first group |
| 317 |
.flatMap(new Function<GroupedFlowable<Integer, Event>, Flowable<String>>() { |
| 318 |
|
| 319 |
@Override |
| 320 |
public Flowable<String> apply(GroupedFlowable<Integer, Event> eventGroupedFlowable) { |
| 321 |
System.out.println("testUnsubscribe => GroupedFlowable Key: " + eventGroupedFlowable.getKey()); |
| 322 |
groupCounter.incrementAndGet(); |
| 323 |
|
| 324 |
return eventGroupedFlowable |
| 325 |
.take(20) // limit to only 20 events on this group |
| 326 |
.map(new Function<Event, String>() { |
| 327 |
|
| 328 |
@Override |
| 329 |
public String apply(Event event) { |
| 330 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 331 |
} |
| 332 |
}); |
| 333 |
|
| 334 |
} |
| 335 |
}).subscribe(new DefaultSubscriber<String>() { |
| 336 |
|
| 337 |
@Override |
| 338 |
public void onComplete() { |
| 339 |
latch.countDown(); |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public void onError(Throwable e) { |
| 344 |
e.printStackTrace(); |
| 345 |
latch.countDown(); |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
public void onNext(String outputMessage) { |
| 350 |
System.out.println(outputMessage); |
| 351 |
eventCounter.incrementAndGet(); |
| 352 |
} |
| 353 |
}); |
| 354 |
|
| 355 |
if (!latch.await(2000, TimeUnit.MILLISECONDS)) { |
| 356 |
fail("timed out so likely did not unsubscribe correctly"); |
| 357 |
} |
| 358 |
assertEquals(1, subscribeCounter.get()); |
| 359 |
assertEquals(1, groupCounter.get()); |
| 360 |
assertEquals(20, eventCounter.get()); |
| 361 |
// sentEvents will go until 'eventCounter' hits 20 and then unsubscribes |
| 362 |
// which means it will also send (but ignore) the 19/20 events for the other group |
| 363 |
// It will not however send all 100 events. |
| 364 |
} |
| 365 |
|
| 366 |
@Test |
| 367 |
public void unsubscribeViaTakeOnGroupThenMergeAndTake() { |
| 368 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 369 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 370 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 371 |
|
| 372 |
SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) |
| 373 |
.groupBy(new Function<Event, Integer>() { |
| 374 |
|
| 375 |
@Override |
| 376 |
public Integer apply(Event e) { |
| 377 |
return e.source; |
| 378 |
} |
| 379 |
}) |
| 380 |
// take 2 of the 4 groups |
| 381 |
.take(2) |
| 382 |
.flatMap(new Function<GroupedFlowable<Integer, Event>, Flowable<String>>() { |
| 383 |
|
| 384 |
@Override |
| 385 |
public Flowable<String> apply(GroupedFlowable<Integer, Event> eventGroupedFlowable) { |
| 386 |
return eventGroupedFlowable |
| 387 |
.map(new Function<Event, String>() { |
| 388 |
|
| 389 |
@Override |
| 390 |
public String apply(Event event) { |
| 391 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 392 |
} |
| 393 |
}); |
| 394 |
|
| 395 |
} |
| 396 |
}) |
| 397 |
.take(30).subscribe(new Consumer<String>() { |
| 398 |
|
| 399 |
@Override |
| 400 |
public void accept(String s) { |
| 401 |
eventCounter.incrementAndGet(); |
| 402 |
System.out.println("=> " + s); |
| 403 |
} |
| 404 |
|
| 405 |
}); |
| 406 |
|
| 407 |
assertEquals(30, eventCounter.get()); |
| 408 |
// we should send 28 additional events that are filtered out as they are in the groups we skip |
| 409 |
assertEquals(58, sentEventCounter.get()); |
| 410 |
} |
| 411 |
|
| 412 |
@Test |
| 413 |
public void unsubscribeViaTakeOnGroupThenTakeOnInner() { |
| 414 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 415 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 416 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 417 |
|
| 418 |
SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) |
| 419 |
.groupBy(new Function<Event, Integer>() { |
| 420 |
|
| 421 |
@Override |
| 422 |
public Integer apply(Event e) { |
| 423 |
return e.source; |
| 424 |
} |
| 425 |
}) |
| 426 |
// take 2 of the 4 groups |
| 427 |
.take(2) |
| 428 |
.flatMap(new Function<GroupedFlowable<Integer, Event>, Flowable<String>>() { |
| 429 |
|
| 430 |
@Override |
| 431 |
public Flowable<String> apply(GroupedFlowable<Integer, Event> eventGroupedFlowable) { |
| 432 |
int numToTake = 0; |
| 433 |
if (eventGroupedFlowable.getKey() == 1) { |
| 434 |
numToTake = 10; |
| 435 |
} else if (eventGroupedFlowable.getKey() == 2) { |
| 436 |
numToTake = 5; |
| 437 |
} |
| 438 |
return eventGroupedFlowable |
| 439 |
.take(numToTake) |
| 440 |
.map(new Function<Event, String>() { |
| 441 |
|
| 442 |
@Override |
| 443 |
public String apply(Event event) { |
| 444 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 445 |
} |
| 446 |
}); |
| 447 |
|
| 448 |
} |
| 449 |
}) |
| 450 |
.subscribe(new Consumer<String>() { |
| 451 |
|
| 452 |
@Override |
| 453 |
public void accept(String s) { |
| 454 |
eventCounter.incrementAndGet(); |
| 455 |
System.out.println("=> " + s); |
| 456 |
} |
| 457 |
|
| 458 |
}); |
| 459 |
|
| 460 |
assertEquals(15, eventCounter.get()); |
| 461 |
// we should send 22 additional events that are filtered out as they are skipped while taking the 15 we want |
| 462 |
assertEquals(37, sentEventCounter.get()); |
| 463 |
} |
| 464 |
|
| 465 |
@Test |
| 466 |
public void staggeredCompletion() throws InterruptedException { |
| 467 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 468 |
final CountDownLatch latch = new CountDownLatch(1); |
| 469 |
Flowable.range(0, 100) |
| 470 |
.groupBy(new Function<Integer, Integer>() { |
| 471 |
|
| 472 |
@Override |
| 473 |
public Integer apply(Integer i) { |
| 474 |
return i % 2; |
| 475 |
} |
| 476 |
}) |
| 477 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>>() { |
| 478 |
|
| 479 |
@Override |
| 480 |
public Flowable<Integer> apply(GroupedFlowable<Integer, Integer> group) { |
| 481 |
if (group.getKey() == 0) { |
| 482 |
return group.delay(100, TimeUnit.MILLISECONDS).map(new Function<Integer, Integer>() { |
| 483 |
@Override |
| 484 |
public Integer apply(Integer t) { |
| 485 |
return t * 10; |
| 486 |
} |
| 487 |
|
| 488 |
}); |
| 489 |
} else { |
| 490 |
return group; |
| 491 |
} |
| 492 |
} |
| 493 |
}) |
| 494 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 495 |
|
| 496 |
@Override |
| 497 |
public void onComplete() { |
| 498 |
System.out.println("=> onComplete"); |
| 499 |
latch.countDown(); |
| 500 |
} |
| 501 |
|
| 502 |
@Override |
| 503 |
public void onError(Throwable e) { |
| 504 |
e.printStackTrace(); |
| 505 |
latch.countDown(); |
| 506 |
} |
| 507 |
|
| 508 |
@Override |
| 509 |
public void onNext(Integer s) { |
| 510 |
eventCounter.incrementAndGet(); |
| 511 |
System.out.println("=> " + s); |
| 512 |
} |
| 513 |
}); |
| 514 |
|
| 515 |
if (!latch.await(3000, TimeUnit.MILLISECONDS)) { |
| 516 |
fail("timed out"); |
| 517 |
} |
| 518 |
|
| 519 |
assertEquals(100, eventCounter.get()); |
| 520 |
} |
| 521 |
|
| 522 |
@Test |
| 523 |
public void completionIfInnerNotSubscribed() throws InterruptedException { |
| 524 |
final CountDownLatch latch = new CountDownLatch(1); |
| 525 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 526 |
Flowable.range(0, 100) |
| 527 |
.groupBy(new Function<Integer, Integer>() { |
| 528 |
|
| 529 |
@Override |
| 530 |
public Integer apply(Integer i) { |
| 531 |
return i % 2; |
| 532 |
} |
| 533 |
}) |
| 534 |
.subscribe(new DefaultSubscriber<GroupedFlowable<Integer, Integer>>() { |
| 535 |
|
| 536 |
@Override |
| 537 |
public void onComplete() { |
| 538 |
latch.countDown(); |
| 539 |
} |
| 540 |
|
| 541 |
@Override |
| 542 |
public void onError(Throwable e) { |
| 543 |
e.printStackTrace(); |
| 544 |
latch.countDown(); |
| 545 |
} |
| 546 |
|
| 547 |
@Override |
| 548 |
public void onNext(GroupedFlowable<Integer, Integer> s) { |
| 549 |
eventCounter.incrementAndGet(); |
| 550 |
System.out.println("=> " + s); |
| 551 |
} |
| 552 |
}); |
| 553 |
if (!latch.await(500, TimeUnit.MILLISECONDS)) { |
| 554 |
fail("timed out - never got completion"); |
| 555 |
} |
| 556 |
// Behavior change: groups not subscribed immediately will be automatically abandoned |
| 557 |
// so this leads to group recreation |
| 558 |
assertEquals(100, eventCounter.get()); |
| 559 |
} |
| 560 |
|
| 561 |
@Test |
| 562 |
public void ignoringGroups() { |
| 563 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 564 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 565 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 566 |
|
| 567 |
SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) |
| 568 |
.groupBy(new Function<Event, Integer>() { |
| 569 |
|
| 570 |
@Override |
| 571 |
public Integer apply(Event e) { |
| 572 |
return e.source; |
| 573 |
} |
| 574 |
}) |
| 575 |
.flatMap(new Function<GroupedFlowable<Integer, Event>, Flowable<String>>() { |
| 576 |
|
| 577 |
@Override |
| 578 |
public Flowable<String> apply(GroupedFlowable<Integer, Event> eventGroupedFlowable) { |
| 579 |
Flowable<Event> eventStream = eventGroupedFlowable; |
| 580 |
if (eventGroupedFlowable.getKey() >= 2) { |
| 581 |
// filter these |
| 582 |
eventStream = eventGroupedFlowable.filter(new Predicate<Event>() { |
| 583 |
@Override |
| 584 |
public boolean test(Event t1) { |
| 585 |
return false; |
| 586 |
} |
| 587 |
}); |
| 588 |
} |
| 589 |
|
| 590 |
return eventStream |
| 591 |
.map(new Function<Event, String>() { |
| 592 |
|
| 593 |
@Override |
| 594 |
public String apply(Event event) { |
| 595 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 596 |
} |
| 597 |
}); |
| 598 |
|
| 599 |
} |
| 600 |
}) |
| 601 |
.take(30).subscribe(new Consumer<String>() { |
| 602 |
|
| 603 |
@Override |
| 604 |
public void accept(String s) { |
| 605 |
eventCounter.incrementAndGet(); |
| 606 |
System.out.println("=> " + s); |
| 607 |
} |
| 608 |
|
| 609 |
}); |
| 610 |
|
| 611 |
assertEquals(30, eventCounter.get()); |
| 612 |
// we should send 30 additional events that are filtered out as they are in the groups we skip |
| 613 |
assertEquals(60, sentEventCounter.get()); |
| 614 |
} |
| 615 |
|
| 616 |
@Test |
| 617 |
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException { |
| 618 |
final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete |
| 619 |
final ArrayList<String> results = new ArrayList<>(); |
| 620 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 621 |
|
| 622 |
@Override |
| 623 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 624 |
sub.onSubscribe(new BooleanSubscription()); |
| 625 |
sub.onNext(1); |
| 626 |
sub.onNext(2); |
| 627 |
sub.onNext(1); |
| 628 |
sub.onNext(2); |
| 629 |
try { |
| 630 |
first.await(); |
| 631 |
} catch (InterruptedException e) { |
| 632 |
sub.onError(e); |
| 633 |
return; |
| 634 |
} |
| 635 |
sub.onNext(3); |
| 636 |
sub.onNext(3); |
| 637 |
sub.onComplete(); |
| 638 |
} |
| 639 |
|
| 640 |
}).groupBy(new Function<Integer, Integer>() { |
| 641 |
|
| 642 |
@Override |
| 643 |
public Integer apply(Integer t) { |
| 644 |
return t; |
| 645 |
} |
| 646 |
|
| 647 |
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() { |
| 648 |
|
| 649 |
@Override |
| 650 |
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) { |
| 651 |
if (group.getKey() < 3) { |
| 652 |
return group.map(new Function<Integer, String>() { |
| 653 |
|
| 654 |
@Override |
| 655 |
public String apply(Integer t1) { |
| 656 |
return "first groups: " + t1; |
| 657 |
} |
| 658 |
|
| 659 |
}) |
| 660 |
// must take(2) so an onComplete + unsubscribe happens on these first 2 groups |
| 661 |
.take(2).doOnComplete(new Action() { |
| 662 |
|
| 663 |
@Override |
| 664 |
public void run() { |
| 665 |
first.countDown(); |
| 666 |
} |
| 667 |
|
| 668 |
}); |
| 669 |
} else { |
| 670 |
return group.map(new Function<Integer, String>() { |
| 671 |
|
| 672 |
@Override |
| 673 |
public String apply(Integer t1) { |
| 674 |
return "last group: " + t1; |
| 675 |
} |
| 676 |
|
| 677 |
}); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
}).blockingForEach(new Consumer<String>() { |
| 682 |
|
| 683 |
@Override |
| 684 |
public void accept(String s) { |
| 685 |
results.add(s); |
| 686 |
} |
| 687 |
|
| 688 |
}); |
| 689 |
|
| 690 |
System.out.println("Results: " + results); |
| 691 |
assertEquals(6, results.size()); |
| 692 |
} |
| 693 |
|
| 694 |
@Test |
| 695 |
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenSubscribesOnAndDelaysAndThenCompletes() throws InterruptedException { |
| 696 |
System.err.println("----------------------------------------------------------------------------------------------"); |
| 697 |
final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete |
| 698 |
final ArrayList<String> results = new ArrayList<>(); |
| 699 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 700 |
|
| 701 |
@Override |
| 702 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 703 |
sub.onSubscribe(new BooleanSubscription()); |
| 704 |
sub.onNext(1); |
| 705 |
sub.onNext(2); |
| 706 |
sub.onNext(1); |
| 707 |
sub.onNext(2); |
| 708 |
try { |
| 709 |
first.await(); |
| 710 |
} catch (InterruptedException e) { |
| 711 |
sub.onError(e); |
| 712 |
return; |
| 713 |
} |
| 714 |
sub.onNext(3); |
| 715 |
sub.onNext(3); |
| 716 |
sub.onComplete(); |
| 717 |
} |
| 718 |
|
| 719 |
}).groupBy(new Function<Integer, Integer>() { |
| 720 |
|
| 721 |
@Override |
| 722 |
public Integer apply(Integer t) { |
| 723 |
return t; |
| 724 |
} |
| 725 |
|
| 726 |
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() { |
| 727 |
|
| 728 |
@Override |
| 729 |
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) { |
| 730 |
if (group.getKey() < 3) { |
| 731 |
return group.map(new Function<Integer, String>() { |
| 732 |
|
| 733 |
@Override |
| 734 |
public String apply(Integer t1) { |
| 735 |
return "first groups: " + t1; |
| 736 |
} |
| 737 |
|
| 738 |
}) |
| 739 |
// must take(2) so an onComplete + unsubscribe happens on these first 2 groups |
| 740 |
.take(2).doOnComplete(new Action() { |
| 741 |
|
| 742 |
@Override |
| 743 |
public void run() { |
| 744 |
first.countDown(); |
| 745 |
} |
| 746 |
|
| 747 |
}); |
| 748 |
} else { |
| 749 |
return group.subscribeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() { |
| 750 |
|
| 751 |
@Override |
| 752 |
public String apply(Integer t1) { |
| 753 |
return "last group: " + t1; |
| 754 |
} |
| 755 |
|
| 756 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 757 |
|
| 758 |
@Override |
| 759 |
public void accept(Notification<String> t1) { |
| 760 |
System.err.println("subscribeOn notification => " + t1); |
| 761 |
} |
| 762 |
|
| 763 |
}); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 768 |
|
| 769 |
@Override |
| 770 |
public void accept(Notification<String> t1) { |
| 771 |
System.err.println("outer notification => " + t1); |
| 772 |
} |
| 773 |
|
| 774 |
}).blockingForEach(new Consumer<String>() { |
| 775 |
|
| 776 |
@Override |
| 777 |
public void accept(String s) { |
| 778 |
results.add(s); |
| 779 |
} |
| 780 |
|
| 781 |
}); |
| 782 |
|
| 783 |
System.out.println("Results: " + results); |
| 784 |
assertEquals(6, results.size()); |
| 785 |
} |
| 786 |
|
| 787 |
@Test |
| 788 |
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes() throws InterruptedException { |
| 789 |
final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete |
| 790 |
final ArrayList<String> results = new ArrayList<>(); |
| 791 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 792 |
|
| 793 |
@Override |
| 794 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 795 |
sub.onSubscribe(new BooleanSubscription()); |
| 796 |
sub.onNext(1); |
| 797 |
sub.onNext(2); |
| 798 |
sub.onNext(1); |
| 799 |
sub.onNext(2); |
| 800 |
try { |
| 801 |
first.await(); |
| 802 |
} catch (InterruptedException e) { |
| 803 |
sub.onError(e); |
| 804 |
return; |
| 805 |
} |
| 806 |
sub.onNext(3); |
| 807 |
sub.onNext(3); |
| 808 |
sub.onComplete(); |
| 809 |
} |
| 810 |
|
| 811 |
}).groupBy(new Function<Integer, Integer>() { |
| 812 |
|
| 813 |
@Override |
| 814 |
public Integer apply(Integer t) { |
| 815 |
return t; |
| 816 |
} |
| 817 |
|
| 818 |
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() { |
| 819 |
|
| 820 |
@Override |
| 821 |
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) { |
| 822 |
if (group.getKey() < 3) { |
| 823 |
return group.map(new Function<Integer, String>() { |
| 824 |
|
| 825 |
@Override |
| 826 |
public String apply(Integer t1) { |
| 827 |
return "first groups: " + t1; |
| 828 |
} |
| 829 |
|
| 830 |
}) |
| 831 |
// must take(2) so an onComplete + unsubscribe happens on these first 2 groups |
| 832 |
.take(2).doOnComplete(new Action() { |
| 833 |
|
| 834 |
@Override |
| 835 |
public void run() { |
| 836 |
first.countDown(); |
| 837 |
} |
| 838 |
|
| 839 |
}); |
| 840 |
} else { |
| 841 |
return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() { |
| 842 |
|
| 843 |
@Override |
| 844 |
public String apply(Integer t1) { |
| 845 |
return "last group: " + t1; |
| 846 |
} |
| 847 |
|
| 848 |
}); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
}).blockingForEach(new Consumer<String>() { |
| 853 |
|
| 854 |
@Override |
| 855 |
public void accept(String s) { |
| 856 |
results.add(s); |
| 857 |
} |
| 858 |
|
| 859 |
}); |
| 860 |
|
| 861 |
System.out.println("Results: " + results); |
| 862 |
assertEquals(6, results.size()); |
| 863 |
} |
| 864 |
|
| 865 |
@Test |
| 866 |
public void groupsWithNestedSubscribeOn() throws InterruptedException { |
| 867 |
final ArrayList<String> results = new ArrayList<>(); |
| 868 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 869 |
|
| 870 |
@Override |
| 871 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 872 |
sub.onSubscribe(new BooleanSubscription()); |
| 873 |
sub.onNext(1); |
| 874 |
sub.onNext(2); |
| 875 |
sub.onNext(1); |
| 876 |
sub.onNext(2); |
| 877 |
sub.onComplete(); |
| 878 |
} |
| 879 |
|
| 880 |
}).groupBy(new Function<Integer, Integer>() { |
| 881 |
|
| 882 |
@Override |
| 883 |
public Integer apply(Integer t) { |
| 884 |
return t; |
| 885 |
} |
| 886 |
|
| 887 |
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() { |
| 888 |
|
| 889 |
@Override |
| 890 |
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) { |
| 891 |
return group.subscribeOn(Schedulers.newThread()).map(new Function<Integer, String>() { |
| 892 |
|
| 893 |
@Override |
| 894 |
public String apply(Integer t1) { |
| 895 |
System.out.println("Received: " + t1 + " on group : " + group.getKey()); |
| 896 |
return "first groups: " + t1; |
| 897 |
} |
| 898 |
|
| 899 |
}); |
| 900 |
} |
| 901 |
|
| 902 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 903 |
|
| 904 |
@Override |
| 905 |
public void accept(Notification<String> t1) { |
| 906 |
System.out.println("notification => " + t1); |
| 907 |
} |
| 908 |
|
| 909 |
}).blockingForEach(new Consumer<String>() { |
| 910 |
|
| 911 |
@Override |
| 912 |
public void accept(String s) { |
| 913 |
results.add(s); |
| 914 |
} |
| 915 |
|
| 916 |
}); |
| 917 |
|
| 918 |
System.out.println("Results: " + results); |
| 919 |
assertEquals(4, results.size()); |
| 920 |
} |
| 921 |
|
| 922 |
@Test |
| 923 |
public void groupsWithNestedObserveOn() throws InterruptedException { |
| 924 |
final ArrayList<String> results = new ArrayList<>(); |
| 925 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 926 |
|
| 927 |
@Override |
| 928 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 929 |
sub.onSubscribe(new BooleanSubscription()); |
| 930 |
sub.onNext(1); |
| 931 |
sub.onNext(2); |
| 932 |
sub.onNext(1); |
| 933 |
sub.onNext(2); |
| 934 |
sub.onComplete(); |
| 935 |
} |
| 936 |
|
| 937 |
}).groupBy(new Function<Integer, Integer>() { |
| 938 |
|
| 939 |
@Override |
| 940 |
public Integer apply(Integer t) { |
| 941 |
return t; |
| 942 |
} |
| 943 |
|
| 944 |
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() { |
| 945 |
|
| 946 |
@Override |
| 947 |
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) { |
| 948 |
return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() { |
| 949 |
|
| 950 |
@Override |
| 951 |
public String apply(Integer t1) { |
| 952 |
return "first groups: " + t1; |
| 953 |
} |
| 954 |
|
| 955 |
}); |
| 956 |
} |
| 957 |
|
| 958 |
}).blockingForEach(new Consumer<String>() { |
| 959 |
|
| 960 |
@Override |
| 961 |
public void accept(String s) { |
| 962 |
results.add(s); |
| 963 |
} |
| 964 |
|
| 965 |
}); |
| 966 |
|
| 967 |
System.out.println("Results: " + results); |
| 968 |
assertEquals(4, results.size()); |
| 969 |
} |
| 970 |
|
| 971 |
private static class Event { |
| 972 |
int source; |
| 973 |
String message; |
| 974 |
|
| 975 |
@Override |
| 976 |
public String toString() { |
| 977 |
return "Event => source: " + source + " message: " + message; |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
Flowable<Event> ASYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final AtomicInteger subscribeCounter, final AtomicInteger sentEventCounter) { |
| 982 |
return SYNC_INFINITE_OBSERVABLE_OF_EVENT(numGroups, subscribeCounter, sentEventCounter).subscribeOn(Schedulers.newThread()); |
| 983 |
}; |
| 984 |
|
| 985 |
Flowable<Event> SYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final AtomicInteger subscribeCounter, final AtomicInteger sentEventCounter) { |
| 986 |
return Flowable.unsafeCreate(new Publisher<Event>() { |
| 987 |
|
| 988 |
@Override |
| 989 |
public void subscribe(final Subscriber<? super Event> op) { |
| 990 |
BooleanSubscription bs = new BooleanSubscription(); |
| 991 |
op.onSubscribe(bs); |
| 992 |
subscribeCounter.incrementAndGet(); |
| 993 |
int i = 0; |
| 994 |
while (!bs.isCancelled()) { |
| 995 |
i++; |
| 996 |
Event e = new Event(); |
| 997 |
e.source = i % numGroups; |
| 998 |
e.message = "Event-" + i; |
| 999 |
op.onNext(e); |
| 1000 |
sentEventCounter.incrementAndGet(); |
| 1001 |
} |
| 1002 |
op.onComplete(); |
| 1003 |
} |
| 1004 |
|
| 1005 |
}); |
| 1006 |
}; |
| 1007 |
|
| 1008 |
@Test |
| 1009 |
public void groupByOnAsynchronousSourceAcceptsMultipleSubscriptions() throws InterruptedException { |
| 1010 |
|
| 1011 |
// choose an asynchronous source |
| 1012 |
Flowable<Long> source = Flowable.interval(10, TimeUnit.MILLISECONDS).take(1); |
| 1013 |
|
| 1014 |
// apply groupBy to the source |
| 1015 |
Flowable<GroupedFlowable<Boolean, Long>> stream = source.groupBy(IS_EVEN); |
| 1016 |
|
| 1017 |
// create two observers |
| 1018 |
Subscriber<GroupedFlowable<Boolean, Long>> f1 = TestHelper.mockSubscriber(); |
| 1019 |
Subscriber<GroupedFlowable<Boolean, Long>> f2 = TestHelper.mockSubscriber(); |
| 1020 |
|
| 1021 |
// subscribe with the observers |
| 1022 |
stream.subscribe(f1); |
| 1023 |
stream.subscribe(f2); |
| 1024 |
|
| 1025 |
// check that subscriptions were successful |
| 1026 |
verify(f1, never()).onError(Mockito.<Throwable> any()); |
| 1027 |
verify(f2, never()).onError(Mockito.<Throwable> any()); |
| 1028 |
} |
| 1029 |
|
| 1030 |
private static Function<Long, Boolean> IS_EVEN = new Function<Long, Boolean>() { |
| 1031 |
|
| 1032 |
@Override |
| 1033 |
public Boolean apply(Long n) { |
| 1034 |
return n % 2 == 0; |
| 1035 |
} |
| 1036 |
}; |
| 1037 |
|
| 1038 |
private static Function<Integer, Boolean> IS_EVEN2 = new Function<Integer, Boolean>() { |
| 1039 |
|
| 1040 |
@Override |
| 1041 |
public Boolean apply(Integer n) { |
| 1042 |
return n % 2 == 0; |
| 1043 |
} |
| 1044 |
}; |
| 1045 |
|
| 1046 |
@Test |
| 1047 |
public void groupByBackpressure() throws InterruptedException { |
| 1048 |
|
| 1049 |
TestSubscriber<String> ts = new TestSubscriber<>(); |
| 1050 |
|
| 1051 |
Flowable.range(1, 4000) |
| 1052 |
.groupBy(IS_EVEN2) |
| 1053 |
.flatMap(new Function<GroupedFlowable<Boolean, Integer>, Flowable<String>>() { |
| 1054 |
|
| 1055 |
@Override |
| 1056 |
public Flowable<String> apply(final GroupedFlowable<Boolean, Integer> g) { |
| 1057 |
return g.observeOn(Schedulers.computation()).map(new Function<Integer, String>() { |
| 1058 |
|
| 1059 |
@Override |
| 1060 |
public String apply(Integer l) { |
| 1061 |
if (g.getKey()) { |
| 1062 |
try { |
| 1063 |
Thread.sleep(1); |
| 1064 |
} catch (InterruptedException e) { |
| 1065 |
} |
| 1066 |
return l + " is even."; |
| 1067 |
} else { |
| 1068 |
return l + " is odd."; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
}); |
| 1073 |
} |
| 1074 |
|
| 1075 |
}).subscribe(ts); |
| 1076 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1077 |
ts.assertNoErrors(); |
| 1078 |
} |
| 1079 |
|
| 1080 |
<T, R> Function<T, R> just(final R value) { |
| 1081 |
return new Function<T, R>() { |
| 1082 |
@Override |
| 1083 |
public R apply(T t1) { |
| 1084 |
return value; |
| 1085 |
} |
| 1086 |
}; |
| 1087 |
} |
| 1088 |
|
| 1089 |
<T> Function<Integer, T> fail(T dummy) { |
| 1090 |
return new Function<Integer, T>() { |
| 1091 |
@Override |
| 1092 |
public T apply(Integer t1) { |
| 1093 |
throw new RuntimeException("Forced failure"); |
| 1094 |
} |
| 1095 |
}; |
| 1096 |
} |
| 1097 |
|
| 1098 |
<T, R> Function<T, R> fail2(R dummy2) { |
| 1099 |
return new Function<T, R>() { |
| 1100 |
@Override |
| 1101 |
public R apply(T t1) { |
| 1102 |
throw new RuntimeException("Forced failure"); |
| 1103 |
} |
| 1104 |
}; |
| 1105 |
} |
| 1106 |
|
| 1107 |
Function<Integer, Integer> dbl = new Function<Integer, Integer>() { |
| 1108 |
@Override |
| 1109 |
public Integer apply(Integer t1) { |
| 1110 |
return t1 * 2; |
| 1111 |
} |
| 1112 |
}; |
| 1113 |
Function<Integer, Integer> identity = new Function<Integer, Integer>() { |
| 1114 |
@Override |
| 1115 |
public Integer apply(Integer v) { |
| 1116 |
return v; |
| 1117 |
} |
| 1118 |
}; |
| 1119 |
|
| 1120 |
@Test |
| 1121 |
public void normalBehavior() { |
| 1122 |
Flowable<String> source = Flowable.fromIterable(Arrays.asList( |
| 1123 |
" foo", |
| 1124 |
" FoO ", |
| 1125 |
"baR ", |
| 1126 |
"foO ", |
| 1127 |
" Baz ", |
| 1128 |
" qux ", |
| 1129 |
" bar", |
| 1130 |
" BAR ", |
| 1131 |
"FOO ", |
| 1132 |
"baz ", |
| 1133 |
" bAZ ", |
| 1134 |
" fOo " |
| 1135 |
)); |
| 1136 |
|
| 1137 |
/* |
| 1138 |
* foo FoO foO FOO fOo |
| 1139 |
* baR bar BAR |
| 1140 |
* Baz baz bAZ |
| 1141 |
* qux |
| 1142 |
* |
| 1143 |
*/ |
| 1144 |
Function<String, String> keysel = new Function<String, String>() { |
| 1145 |
@Override |
| 1146 |
public String apply(String t1) { |
| 1147 |
return t1.trim().toLowerCase(); |
| 1148 |
} |
| 1149 |
}; |
| 1150 |
Function<String, String> valuesel = new Function<String, String>() { |
| 1151 |
@Override |
| 1152 |
public String apply(String t1) { |
| 1153 |
return t1 + t1; |
| 1154 |
} |
| 1155 |
}; |
| 1156 |
|
| 1157 |
Flowable<String> m = source.groupBy(keysel, valuesel) |
| 1158 |
.flatMap(new Function<GroupedFlowable<String, String>, Publisher<String>>() { |
| 1159 |
@Override |
| 1160 |
public Publisher<String> apply(final GroupedFlowable<String, String> g) { |
| 1161 |
System.out.println("-----------> NEXT: " + g.getKey()); |
| 1162 |
return g.take(2).map(new Function<String, String>() { |
| 1163 |
|
| 1164 |
int count; |
| 1165 |
|
| 1166 |
@Override |
| 1167 |
public String apply(String v) { |
| 1168 |
System.out.println(v); |
| 1169 |
return g.getKey() + "-" + count++; |
| 1170 |
} |
| 1171 |
|
| 1172 |
}); |
| 1173 |
} |
| 1174 |
}); |
| 1175 |
|
| 1176 |
TestSubscriber<String> ts = new TestSubscriber<>(); |
| 1177 |
m.subscribe(ts); |
| 1178 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1179 |
System.out.println("ts .get " + ts.values()); |
| 1180 |
ts.assertNoErrors(); |
| 1181 |
assertEquals(ts.values(), |
| 1182 |
Arrays.asList("foo-0", "foo-1", "bar-0", "foo-0", "baz-0", "qux-0", "bar-1", "bar-0", "foo-1", "baz-1", "baz-0", "foo-0")); |
| 1183 |
|
| 1184 |
} |
| 1185 |
|
| 1186 |
@Test |
| 1187 |
public void keySelectorThrows() { |
| 1188 |
Flowable<Integer> source = Flowable.just(0, 1, 2, 3, 4, 5, 6); |
| 1189 |
|
| 1190 |
Flowable<Integer> m = source.groupBy(fail(0), dbl).flatMap(FLATTEN_INTEGER); |
| 1191 |
|
| 1192 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1193 |
m.subscribe(ts); |
| 1194 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1195 |
assertEquals(1, ts.errors().size()); |
| 1196 |
ts.assertNoValues(); |
| 1197 |
} |
| 1198 |
|
| 1199 |
@Test |
| 1200 |
@SuppressUndeliverable |
| 1201 |
public void valueSelectorThrows() { |
| 1202 |
Flowable<Integer> source = Flowable.just(0, 1, 2, 3, 4, 5, 6); |
| 1203 |
|
| 1204 |
Flowable<Integer> m = source.groupBy(identity, fail(0)).flatMap(FLATTEN_INTEGER); |
| 1205 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1206 |
m.subscribe(ts); |
| 1207 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1208 |
assertEquals(1, ts.errors().size()); |
| 1209 |
ts.assertNoValues(); |
| 1210 |
|
| 1211 |
} |
| 1212 |
|
| 1213 |
@Test |
| 1214 |
public void innerEscapeCompleted() { |
| 1215 |
Flowable<Integer> source = Flowable.just(0); |
| 1216 |
|
| 1217 |
Flowable<Integer> m = source.groupBy(identity, dbl).flatMap(FLATTEN_INTEGER); |
| 1218 |
|
| 1219 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 1220 |
m.subscribe(ts); |
| 1221 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1222 |
ts.assertNoErrors(); |
| 1223 |
System.out.println(ts.values()); |
| 1224 |
} |
| 1225 |
|
| 1226 |
/** |
| 1227 |
* Assert we get an IllegalStateException if trying to subscribe to an inner GroupedFlowable more than once. |
| 1228 |
*/ |
| 1229 |
@Test |
| 1230 |
public void exceptionIfSubscribeToChildMoreThanOnce() { |
| 1231 |
Flowable<Integer> source = Flowable.just(0); |
| 1232 |
|
| 1233 |
final AtomicReference<GroupedFlowable<Integer, Integer>> inner = new AtomicReference<>(); |
| 1234 |
|
| 1235 |
Flowable<GroupedFlowable<Integer, Integer>> m = source.groupBy(identity, dbl); |
| 1236 |
|
| 1237 |
m.subscribe(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 1238 |
@Override |
| 1239 |
public void accept(GroupedFlowable<Integer, Integer> t1) { |
| 1240 |
inner.set(t1); |
| 1241 |
} |
| 1242 |
}); |
| 1243 |
|
| 1244 |
inner.get().subscribe(); |
| 1245 |
|
| 1246 |
Subscriber<Integer> subscriber2 = TestHelper.mockSubscriber(); |
| 1247 |
|
| 1248 |
inner.get().subscribe(subscriber2); |
| 1249 |
|
| 1250 |
verify(subscriber2, never()).onComplete(); |
| 1251 |
verify(subscriber2, never()).onNext(anyInt()); |
| 1252 |
verify(subscriber2).onError(any(IllegalStateException.class)); |
| 1253 |
} |
| 1254 |
|
| 1255 |
@Test |
| 1256 |
@SuppressUndeliverable |
| 1257 |
public void error2() { |
| 1258 |
Flowable<Integer> source = Flowable.concat(Flowable.just(0), |
| 1259 |
Flowable.<Integer> error(new TestException("Forced failure"))); |
| 1260 |
|
| 1261 |
Flowable<Integer> m = source.groupBy(identity, dbl).flatMap(FLATTEN_INTEGER); |
| 1262 |
|
| 1263 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 1264 |
m.subscribe(ts); |
| 1265 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1266 |
assertEquals(1, ts.errors().size()); |
| 1267 |
ts.assertValueCount(1); |
| 1268 |
} |
| 1269 |
|
| 1270 |
@Test |
| 1271 |
public void groupByBackpressure3() throws InterruptedException { |
| 1272 |
TestSubscriber<String> ts = new TestSubscriber<>(); |
| 1273 |
|
| 1274 |
Flowable.range(1, 4000).groupBy(IS_EVEN2).flatMap(new Function<GroupedFlowable<Boolean, Integer>, Flowable<String>>() { |
| 1275 |
|
| 1276 |
@Override |
| 1277 |
public Flowable<String> apply(final GroupedFlowable<Boolean, Integer> g) { |
| 1278 |
return g.doOnComplete(new Action() { |
| 1279 |
|
| 1280 |
@Override |
| 1281 |
public void run() { |
| 1282 |
System.out.println("//////////////////// COMPLETED-A"); |
| 1283 |
} |
| 1284 |
|
| 1285 |
}).observeOn(Schedulers.computation()).map(new Function<Integer, String>() { |
| 1286 |
|
| 1287 |
int c; |
| 1288 |
|
| 1289 |
@Override |
| 1290 |
public String apply(Integer l) { |
| 1291 |
if (g.getKey()) { |
| 1292 |
if (c++ < 400) { |
| 1293 |
try { |
| 1294 |
Thread.sleep(1); |
| 1295 |
} catch (InterruptedException e) { |
| 1296 |
} |
| 1297 |
} |
| 1298 |
return l + " is even."; |
| 1299 |
} else { |
| 1300 |
return l + " is odd."; |
| 1301 |
} |
| 1302 |
} |
| 1303 |
|
| 1304 |
}).doOnComplete(new Action() { |
| 1305 |
|
| 1306 |
@Override |
| 1307 |
public void run() { |
| 1308 |
System.out.println("//////////////////// COMPLETED-B"); |
| 1309 |
} |
| 1310 |
|
| 1311 |
}); |
| 1312 |
} |
| 1313 |
|
| 1314 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 1315 |
|
| 1316 |
@Override |
| 1317 |
public void accept(Notification<String> t1) { |
| 1318 |
System.out.println("NEXT: " + t1); |
| 1319 |
} |
| 1320 |
|
| 1321 |
}).subscribe(ts); |
| 1322 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1323 |
ts.assertNoErrors(); |
| 1324 |
} |
| 1325 |
|
| 1326 |
@Test |
| 1327 |
public void groupByBackpressure2() throws InterruptedException { |
| 1328 |
|
| 1329 |
TestSubscriber<String> ts = new TestSubscriber<>(); |
| 1330 |
|
| 1331 |
Flowable.range(1, 4000) |
| 1332 |
.doOnNext(new Consumer<Integer>() { |
| 1333 |
@Override |
| 1334 |
public void accept(Integer v) { |
| 1335 |
System.out.println("testgroupByBackpressure2 >> " + v); |
| 1336 |
} |
| 1337 |
}) |
| 1338 |
.groupBy(IS_EVEN2) |
| 1339 |
.flatMap(new Function<GroupedFlowable<Boolean, Integer>, Flowable<String>>() { |
| 1340 |
@Override |
| 1341 |
public Flowable<String> apply(final GroupedFlowable<Boolean, Integer> g) { |
| 1342 |
return g.take(2) |
| 1343 |
.observeOn(Schedulers.computation()) |
| 1344 |
.map(new Function<Integer, String>() { |
| 1345 |
@Override |
| 1346 |
public String apply(Integer l) { |
| 1347 |
if (g.getKey()) { |
| 1348 |
try { |
| 1349 |
Thread.sleep(1); |
| 1350 |
} catch (InterruptedException e) { |
| 1351 |
} |
| 1352 |
return l + " is even."; |
| 1353 |
} else { |
| 1354 |
return l + " is odd."; |
| 1355 |
} |
| 1356 |
} |
| 1357 |
}); |
| 1358 |
} |
| 1359 |
}, 4000) // a lot of groups are created due to take(2) |
| 1360 |
.subscribe(ts); |
| 1361 |
|
| 1362 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1363 |
ts.assertNoErrors(); |
| 1364 |
} |
| 1365 |
|
| 1366 |
@Test |
| 1367 |
public void groupByWithNullKey() { |
| 1368 |
final String[] key = new String[]{"uninitialized"}; |
| 1369 |
final List<String> values = new ArrayList<>(); |
| 1370 |
Flowable.just("a", "b", "c").groupBy(new Function<String, String>() { |
| 1371 |
|
| 1372 |
@Override |
| 1373 |
public String apply(String value) { |
| 1374 |
return null; |
| 1375 |
} |
| 1376 |
}).subscribe(new Consumer<GroupedFlowable<String, String>>() { |
| 1377 |
|
| 1378 |
@Override |
| 1379 |
public void accept(GroupedFlowable<String, String> groupedFlowable) { |
| 1380 |
key[0] = groupedFlowable.getKey(); |
| 1381 |
groupedFlowable.subscribe(new Consumer<String>() { |
| 1382 |
|
| 1383 |
@Override |
| 1384 |
public void accept(String s) { |
| 1385 |
values.add(s); |
| 1386 |
} |
| 1387 |
}); |
| 1388 |
} |
| 1389 |
}); |
| 1390 |
assertNull(key[0]); |
| 1391 |
assertEquals(Arrays.asList("a", "b", "c"), values); |
| 1392 |
} |
| 1393 |
|
| 1394 |
@Test |
| 1395 |
public void groupByUnsubscribe() { |
| 1396 |
final Subscription s = mock(Subscription.class); |
| 1397 |
Flowable<Integer> f = Flowable.unsafeCreate( |
| 1398 |
new Publisher<Integer>() { |
| 1399 |
@Override |
| 1400 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 1401 |
subscriber.onSubscribe(s); |
| 1402 |
} |
| 1403 |
} |
| 1404 |
); |
| 1405 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 1406 |
|
| 1407 |
f.groupBy(new Function<Integer, Integer>() { |
| 1408 |
|
| 1409 |
@Override |
| 1410 |
public Integer apply(Integer integer) { |
| 1411 |
return null; |
| 1412 |
} |
| 1413 |
}).subscribe(ts); |
| 1414 |
|
| 1415 |
ts.cancel(); |
| 1416 |
|
| 1417 |
verify(s).cancel(); |
| 1418 |
} |
| 1419 |
|
| 1420 |
@Test |
| 1421 |
public void groupByShouldPropagateError() { |
| 1422 |
final Throwable e = new RuntimeException("Oops"); |
| 1423 |
final TestSubscriberEx<Integer> inner1 = new TestSubscriberEx<>(); |
| 1424 |
final TestSubscriberEx<Integer> inner2 = new TestSubscriberEx<>(); |
| 1425 |
|
| 1426 |
final TestSubscriberEx<GroupedFlowable<Integer, Integer>> outer |
| 1427 |
= new TestSubscriberEx<>(new DefaultSubscriber<GroupedFlowable<Integer, Integer>>() { |
| 1428 |
|
| 1429 |
@Override |
| 1430 |
public void onComplete() { |
| 1431 |
} |
| 1432 |
|
| 1433 |
@Override |
| 1434 |
public void onError(Throwable e) { |
| 1435 |
} |
| 1436 |
|
| 1437 |
@Override |
| 1438 |
public void onNext(GroupedFlowable<Integer, Integer> f) { |
| 1439 |
if (f.getKey() == 0) { |
| 1440 |
f.subscribe(inner1); |
| 1441 |
} else { |
| 1442 |
f.subscribe(inner2); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
}); |
| 1446 |
Flowable.unsafeCreate( |
| 1447 |
new Publisher<Integer>() { |
| 1448 |
@Override |
| 1449 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 1450 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1451 |
subscriber.onNext(0); |
| 1452 |
subscriber.onNext(1); |
| 1453 |
subscriber.onError(e); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
).groupBy(new Function<Integer, Integer>() { |
| 1457 |
|
| 1458 |
@Override |
| 1459 |
public Integer apply(Integer i) { |
| 1460 |
return i % 2; |
| 1461 |
} |
| 1462 |
}).subscribe(outer); |
| 1463 |
assertEquals(Arrays.asList(e), outer.errors()); |
| 1464 |
assertEquals(Arrays.asList(e), inner1.errors()); |
| 1465 |
assertEquals(Arrays.asList(e), inner2.errors()); |
| 1466 |
} |
| 1467 |
|
| 1468 |
@Test |
| 1469 |
public void requestOverflow() { |
| 1470 |
final AtomicBoolean completed = new AtomicBoolean(false); |
| 1471 |
Flowable |
| 1472 |
.just(1, 2, 3) |
| 1473 |
// group into one group |
| 1474 |
.groupBy(new Function<Integer, Integer>() { |
| 1475 |
@Override |
| 1476 |
public Integer apply(Integer t) { |
| 1477 |
return 1; |
| 1478 |
} |
| 1479 |
}) |
| 1480 |
// flatten |
| 1481 |
.concatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>>() { |
| 1482 |
@Override |
| 1483 |
public Flowable<Integer> apply(GroupedFlowable<Integer, Integer> g) { |
| 1484 |
return g; |
| 1485 |
} |
| 1486 |
}) |
| 1487 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 1488 |
|
| 1489 |
@Override |
| 1490 |
public void onStart() { |
| 1491 |
request(2); |
| 1492 |
} |
| 1493 |
|
| 1494 |
@Override |
| 1495 |
public void onComplete() { |
| 1496 |
completed.set(true); |
| 1497 |
|
| 1498 |
} |
| 1499 |
|
| 1500 |
@Override |
| 1501 |
public void onError(Throwable e) { |
| 1502 |
|
| 1503 |
} |
| 1504 |
|
| 1505 |
@Override |
| 1506 |
public void onNext(Integer t) { |
| 1507 |
System.out.println(t); |
| 1508 |
//provoke possible request overflow |
| 1509 |
request(Long.MAX_VALUE - 1); |
| 1510 |
}}); |
| 1511 |
assertTrue(completed.get()); |
| 1512 |
} |
| 1513 |
|
| 1514 |
/** |
| 1515 |
* Issue #3425. |
| 1516 |
* |
| 1517 |
* The problem is that a request of 1 may create a new group, emit to the desired group |
| 1518 |
* or emit to a completely different group. In this test, the merge requests N which |
| 1519 |
* must be produced by the range, however it will create a bunch of groups before the actual |
| 1520 |
* group receives a value. |
| 1521 |
* |
| 1522 |
* 12/03/2019: this test produces abandoned groups and as such keeps producing new groups |
| 1523 |
* that have to be ready to be received by observeOn and merge. |
| 1524 |
*/ |
| 1525 |
@Test |
| 1526 |
public void backpressureObserveOnOuter() { |
| 1527 |
int n = 500; |
| 1528 |
for (int j = 0; j < 1000; j++) { |
| 1529 |
Flowable.merge( |
| 1530 |
Flowable.range(0, n) |
| 1531 |
.groupBy(new Function<Integer, Object>() { |
| 1532 |
@Override |
| 1533 |
public Object apply(Integer i) { |
| 1534 |
return i % (Flowable.bufferSize() + 2); |
| 1535 |
} |
| 1536 |
}) |
| 1537 |
.observeOn(Schedulers.computation(), false, n) |
| 1538 |
, n) |
| 1539 |
.blockingLast(); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
@Test(expected = MissingBackpressureException.class) |
| 1544 |
public void backpressureObserveOnOuterMissingBackpressure() { |
| 1545 |
for (int j = 0; j < 1000; j++) { |
| 1546 |
Flowable.merge( |
| 1547 |
Flowable.range(0, 500) |
| 1548 |
.groupBy(new Function<Integer, Object>() { |
| 1549 |
@Override |
| 1550 |
public Object apply(Integer i) { |
| 1551 |
return i % (Flowable.bufferSize() + 2); |
| 1552 |
} |
| 1553 |
}) |
| 1554 |
.observeOn(Schedulers.computation()) |
| 1555 |
).blockingLast(); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
/** |
| 1560 |
* Synchronous verification of issue #3425. |
| 1561 |
*/ |
| 1562 |
@Test |
| 1563 |
public void backpressureInnerDoesntOverflowOuter() { |
| 1564 |
TestSubscriber<GroupedFlowable<Integer, Integer>> ts = new TestSubscriber<>(0L); |
| 1565 |
|
| 1566 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1567 |
|
| 1568 |
pp.groupBy(new Function<Integer, Integer>() { |
| 1569 |
@Override |
| 1570 |
public Integer apply(Integer v) { |
| 1571 |
return v; |
| 1572 |
} |
| 1573 |
}) |
| 1574 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 1575 |
@Override |
| 1576 |
public void accept(GroupedFlowable<Integer, Integer> g) { |
| 1577 |
g.subscribe(); |
| 1578 |
} |
| 1579 |
}) // this will request Long.MAX_VALUE |
| 1580 |
.subscribe(ts) |
| 1581 |
; |
| 1582 |
ts.request(1); |
| 1583 |
|
| 1584 |
pp.onNext(1); |
| 1585 |
|
| 1586 |
ts.assertNotComplete(); |
| 1587 |
ts.assertNoErrors(); |
| 1588 |
ts.assertValueCount(1); |
| 1589 |
} |
| 1590 |
|
| 1591 |
@Test |
| 1592 |
public void backpressureInnerDoesntOverflowOuterMissingBackpressure() { |
| 1593 |
TestSubscriber<GroupedFlowable<Integer, Integer>> ts = new TestSubscriber<>(1); |
| 1594 |
|
| 1595 |
Flowable.fromArray(1, 2) |
| 1596 |
.groupBy(new Function<Integer, Integer>() { |
| 1597 |
@Override |
| 1598 |
public Integer apply(Integer v) { |
| 1599 |
return v; |
| 1600 |
} |
| 1601 |
}) |
| 1602 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 1603 |
@Override |
| 1604 |
public void accept(GroupedFlowable<Integer, Integer> g) { |
| 1605 |
g.subscribe(); |
| 1606 |
} |
| 1607 |
}) // this will request Long.MAX_VALUE |
| 1608 |
.subscribe(ts) |
| 1609 |
; |
| 1610 |
ts.assertValueCount(1) |
| 1611 |
.assertError(MissingBackpressureException.class) |
| 1612 |
.assertNotComplete(); |
| 1613 |
} |
| 1614 |
|
| 1615 |
@Test |
| 1616 |
public void oneGroupInnerRequestsTwiceBuffer() { |
| 1617 |
// FIXME: delayed requesting in groupBy results in group abandonment |
| 1618 |
TestSubscriber<Object> ts1 = new TestSubscriber<>(1L); |
| 1619 |
|
| 1620 |
final TestSubscriber<Object> ts2 = new TestSubscriber<>(0L); |
| 1621 |
|
| 1622 |
Flowable.range(1, Flowable.bufferSize() * 2) |
| 1623 |
.groupBy(new Function<Integer, Object>() { |
| 1624 |
@Override |
| 1625 |
public Object apply(Integer v) { |
| 1626 |
return 1; |
| 1627 |
} |
| 1628 |
}) |
| 1629 |
.doOnNext(new Consumer<GroupedFlowable<Object, Integer>>() { |
| 1630 |
@Override |
| 1631 |
public void accept(GroupedFlowable<Object, Integer> g) { |
| 1632 |
g.subscribe(ts2); |
| 1633 |
} |
| 1634 |
}) |
| 1635 |
.subscribe(ts1); |
| 1636 |
|
| 1637 |
ts1.assertValueCount(1); |
| 1638 |
ts1.assertNoErrors(); |
| 1639 |
ts1.assertNotComplete(); |
| 1640 |
|
| 1641 |
ts2.assertNoValues(); |
| 1642 |
ts2.assertNoErrors(); |
| 1643 |
ts2.assertNotComplete(); |
| 1644 |
|
| 1645 |
ts2.request(Flowable.bufferSize() * 2); |
| 1646 |
|
| 1647 |
ts2.assertValueCount(Flowable.bufferSize() * 2); |
| 1648 |
ts2.assertNoErrors(); |
| 1649 |
ts2.assertComplete(); |
| 1650 |
} |
| 1651 |
|
| 1652 |
@Test |
| 1653 |
public void outerInnerFusion() { |
| 1654 |
final TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1655 |
|
| 1656 |
final TestSubscriberEx<GroupedFlowable<Integer, Integer>> ts2 = new TestSubscriberEx<GroupedFlowable<Integer, Integer>>().setInitialFusionMode(QueueFuseable.ANY); |
| 1657 |
|
| 1658 |
Flowable.range(1, 10).groupBy(new Function<Integer, Integer>() { |
| 1659 |
@Override |
| 1660 |
public Integer apply(Integer v) { |
| 1661 |
return 1; |
| 1662 |
} |
| 1663 |
}, new Function<Integer, Integer>() { |
| 1664 |
@Override |
| 1665 |
public Integer apply(Integer v) { |
| 1666 |
return v + 1; |
| 1667 |
} |
| 1668 |
}) |
| 1669 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 1670 |
@Override |
| 1671 |
public void accept(GroupedFlowable<Integer, Integer> g) { |
| 1672 |
g.subscribe(ts1); |
| 1673 |
} |
| 1674 |
}) |
| 1675 |
.subscribe(ts2); |
| 1676 |
|
| 1677 |
ts1 |
| 1678 |
// FIXME fusion mode causes hangs |
| 1679 |
//.assertFusionMode(QueueFuseable.ASYNC) |
| 1680 |
.assertFusionMode(QueueFuseable.NONE) |
| 1681 |
.assertValues(2, 3, 4, 5, 6, 7, 8, 9, 10, 11) |
| 1682 |
.assertNoErrors() |
| 1683 |
.assertComplete(); |
| 1684 |
|
| 1685 |
ts2 |
| 1686 |
.assertValueCount(1) |
| 1687 |
.assertNoErrors() |
| 1688 |
.assertComplete(); |
| 1689 |
} |
| 1690 |
|
| 1691 |
@Test |
| 1692 |
@SuppressUndeliverable |
| 1693 |
public void keySelectorAndDelayError() { |
| 1694 |
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 1695 |
.groupBy(Functions.<Integer>identity(), true) |
| 1696 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>>() { |
| 1697 |
@Override |
| 1698 |
public Flowable<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1699 |
return g; |
| 1700 |
} |
| 1701 |
}) |
| 1702 |
.test() |
| 1703 |
.assertFailure(TestException.class, 1); |
| 1704 |
} |
| 1705 |
|
| 1706 |
@Test |
| 1707 |
@SuppressUndeliverable |
| 1708 |
public void keyAndValueSelectorAndDelayError() { |
| 1709 |
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 1710 |
.groupBy(Functions.<Integer>identity(), Functions.<Integer>identity(), true) |
| 1711 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>>() { |
| 1712 |
@Override |
| 1713 |
public Flowable<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1714 |
return g; |
| 1715 |
} |
| 1716 |
}) |
| 1717 |
.test() |
| 1718 |
.assertFailure(TestException.class, 1); |
| 1719 |
} |
| 1720 |
|
| 1721 |
@Test |
| 1722 |
public void dispose() { |
| 1723 |
TestHelper.checkDisposed(Flowable.just(1).groupBy(Functions.justFunction(1))); |
| 1724 |
|
| 1725 |
Flowable.just(1) |
| 1726 |
.groupBy(Functions.justFunction(1)) |
| 1727 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 1728 |
@Override |
| 1729 |
public void accept(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1730 |
TestHelper.checkDisposed(g); |
| 1731 |
} |
| 1732 |
}) |
| 1733 |
.test(); |
| 1734 |
} |
| 1735 |
|
| 1736 |
@Test |
| 1737 |
public void reentrantComplete() { |
| 1738 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1739 |
|
| 1740 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1741 |
@Override |
| 1742 |
public void onNext(Integer t) { |
| 1743 |
super.onNext(t); |
| 1744 |
if (t == 1) { |
| 1745 |
pp.onComplete(); |
| 1746 |
} |
| 1747 |
} |
| 1748 |
}; |
| 1749 |
|
| 1750 |
Flowable.merge(pp.groupBy(Functions.justFunction(1))) |
| 1751 |
.subscribe(ts); |
| 1752 |
|
| 1753 |
pp.onNext(1); |
| 1754 |
|
| 1755 |
ts.assertResult(1); |
| 1756 |
} |
| 1757 |
|
| 1758 |
@Test |
| 1759 |
public void reentrantCompleteCancel() { |
| 1760 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1761 |
|
| 1762 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() { |
| 1763 |
@Override |
| 1764 |
public void onNext(Integer t) { |
| 1765 |
super.onNext(t); |
| 1766 |
if (t == 1) { |
| 1767 |
pp.onComplete(); |
| 1768 |
dispose(); |
| 1769 |
} |
| 1770 |
} |
| 1771 |
}; |
| 1772 |
|
| 1773 |
Flowable.merge(pp.groupBy(Functions.justFunction(1))) |
| 1774 |
.subscribe(ts); |
| 1775 |
|
| 1776 |
pp.onNext(1); |
| 1777 |
|
| 1778 |
ts.assertSubscribed().assertValue(1).assertNoErrors().assertNotComplete(); |
| 1779 |
} |
| 1780 |
|
| 1781 |
@Test |
| 1782 |
public void delayErrorSimpleComplete() { |
| 1783 |
Flowable.just(1) |
| 1784 |
.groupBy(Functions.justFunction(1), true) |
| 1785 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 1786 |
.test() |
| 1787 |
.assertResult(1); |
| 1788 |
} |
| 1789 |
|
| 1790 |
@Test |
| 1791 |
public void mainFusionRejected() { |
| 1792 |
TestSubscriberEx<Flowable<Integer>> ts = new TestSubscriberEx<Flowable<Integer>>().setInitialFusionMode(QueueFuseable.SYNC); |
| 1793 |
|
| 1794 |
Flowable.just(1) |
| 1795 |
.groupBy(Functions.justFunction(1)) |
| 1796 |
.subscribe(ts); |
| 1797 |
|
| 1798 |
ts.assertFusionMode(QueueFuseable.NONE) |
| 1799 |
.assertValueCount(1) |
| 1800 |
.assertComplete() |
| 1801 |
.assertNoErrors(); |
| 1802 |
} |
| 1803 |
|
| 1804 |
@Test |
| 1805 |
public void badSource() { |
| 1806 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 1807 |
@Override |
| 1808 |
public Object apply(Flowable<Object> f) throws Exception { |
| 1809 |
return f.groupBy(Functions.justFunction(1)); |
| 1810 |
} |
| 1811 |
}, false, 1, 1, (Object[])null); |
| 1812 |
} |
| 1813 |
|
| 1814 |
@Test |
| 1815 |
public void badRequest() { |
| 1816 |
TestHelper.assertBadRequestReported(Flowable.just(1).hide() |
| 1817 |
.groupBy(Functions.justFunction(1))); |
| 1818 |
} |
| 1819 |
|
| 1820 |
@Test |
| 1821 |
public void badRequestInner() { |
| 1822 |
Flowable.just(1).hide() |
| 1823 |
.groupBy(Functions.justFunction(1)) |
| 1824 |
.doOnNext(g -> { |
| 1825 |
TestHelper.assertBadRequestReported(g); |
| 1826 |
}) |
| 1827 |
.test() |
| 1828 |
.assertNoErrors(); |
| 1829 |
} |
| 1830 |
|
| 1831 |
@Test |
| 1832 |
public void doubleOnSubscribe() { |
| 1833 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<GroupedFlowable<Integer, Object>>>() { |
| 1834 |
@Override |
| 1835 |
public Publisher<GroupedFlowable<Integer, Object>> apply(Flowable<Object> f) throws Exception { |
| 1836 |
return f.groupBy(Functions.justFunction(1)); |
| 1837 |
} |
| 1838 |
}); |
| 1839 |
} |
| 1840 |
|
| 1841 |
@Test |
| 1842 |
public void nullKeyTakeInner() { |
| 1843 |
Flowable.just(1) |
| 1844 |
.groupBy(new Function<Integer, Object>() { |
| 1845 |
@Override |
| 1846 |
public Object apply(Integer v) throws Exception { |
| 1847 |
return null; |
| 1848 |
} |
| 1849 |
}) |
| 1850 |
.flatMap(new Function<GroupedFlowable<Object, Integer>, Publisher<Integer>>() { |
| 1851 |
@Override |
| 1852 |
public Publisher<Integer> apply(GroupedFlowable<Object, Integer> g) throws Exception { |
| 1853 |
return g.take(1); |
| 1854 |
} |
| 1855 |
}) |
| 1856 |
.test() |
| 1857 |
.assertResult(1); |
| 1858 |
} |
| 1859 |
|
| 1860 |
@Test |
| 1861 |
@SuppressUndeliverable |
| 1862 |
public void groupError() { |
| 1863 |
Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 1864 |
.groupBy(Functions.justFunction(1), true) |
| 1865 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 1866 |
@Override |
| 1867 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1868 |
return g.hide(); |
| 1869 |
} |
| 1870 |
}) |
| 1871 |
.test() |
| 1872 |
.assertFailure(TestException.class, 1); |
| 1873 |
} |
| 1874 |
|
| 1875 |
@Test |
| 1876 |
public void groupComplete() { |
| 1877 |
Flowable.just(1) |
| 1878 |
.groupBy(Functions.justFunction(1), true) |
| 1879 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 1880 |
@Override |
| 1881 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1882 |
return g.hide(); |
| 1883 |
} |
| 1884 |
}) |
| 1885 |
.test() |
| 1886 |
.assertResult(1); |
| 1887 |
} |
| 1888 |
|
| 1889 |
@Test |
| 1890 |
public void mapFactoryThrows() { |
| 1891 |
final IOException ex = new IOException("boo"); |
| 1892 |
Function<Consumer<Object>, Map<Integer, Object>> evictingMapFactory = // |
| 1893 |
new Function<Consumer<Object>, Map<Integer, Object>>() { |
| 1894 |
|
| 1895 |
@Override |
| 1896 |
public Map<Integer, Object> apply(final Consumer<Object> notify) throws Exception { |
| 1897 |
throw ex; |
| 1898 |
} |
| 1899 |
}; |
| 1900 |
Flowable.just(1) |
| 1901 |
.groupBy(Functions.<Integer>identity(), Functions.identity(), true, 16, evictingMapFactory) |
| 1902 |
.test() |
| 1903 |
.assertNoValues() |
| 1904 |
.assertError(ex); |
| 1905 |
} |
| 1906 |
// ----------------------------------------------------------------------------------------------------------------------- |
| 1907 |
|
| 1908 |
private static final Function<Integer, Integer> mod5 = new Function<Integer, Integer>() { |
| 1909 |
|
| 1910 |
@Override |
| 1911 |
public Integer apply(Integer n) throws Exception { |
| 1912 |
return n % 5; |
| 1913 |
} |
| 1914 |
}; |
| 1915 |
|
| 1916 |
private static Function<GroupedFlowable<Integer, Integer>, Publisher<? extends Integer>> addCompletedKey( |
| 1917 |
final List<Integer> completed) { |
| 1918 |
return new Function<GroupedFlowable<Integer, Integer>, Publisher<? extends Integer>>() { |
| 1919 |
@Override |
| 1920 |
public Publisher<? extends Integer> apply(final GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1921 |
return g.doOnComplete(new Action() { |
| 1922 |
@Override |
| 1923 |
public void run() throws Exception { |
| 1924 |
completed.add(g.getKey()); |
| 1925 |
} |
| 1926 |
}); |
| 1927 |
} |
| 1928 |
}; |
| 1929 |
} |
| 1930 |
|
| 1931 |
private static final class TestTicker extends Ticker { |
| 1932 |
long tick; |
| 1933 |
|
| 1934 |
@Override |
| 1935 |
public long read() { |
| 1936 |
return tick; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
|
| 1940 |
@Test |
| 1941 |
public void mapFactoryExpiryCompletesGroupedFlowable() { |
| 1942 |
final List<Integer> completed = new CopyOnWriteArrayList<>(); |
| 1943 |
Function<Consumer<Object>, Map<Integer, Object>> evictingMapFactory = createEvictingMapFactorySynchronousOnly(1); |
| 1944 |
PublishSubject<Integer> subject = PublishSubject.create(); |
| 1945 |
TestSubscriberEx<Integer> ts = subject.toFlowable(BackpressureStrategy.BUFFER) |
| 1946 |
.groupBy(Functions.<Integer>identity(), Functions.<Integer>identity(), true, 16, evictingMapFactory) |
| 1947 |
.flatMap(addCompletedKey(completed)) |
| 1948 |
.to(TestHelper.<Integer>testConsumer()); |
| 1949 |
subject.onNext(1); |
| 1950 |
subject.onNext(2); |
| 1951 |
subject.onNext(3); |
| 1952 |
ts.assertValues(1, 2, 3) |
| 1953 |
.assertNotTerminated(); |
| 1954 |
assertEquals(Arrays.asList(1, 2), completed); |
| 1955 |
//ensure coverage of the code that clears the evicted queue |
| 1956 |
subject.onComplete(); |
| 1957 |
ts.assertComplete(); |
| 1958 |
ts.assertValueCount(3); |
| 1959 |
} |
| 1960 |
|
| 1961 |
@Test |
| 1962 |
public void mapFactoryEvictionQueueClearedOnErrorCoverageOnly() { |
| 1963 |
Function<Consumer<Object>, Map<Integer, Object>> evictingMapFactory = createEvictingMapFactorySynchronousOnly(1); |
| 1964 |
PublishSubject<Integer> subject = PublishSubject.create(); |
| 1965 |
TestSubscriber<Integer> ts = subject |
| 1966 |
.toFlowable(BackpressureStrategy.BUFFER) |
| 1967 |
.groupBy(Functions.<Integer>identity(), Functions.<Integer>identity(), true, 16, evictingMapFactory) |
| 1968 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 1969 |
@Override |
| 1970 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 1971 |
return g; |
| 1972 |
} |
| 1973 |
}) |
| 1974 |
.test(); |
| 1975 |
RuntimeException ex = new RuntimeException(); |
| 1976 |
//ensure coverage of the code that clears the evicted queue |
| 1977 |
subject.onError(ex); |
| 1978 |
ts.assertNoValues() |
| 1979 |
.assertError(ex); |
| 1980 |
} |
| 1981 |
|
| 1982 |
@Test |
| 1983 |
public void mapFactoryWithExpiringGuavaCacheDemonstrationCodeForUseInJavadoc() { |
| 1984 |
//javadoc will be a version of this using lambdas and without assertions |
| 1985 |
final List<Integer> completed = new CopyOnWriteArrayList<>(); |
| 1986 |
|
| 1987 |
AtomicReference<Cache<Integer, Object>> cacheOut = new AtomicReference<>(); |
| 1988 |
|
| 1989 |
//size should be less than 5 to notice the effect |
| 1990 |
Function<Consumer<Object>, Map<Integer, Object>> evictingMapFactory = createEvictingMapFactoryGuava(3, cacheOut); |
| 1991 |
int numValues = 1000; |
| 1992 |
TestSubscriber<Integer> ts = |
| 1993 |
Flowable.range(1, numValues) |
| 1994 |
.groupBy(mod5, Functions.<Integer>identity(), true, 16, evictingMapFactory) |
| 1995 |
.flatMap(addCompletedKey(completed)) |
| 1996 |
.test() |
| 1997 |
.assertComplete() |
| 1998 |
; |
| 1999 |
ts.assertValueCount(numValues); |
| 2000 |
//the exact eviction behaviour of the guava cache is not specified so we make some approximate tests |
| 2001 |
assertTrue(completed.size() > numValues * 0.9); |
| 2002 |
|
| 2003 |
cacheOut.get().invalidateAll(); |
| 2004 |
} |
| 2005 |
|
| 2006 |
@Test |
| 2007 |
public void groupByEvictionCancellationOfSource5933() { |
| 2008 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 2009 |
final TestTicker testTicker = new TestTicker(); |
| 2010 |
|
| 2011 |
Function<Consumer<Object>, Map<Integer, Object>> mapFactory = new Function<Consumer<Object>, Map<Integer, Object>>() { |
| 2012 |
@Override |
| 2013 |
public Map<Integer, Object> apply(final Consumer<Object> action) throws Exception { |
| 2014 |
return CacheBuilder.newBuilder() // |
| 2015 |
.expireAfterAccess(5, TimeUnit.SECONDS).removalListener(new RemovalListener<Object, Object>() { |
| 2016 |
@Override |
| 2017 |
public void onRemoval(RemovalNotification<Object, Object> notification) { |
| 2018 |
try { |
| 2019 |
action.accept(notification.getValue()); |
| 2020 |
} catch (Throwable ex) { |
| 2021 |
throw new RuntimeException(ex); |
| 2022 |
} |
| 2023 |
} |
| 2024 |
}).ticker(testTicker) // |
| 2025 |
.<Integer, Object>build().asMap(); |
| 2026 |
} |
| 2027 |
}; |
| 2028 |
|
| 2029 |
final List<String> list = new CopyOnWriteArrayList<>(); |
| 2030 |
Flowable<Integer> stream = source // |
| 2031 |
.doOnCancel(new Action() { |
| 2032 |
@Override |
| 2033 |
public void run() throws Exception { |
| 2034 |
list.add("Source canceled"); |
| 2035 |
} |
| 2036 |
}) |
| 2037 |
.<Integer, Integer>groupBy(Functions.<Integer>identity(), Functions.<Integer>identity(), false, |
| 2038 |
Flowable.bufferSize(), mapFactory) // |
| 2039 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<? extends Integer>>() { |
| 2040 |
@Override |
| 2041 |
public Publisher<? extends Integer> apply(GroupedFlowable<Integer, Integer> group) |
| 2042 |
throws Exception { |
| 2043 |
return group // |
| 2044 |
.doOnComplete(new Action() { |
| 2045 |
@Override |
| 2046 |
public void run() throws Exception { |
| 2047 |
list.add("Group completed"); |
| 2048 |
} |
| 2049 |
}).doOnCancel(new Action() { |
| 2050 |
@Override |
| 2051 |
public void run() throws Exception { |
| 2052 |
list.add("Group canceled"); |
| 2053 |
} |
| 2054 |
}); |
| 2055 |
} |
| 2056 |
}); |
| 2057 |
TestSubscriber<Integer> ts = stream // |
| 2058 |
.doOnCancel(new Action() { |
| 2059 |
@Override |
| 2060 |
public void run() throws Exception { |
| 2061 |
list.add("Outer group by canceled"); |
| 2062 |
} |
| 2063 |
}).test(); |
| 2064 |
|
| 2065 |
// Send 3 in the same group and wait for them to be seen |
| 2066 |
source.onNext(1); |
| 2067 |
source.onNext(1); |
| 2068 |
source.onNext(1); |
| 2069 |
ts.awaitCount(3); |
| 2070 |
|
| 2071 |
// Advance time far enough to evict the group. |
| 2072 |
// NOTE -- Comment this line out to make the test "pass". |
| 2073 |
testTicker.tick = TimeUnit.SECONDS.toNanos(6); |
| 2074 |
|
| 2075 |
// Send more data in the group (triggering eviction and recreation) |
| 2076 |
source.onNext(1); |
| 2077 |
|
| 2078 |
// Wait for the last 2 and then cancel the subscription |
| 2079 |
ts.awaitCount(4); |
| 2080 |
ts.cancel(); |
| 2081 |
|
| 2082 |
// Observe the result. Note that right now the result differs depending on whether eviction occurred or |
| 2083 |
// not. The observed sequence in that case is: Group completed, Outer group by canceled., Group canceled. |
| 2084 |
// The addition of the "Group completed" is actually fine, but the fact that the cancel doesn't reach the |
| 2085 |
// source seems like a bug. Commenting out the setting of "tick" above will produce the "expected" sequence. |
| 2086 |
System.out.println(list); |
| 2087 |
assertTrue(list.contains("Source canceled")); |
| 2088 |
assertEquals(Arrays.asList( |
| 2089 |
"Group completed", // this is here when eviction occurs |
| 2090 |
"Outer group by canceled", |
| 2091 |
"Group canceled", |
| 2092 |
"Source canceled" // This is *not* here when eviction occurs |
| 2093 |
), list); |
| 2094 |
} |
| 2095 |
|
| 2096 |
//not thread safe |
| 2097 |
private static final class SingleThreadEvictingHashMap<K, V> implements Map<K, V> { |
| 2098 |
|
| 2099 |
private final List<K> list = new ArrayList<>(); |
| 2100 |
private final Map<K, V> map = new HashMap<>(); |
| 2101 |
private final int maxSize; |
| 2102 |
private final Consumer<V> evictedListener; |
| 2103 |
|
| 2104 |
SingleThreadEvictingHashMap(int maxSize, Consumer<V> evictedListener) { |
| 2105 |
this.maxSize = maxSize; |
| 2106 |
this.evictedListener = evictedListener; |
| 2107 |
} |
| 2108 |
|
| 2109 |
@Override |
| 2110 |
public int size() { |
| 2111 |
return map.size(); |
| 2112 |
} |
| 2113 |
|
| 2114 |
@Override |
| 2115 |
public boolean isEmpty() { |
| 2116 |
return map.isEmpty(); |
| 2117 |
} |
| 2118 |
|
| 2119 |
@Override |
| 2120 |
public boolean containsKey(Object key) { |
| 2121 |
return map.containsKey(key); |
| 2122 |
} |
| 2123 |
|
| 2124 |
@Override |
| 2125 |
public boolean containsValue(Object value) { |
| 2126 |
return map.containsValue(value); |
| 2127 |
} |
| 2128 |
|
| 2129 |
@Override |
| 2130 |
public V get(Object key) { |
| 2131 |
return map.get(key); |
| 2132 |
} |
| 2133 |
|
| 2134 |
@Override |
| 2135 |
public V put(K key, V value) { |
| 2136 |
list.remove(key); |
| 2137 |
V v; |
| 2138 |
if (maxSize > 0 && list.size() == maxSize) { |
| 2139 |
//remove first |
| 2140 |
K k = list.get(0); |
| 2141 |
list.remove(0); |
| 2142 |
v = map.remove(k); |
| 2143 |
} else { |
| 2144 |
v = null; |
| 2145 |
} |
| 2146 |
list.add(key); |
| 2147 |
V result = map.put(key, value); |
| 2148 |
if (v != null) { |
| 2149 |
try { |
| 2150 |
evictedListener.accept(v); |
| 2151 |
} catch (Throwable e) { |
| 2152 |
throw new RuntimeException(e); |
| 2153 |
} |
| 2154 |
} |
| 2155 |
return result; |
| 2156 |
} |
| 2157 |
|
| 2158 |
@Override |
| 2159 |
public V remove(Object key) { |
| 2160 |
list.remove(key); |
| 2161 |
return map.remove(key); |
| 2162 |
} |
| 2163 |
|
| 2164 |
@Override |
| 2165 |
public void putAll(Map<? extends K, ? extends V> m) { |
| 2166 |
for (Entry<? extends K, ? extends V> entry: m.entrySet()) { |
| 2167 |
put(entry.getKey(), entry.getValue()); |
| 2168 |
} |
| 2169 |
} |
| 2170 |
|
| 2171 |
@Override |
| 2172 |
public void clear() { |
| 2173 |
list.clear(); |
| 2174 |
map.clear(); |
| 2175 |
} |
| 2176 |
|
| 2177 |
@Override |
| 2178 |
public Set<K> keySet() { |
| 2179 |
return map.keySet(); |
| 2180 |
} |
| 2181 |
|
| 2182 |
@Override |
| 2183 |
public Collection<V> values() { |
| 2184 |
return map.values(); |
| 2185 |
} |
| 2186 |
|
| 2187 |
@Override |
| 2188 |
public Set<Entry<K, V>> entrySet() { |
| 2189 |
return map.entrySet(); |
| 2190 |
} |
| 2191 |
} |
| 2192 |
|
| 2193 |
private static Function<Consumer<Object>, Map<Integer, Object>> createEvictingMapFactoryGuava(final int maxSize, |
| 2194 |
final AtomicReference<Cache<Integer, Object>> cacheOut) { |
| 2195 |
Function<Consumer<Object>, Map<Integer, Object>> evictingMapFactory = // |
| 2196 |
new Function<Consumer<Object>, Map<Integer, Object>>() { |
| 2197 |
|
| 2198 |
@Override |
| 2199 |
public Map<Integer, Object> apply(final Consumer<Object> notify) throws Exception { |
| 2200 |
Cache<Integer, Object> cache = CacheBuilder.newBuilder() // |
| 2201 |
.maximumSize(maxSize) // |
| 2202 |
.removalListener(new RemovalListener<Integer, Object>() { |
| 2203 |
@Override |
| 2204 |
public void onRemoval(RemovalNotification<Integer, Object> notification) { |
| 2205 |
try { |
| 2206 |
notify.accept(notification.getValue()); |
| 2207 |
} catch (Throwable e) { |
| 2208 |
throw new RuntimeException(e); |
| 2209 |
} |
| 2210 |
}}) |
| 2211 |
.<Integer, Object> build(); |
| 2212 |
cacheOut.set(cache); |
| 2213 |
return cache.asMap(); |
| 2214 |
}}; |
| 2215 |
return evictingMapFactory; |
| 2216 |
} |
| 2217 |
|
| 2218 |
private static Function<Consumer<Object>, Map<Integer, Object>> createEvictingMapFactorySynchronousOnly(final int maxSize) { |
| 2219 |
Function<Consumer<Object>, Map<Integer, Object>> evictingMapFactory = // |
| 2220 |
new Function<Consumer<Object>, Map<Integer, Object>>() { |
| 2221 |
|
| 2222 |
@Override |
| 2223 |
public Map<Integer, Object> apply(final Consumer<Object> notify) throws Exception { |
| 2224 |
return new SingleThreadEvictingHashMap<>(maxSize, new Consumer<Object>() { |
| 2225 |
@Override |
| 2226 |
public void accept(Object object) { |
| 2227 |
try { |
| 2228 |
notify.accept(object); |
| 2229 |
} catch (Throwable e) { |
| 2230 |
throw new RuntimeException(e); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
}); |
| 2234 |
}}; |
| 2235 |
return evictingMapFactory; |
| 2236 |
} |
| 2237 |
|
| 2238 |
// ----------------------------------------------------------------------------------------------------------------------- |
| 2239 |
|
| 2240 |
@Test |
| 2241 |
public void cancellationOfUpstreamWhenGroupedFlowableCompletes() { |
| 2242 |
final AtomicBoolean cancelled = new AtomicBoolean(); |
| 2243 |
Flowable.just(1).repeat().doOnCancel(new Action() { |
| 2244 |
@Override |
| 2245 |
public void run() throws Exception { |
| 2246 |
cancelled.set(true); |
| 2247 |
} |
| 2248 |
}) |
| 2249 |
.groupBy(Functions.<Integer>identity(), Functions.<Integer>identity()) // |
| 2250 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<? extends Object>>() { |
| 2251 |
@Override |
| 2252 |
public Publisher<? extends Object> apply(GroupedFlowable<Integer, Integer> g) throws Exception { |
| 2253 |
return g.first(0).toFlowable(); |
| 2254 |
} |
| 2255 |
}) |
| 2256 |
.take(4) // |
| 2257 |
.test() // |
| 2258 |
.assertComplete(); |
| 2259 |
assertTrue(cancelled.get()); |
| 2260 |
} |
| 2261 |
|
| 2262 |
@Test |
| 2263 |
public void cancelOverFlatmapRace() { |
| 2264 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 2265 |
|
| 2266 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 2267 |
|
| 2268 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2269 |
|
| 2270 |
pp.groupBy(new Function<Integer, Integer>() { |
| 2271 |
@Override |
| 2272 |
public Integer apply(Integer v) throws Throwable { |
| 2273 |
return v % 10; |
| 2274 |
} |
| 2275 |
}, Functions.<Integer>identity(), false, 2048) |
| 2276 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, GroupedFlowable<Integer, Integer>>() { |
| 2277 |
@Override |
| 2278 |
public GroupedFlowable<Integer, Integer> apply(GroupedFlowable<Integer, Integer> v) |
| 2279 |
throws Throwable { |
| 2280 |
return v; |
| 2281 |
} |
| 2282 |
}) |
| 2283 |
.subscribe(ts); |
| 2284 |
|
| 2285 |
Runnable r1 = new Runnable() { |
| 2286 |
@Override |
| 2287 |
public void run() { |
| 2288 |
for (int j = 0; j < 1000; j++) { |
| 2289 |
pp.onNext(j); |
| 2290 |
} |
| 2291 |
} |
| 2292 |
}; |
| 2293 |
|
| 2294 |
Runnable r2 = new Runnable() { |
| 2295 |
@Override |
| 2296 |
public void run() { |
| 2297 |
ts.cancel(); |
| 2298 |
} |
| 2299 |
}; |
| 2300 |
|
| 2301 |
TestHelper.race(r1, r2); |
| 2302 |
|
| 2303 |
assertFalse("Round " + i, pp.hasSubscribers()); |
| 2304 |
} |
| 2305 |
} |
| 2306 |
|
| 2307 |
@Test |
| 2308 |
public void abandonedGroupsNoDataloss() { |
| 2309 |
final List<GroupedFlowable<Integer, Integer>> groups = new ArrayList<>(); |
| 2310 |
|
| 2311 |
Flowable.range(1, 1000) |
| 2312 |
.groupBy(new Function<Integer, Integer>() { |
| 2313 |
@Override |
| 2314 |
public Integer apply(Integer v) throws Throwable { |
| 2315 |
return v % 10; |
| 2316 |
} |
| 2317 |
}) |
| 2318 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 2319 |
@Override |
| 2320 |
public void accept(GroupedFlowable<Integer, Integer> v) throws Throwable { |
| 2321 |
groups.add(v); |
| 2322 |
} |
| 2323 |
}) |
| 2324 |
.test() |
| 2325 |
.assertValueCount(1000) |
| 2326 |
.assertComplete() |
| 2327 |
.assertNoErrors(); |
| 2328 |
|
| 2329 |
Flowable.concat(groups) |
| 2330 |
.test() |
| 2331 |
.assertValueCount(1000) |
| 2332 |
.assertNoErrors() |
| 2333 |
.assertComplete(); |
| 2334 |
} |
| 2335 |
|
| 2336 |
@Test |
| 2337 |
public void newGroupValueSelectorFails() { |
| 2338 |
TestSubscriber<Object> ts1 = new TestSubscriber<>(); |
| 2339 |
final TestSubscriber<Object> ts2 = new TestSubscriber<>(); |
| 2340 |
|
| 2341 |
Flowable.just(1) |
| 2342 |
.groupBy(Functions.<Integer>identity(), new Function<Integer, Object>() { |
| 2343 |
@Override |
| 2344 |
public Object apply(Integer v) throws Throwable { |
| 2345 |
throw new TestException(); |
| 2346 |
} |
| 2347 |
}) |
| 2348 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Object>>() { |
| 2349 |
@Override |
| 2350 |
public void accept(GroupedFlowable<Integer, Object> g) throws Throwable { |
| 2351 |
g.subscribe(ts2); |
| 2352 |
} |
| 2353 |
}) |
| 2354 |
.subscribe(ts1); |
| 2355 |
|
| 2356 |
ts1.assertValueCount(1) |
| 2357 |
.assertError(TestException.class) |
| 2358 |
.assertNotComplete(); |
| 2359 |
|
| 2360 |
ts2.assertFailure(TestException.class); |
| 2361 |
} |
| 2362 |
|
| 2363 |
@Test |
| 2364 |
public void existingGroupValueSelectorFails() { |
| 2365 |
TestSubscriber<Object> ts1 = new TestSubscriber<>(); |
| 2366 |
final TestSubscriber<Object> ts2 = new TestSubscriber<>(); |
| 2367 |
|
| 2368 |
Flowable.just(1, 2) |
| 2369 |
.groupBy(Functions.justFunction(1), new Function<Integer, Object>() { |
| 2370 |
@Override |
| 2371 |
public Object apply(Integer v) throws Throwable { |
| 2372 |
if (v == 2) { |
| 2373 |
throw new TestException(); |
| 2374 |
} |
| 2375 |
return v; |
| 2376 |
} |
| 2377 |
}) |
| 2378 |
.doOnNext(new Consumer<GroupedFlowable<Integer, Object>>() { |
| 2379 |
@Override |
| 2380 |
public void accept(GroupedFlowable<Integer, Object> g) throws Throwable { |
| 2381 |
g.subscribe(ts2); |
| 2382 |
} |
| 2383 |
}) |
| 2384 |
.subscribe(ts1); |
| 2385 |
|
| 2386 |
ts1.assertValueCount(1) |
| 2387 |
.assertError(TestException.class) |
| 2388 |
.assertNotComplete(); |
| 2389 |
|
| 2390 |
ts2.assertFailure(TestException.class, 1); |
| 2391 |
} |
| 2392 |
|
| 2393 |
@Test |
| 2394 |
public void fusedParallelGroupProcessing() { |
| 2395 |
Flowable.range(0, 500000) |
| 2396 |
.subscribeOn(Schedulers.single()) |
| 2397 |
.groupBy(new Function<Integer, Integer>() { |
| 2398 |
@Override |
| 2399 |
public Integer apply(Integer i) throws Throwable { |
| 2400 |
return i % 2; |
| 2401 |
} |
| 2402 |
}) |
| 2403 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 2404 |
@Override |
| 2405 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) { |
| 2406 |
return g.getKey() == 0 |
| 2407 |
? g |
| 2408 |
.parallel() |
| 2409 |
.runOn(Schedulers.computation()) |
| 2410 |
.map(Functions.<Integer>identity()) |
| 2411 |
.sequential() |
| 2412 |
: g.map(Functions.<Integer>identity()) // no need to use hide |
| 2413 |
; |
| 2414 |
} |
| 2415 |
}) |
| 2416 |
.test() |
| 2417 |
.awaitDone(20, TimeUnit.SECONDS) |
| 2418 |
.assertValueCount(500000) |
| 2419 |
.assertComplete() |
| 2420 |
.assertNoErrors(); |
| 2421 |
} |
| 2422 |
|
| 2423 |
@Test |
| 2424 |
public void valueSelectorCrashAndMissingBackpressure() { |
| 2425 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2426 |
|
| 2427 |
TestSubscriberEx<GroupedFlowable<Integer, Integer>> ts = pp.groupBy(Functions.justFunction(1), new Function<Integer, Integer>() { |
| 2428 |
@Override |
| 2429 |
public Integer apply(Integer t) throws Throwable { |
| 2430 |
throw new TestException(); |
| 2431 |
} |
| 2432 |
}) |
| 2433 |
.subscribeWith(new TestSubscriberEx<>(0L)); |
| 2434 |
|
| 2435 |
assertTrue(pp.offer(1)); |
| 2436 |
|
| 2437 |
ts.assertFailure(MissingBackpressureException.class); |
| 2438 |
|
| 2439 |
assertTrue("" + ts.errors().get(0).getCause(), ts.errors().get(0).getCause() instanceof TestException); |
| 2440 |
} |
| 2441 |
|
| 2442 |
@Test |
| 2443 |
public void fusedGroupClearedOnCancel() { |
| 2444 |
Flowable.just(1) |
| 2445 |
.groupBy(Functions.<Integer>identity()) |
| 2446 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 2447 |
@Override |
| 2448 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Throwable { |
| 2449 |
return g.observeOn(ImmediateThinScheduler.INSTANCE).take(1); |
| 2450 |
} |
| 2451 |
}) |
| 2452 |
.test() |
| 2453 |
.assertResult(1); |
| 2454 |
} |
| 2455 |
|
| 2456 |
@Test |
| 2457 |
public void fusedGroupClearedOnCancelDelayed() { |
| 2458 |
Flowable.range(1, 100) |
| 2459 |
.groupBy(Functions.<Integer, Integer>justFunction(1)) |
| 2460 |
.flatMap(new Function<GroupedFlowable<Integer, Integer>, Publisher<Integer>>() { |
| 2461 |
@Override |
| 2462 |
public Publisher<Integer> apply(GroupedFlowable<Integer, Integer> g) throws Throwable { |
| 2463 |
return g.observeOn(Schedulers.io()) |
| 2464 |
.doOnNext(new Consumer<Integer>() { |
| 2465 |
@Override |
| 2466 |
public void accept(Integer v) throws Throwable { |
| 2467 |
Thread.sleep(100); |
| 2468 |
} |
| 2469 |
}) |
| 2470 |
.take(1); |
| 2471 |
} |
| 2472 |
}) |
| 2473 |
.test() |
| 2474 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2475 |
.assertNoErrors() |
| 2476 |
.assertComplete(); |
| 2477 |
} |
| 2478 |
|
| 2479 |
@Test |
| 2480 |
public void cancelledGroupResumesRequesting() { |
| 2481 |
final List<TestSubscriber<Integer>> tss = new ArrayList<>(); |
| 2482 |
final AtomicInteger counter = new AtomicInteger(); |
| 2483 |
final AtomicBoolean done = new AtomicBoolean(); |
| 2484 |
Flowable.range(1, 1000) |
| 2485 |
.doOnNext(new Consumer<Integer>() { |
| 2486 |
@Override |
| 2487 |
public void accept(Integer v) throws Exception { |
| 2488 |
counter.getAndIncrement(); |
| 2489 |
} |
| 2490 |
}) |
| 2491 |
.groupBy(Functions.justFunction(1)) |
| 2492 |
.subscribe(new Consumer<GroupedFlowable<Integer, Integer>>() { |
| 2493 |
@Override |
| 2494 |
public void accept(GroupedFlowable<Integer, Integer> v) throws Exception { |
| 2495 |
TestSubscriber<Integer> ts = TestSubscriber.create(0L); |
| 2496 |
tss.add(ts); |
| 2497 |
v.subscribe(ts); |
| 2498 |
} |
| 2499 |
}, Functions.emptyConsumer(), new Action() { |
| 2500 |
@Override |
| 2501 |
public void run() throws Exception { |
| 2502 |
done.set(true); |
| 2503 |
} |
| 2504 |
}); |
| 2505 |
|
| 2506 |
while (!done.get()) { |
| 2507 |
tss.remove(0).cancel(); |
| 2508 |
} |
| 2509 |
|
| 2510 |
assertEquals(1000, counter.get()); |
| 2511 |
} |
| 2512 |
|
| 2513 |
@Test |
| 2514 |
public void delayErrorCompleteMoreWorkInGroup() { |
| 2515 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2516 |
|
| 2517 |
TestSubscriber<Integer> ts = pp.groupBy(v -> 1, true) |
| 2518 |
.flatMap(g -> g.doOnNext(v -> { |
| 2519 |
if (v == 1) { |
| 2520 |
pp.onNext(2); |
| 2521 |
pp.onComplete(); |
| 2522 |
} |
| 2523 |
}) |
| 2524 |
) |
| 2525 |
.test() |
| 2526 |
; |
| 2527 |
|
| 2528 |
pp.onNext(1); |
| 2529 |
|
| 2530 |
ts.assertResult(1, 2); |
| 2531 |
} |
| 2532 |
|
| 2533 |
@Test |
| 2534 |
public void groupSyncFusionRejected() { |
| 2535 |
Flowable.just(1) |
| 2536 |
.groupBy(v -> 1) |
| 2537 |
.doOnNext(g -> { |
| 2538 |
g.subscribeWith(new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC)) |
| 2539 |
.assertFuseable() |
| 2540 |
.assertFusionMode(QueueFuseable.NONE); |
| 2541 |
}) |
| 2542 |
.test() |
| 2543 |
.assertComplete(); |
| 2544 |
} |
| 2545 |
|
| 2546 |
@Test |
| 2547 |
public void subscribeAbandonRace() throws Throwable { |
| 2548 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 2549 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2550 |
|
| 2551 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 2552 |
|
| 2553 |
CountDownLatch cdl = new CountDownLatch(1); |
| 2554 |
|
| 2555 |
pp.groupBy(v -> 1) |
| 2556 |
.doOnNext(g -> { |
| 2557 |
TestHelper.raceOther(() -> { |
| 2558 |
g.subscribe(ts); |
| 2559 |
}, cdl); |
| 2560 |
}) |
| 2561 |
.test(); |
| 2562 |
|
| 2563 |
pp.onNext(1); |
| 2564 |
|
| 2565 |
cdl.await(); |
| 2566 |
|
| 2567 |
ts.assertValueCount(1); |
| 2568 |
} |
| 2569 |
} |
| 2570 |
|
| 2571 |
@Test |
| 2572 |
public void issue6974() { |
| 2573 |
|
| 2574 |
FlowableTransformer<Integer, Integer> operation = |
| 2575 |
source -> source.publish(shared -> |
| 2576 |
shared |
| 2577 |
.firstElement() |
| 2578 |
.flatMapPublisher(firstElement -> |
| 2579 |
Flowable.just(firstElement).concatWith(shared) |
| 2580 |
) |
| 2581 |
); |
| 2582 |
|
| 2583 |
issue6974Run(20, 500_000, 20 - 1, 20 * 2, operation, false); |
| 2584 |
|
| 2585 |
issue6974Run(20, 500_000, 20, 20 * 2, operation, false); |
| 2586 |
} |
| 2587 |
|
| 2588 |
static void issue6974Run(int groups, int iterations, int sizeCap, int flatMapConcurrency, |
| 2589 |
FlowableTransformer<Integer, Integer> operation, boolean notifyOnExplicitRevoke) { |
| 2590 |
TestSubscriber<Integer> test = Flowable |
| 2591 |
.range(1, groups) |
| 2592 |
.repeat(iterations / groups) |
| 2593 |
.groupBy(i -> i, i -> i, false, 128, sizeCap(sizeCap, notifyOnExplicitRevoke)) |
| 2594 |
.flatMap(gf -> gf.compose(operation), flatMapConcurrency) |
| 2595 |
.test(); |
| 2596 |
test.awaitDone(5, TimeUnit.SECONDS); |
| 2597 |
test.assertValueCount(iterations); |
| 2598 |
} |
| 2599 |
|
| 2600 |
static <T> Function<Consumer<Object>, Map<T, Object>> sizeCap(int maxCapacity, boolean notifyOnExplicit) { |
| 2601 |
return itemEvictConsumer -> |
| 2602 |
CacheBuilder |
| 2603 |
.newBuilder() |
| 2604 |
.maximumSize(maxCapacity) |
| 2605 |
.removalListener(notification -> { |
| 2606 |
if (notification.getCause() != RemovalCause.EXPLICIT || notifyOnExplicit) { |
| 2607 |
try { |
| 2608 |
itemEvictConsumer.accept(notification.getValue()); |
| 2609 |
} catch (Throwable throwable) { |
| 2610 |
throw new RuntimeException(throwable); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
}) |
| 2614 |
.<T, Object>build().asMap(); |
| 2615 |
} |
| 2616 |
|
| 2617 |
static void issue6974RunPart2(int groupByBufferSize, int flatMapMaxConcurrency, int groups, |
| 2618 |
boolean notifyOnExplicitEviction) { |
| 2619 |
TestSubscriber<Integer> ts = Flowable |
| 2620 |
.range(1, 500_000) |
| 2621 |
.map(i -> i % groups) |
| 2622 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, |
| 2623 |
// set cap too high |
| 2624 |
sizeCap(groups * 100, notifyOnExplicitEviction)) |
| 2625 |
.flatMap(gf -> gf |
| 2626 |
.take(10, TimeUnit.MILLISECONDS) |
| 2627 |
, flatMapMaxConcurrency) |
| 2628 |
.test(); |
| 2629 |
|
| 2630 |
ts |
| 2631 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2632 |
.assertNoErrors() |
| 2633 |
.assertComplete(); |
| 2634 |
} |
| 2635 |
|
| 2636 |
@Test |
| 2637 |
public void issue6974Part2Case1() { |
| 2638 |
final int groups = 20; |
| 2639 |
|
| 2640 |
// Not completed (Timed out), buffer is too small |
| 2641 |
int groupByBufferSize = groups * 2; |
| 2642 |
int flatMapMaxConcurrency = 2 * groups; |
| 2643 |
boolean notifyOnExplicitEviction = false; |
| 2644 |
issue6974RunPart2(groupByBufferSize, flatMapMaxConcurrency, groups, notifyOnExplicitEviction); |
| 2645 |
} |
| 2646 |
|
| 2647 |
@Test |
| 2648 |
public void issue6974Part2Case2() { |
| 2649 |
final int groups = 20; |
| 2650 |
|
| 2651 |
// Timeout... explicit eviction notification makes difference |
| 2652 |
int groupByBufferSize = groups * 30; |
| 2653 |
int flatMapMaxConcurrency = 2 * groups; |
| 2654 |
boolean notifyOnExplicitEviction = true; |
| 2655 |
issue6974RunPart2(groupByBufferSize, flatMapMaxConcurrency, groups, notifyOnExplicitEviction); |
| 2656 |
} |
| 2657 |
|
| 2658 |
/* |
| 2659 |
* Disabled: Takes very long. Run it locally only. |
| 2660 |
@Test |
| 2661 |
public void issue6974Part2Case2Loop() { |
| 2662 |
for (int i = 0; i < 1000; i++) { |
| 2663 |
issue6974Part2Case2(); |
| 2664 |
} |
| 2665 |
} |
| 2666 |
*/ |
| 2667 |
|
| 2668 |
static void issue6974RunPart2NoEvict(int groupByBufferSize, int flatMapMaxConcurrency, int groups, |
| 2669 |
boolean notifyOnExplicitEviction) { |
| 2670 |
|
| 2671 |
Flowable |
| 2672 |
.range(1, 500_000) |
| 2673 |
.map(i -> i % groups) |
| 2674 |
.groupBy(i -> i) |
| 2675 |
.flatMap(gf -> gf |
| 2676 |
.take(10, TimeUnit.MILLISECONDS) |
| 2677 |
, flatMapMaxConcurrency) |
| 2678 |
.subscribeWith(new TestSubscriberEx<>()) |
| 2679 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2680 |
.assertTerminated(); // MBE is possible if the async group closing is slow |
| 2681 |
} |
| 2682 |
|
| 2683 |
@Test |
| 2684 |
public void issue6974Part2Case1NoEvict() { |
| 2685 |
final int groups = 20; |
| 2686 |
|
| 2687 |
// Not completed (Timed out), buffer is too small |
| 2688 |
int groupByBufferSize = groups * 2; |
| 2689 |
int flatMapMaxConcurrency = 2 * groups; |
| 2690 |
boolean notifyOnExplicitEviction = false; |
| 2691 |
issue6974RunPart2NoEvict(groupByBufferSize, flatMapMaxConcurrency, groups, notifyOnExplicitEviction); |
| 2692 |
} |
| 2693 |
|
| 2694 |
/* |
| 2695 |
* Disabled: Takes very long. Run it locally only. |
| 2696 |
@Test |
| 2697 |
public void issue6974Part2Case1NoEvictLoop() { |
| 2698 |
for (int i = 0; i < 1000; i++) { |
| 2699 |
issue6974Part2Case1NoEvict(); |
| 2700 |
} |
| 2701 |
} |
| 2702 |
*/ |
| 2703 |
|
| 2704 |
@Test |
| 2705 |
public void issue6974Part2Case1ObserveOn() { |
| 2706 |
final int groups = 20; |
| 2707 |
|
| 2708 |
// Not completed (Timed out), buffer is too small |
| 2709 |
int groupByBufferSize = groups * 2; |
| 2710 |
int flatMapMaxConcurrency = 2 * groups; |
| 2711 |
boolean notifyOnExplicitEviction = false; |
| 2712 |
|
| 2713 |
Flowable |
| 2714 |
.range(1, 500_000) |
| 2715 |
.map(i -> i % groups) |
| 2716 |
.doOnCancel(() -> { |
| 2717 |
System.out.println("Cancelling upstream"); |
| 2718 |
}) |
| 2719 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, |
| 2720 |
sizeCap(groups * 2, notifyOnExplicitEviction)) |
| 2721 |
.flatMap(gf -> gf |
| 2722 |
.observeOn(Schedulers.computation()) |
| 2723 |
// .take(10) |
| 2724 |
.take(10, TimeUnit.MILLISECONDS) |
| 2725 |
, flatMapMaxConcurrency) |
| 2726 |
.subscribeWith(new TestSubscriberEx<>()) |
| 2727 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2728 |
.assertTerminated(); // MBE is possible if the async group closing is slow |
| 2729 |
} |
| 2730 |
|
| 2731 |
@Test |
| 2732 |
public void issue6974Part2Case1ObserveOnHide() { |
| 2733 |
final int groups = 20; |
| 2734 |
|
| 2735 |
// Not completed (Timed out), buffer is too small |
| 2736 |
int groupByBufferSize = groups * 2; |
| 2737 |
int flatMapMaxConcurrency = 2 * groups; |
| 2738 |
boolean notifyOnExplicitEviction = false; |
| 2739 |
|
| 2740 |
Flowable |
| 2741 |
.range(1, 500_000) |
| 2742 |
.map(i -> i % groups) |
| 2743 |
.doOnCancel(() -> System.out.println("Cancelling upstream")) |
| 2744 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, |
| 2745 |
sizeCap(groups * 2, notifyOnExplicitEviction)) |
| 2746 |
.flatMap(gf -> gf |
| 2747 |
.hide() |
| 2748 |
.observeOn(Schedulers.computation()) |
| 2749 |
// .take(10) |
| 2750 |
.take(10, TimeUnit.MILLISECONDS) |
| 2751 |
, flatMapMaxConcurrency) |
| 2752 |
.subscribeWith(new TestSubscriberEx<>()) |
| 2753 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2754 |
.assertTerminated(); // MBE is possible if the async group closing is slow |
| 2755 |
} |
| 2756 |
|
| 2757 |
@Test |
| 2758 |
public void issue6974Part2Case1ObserveOnNoCap() { |
| 2759 |
final int groups = 20; |
| 2760 |
|
| 2761 |
// Not completed (Timed out), buffer is too small |
| 2762 |
int flatMapMaxConcurrency = 1_000_000; |
| 2763 |
|
| 2764 |
Flowable |
| 2765 |
.range(1, 500_000) |
| 2766 |
.map(i -> i % groups) |
| 2767 |
.doOnRequest(v -> { |
| 2768 |
System.out.println("Source: " + v); |
| 2769 |
}) |
| 2770 |
.groupBy(i -> i) |
| 2771 |
.flatMap(gf -> gf |
| 2772 |
.observeOn(Schedulers.computation()) |
| 2773 |
// .take(10) |
| 2774 |
.take(10, TimeUnit.MILLISECONDS) |
| 2775 |
, flatMapMaxConcurrency) |
| 2776 |
.test() |
| 2777 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2778 |
.assertNoErrors() |
| 2779 |
.assertComplete(); |
| 2780 |
} |
| 2781 |
|
| 2782 |
@Test |
| 2783 |
public void issue6974Part2Case1ObserveOnNoCapHide() { |
| 2784 |
final int groups = 20; |
| 2785 |
|
| 2786 |
// Not completed (Timed out), buffer is too small |
| 2787 |
int flatMapMaxConcurrency = 1_000_000; |
| 2788 |
|
| 2789 |
Flowable |
| 2790 |
.range(1, 500_000) |
| 2791 |
.map(i -> i % groups) |
| 2792 |
.doOnRequest(v -> { |
| 2793 |
System.out.println("Source: " + v); |
| 2794 |
}) |
| 2795 |
.groupBy(i -> i) |
| 2796 |
.flatMap(gf -> gf |
| 2797 |
.hide() |
| 2798 |
.observeOn(Schedulers.computation()) |
| 2799 |
// .take(10) |
| 2800 |
.take(10, TimeUnit.MILLISECONDS) |
| 2801 |
, flatMapMaxConcurrency) |
| 2802 |
.test() |
| 2803 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2804 |
.assertNoErrors() |
| 2805 |
.assertComplete(); |
| 2806 |
} |
| 2807 |
|
| 2808 |
/* |
| 2809 |
* Disabled: Takes very long. Run it locally only. |
| 2810 |
@Test |
| 2811 |
public void issue6974Part2Case1ObserveOnNoCapHideLoop() { |
| 2812 |
for (int i = 0; i < 100; i++) { |
| 2813 |
issue6974Part2Case1ObserveOnNoCapHide(); |
| 2814 |
} |
| 2815 |
} |
| 2816 |
*/ |
| 2817 |
|
| 2818 |
@Test |
| 2819 |
public void issue6974Part2Case1ObserveOnConditional() { |
| 2820 |
final int groups = 20; |
| 2821 |
|
| 2822 |
// Not completed (Timed out), buffer is too small |
| 2823 |
int groupByBufferSize = groups * 2; |
| 2824 |
int flatMapMaxConcurrency = 2 * groups; |
| 2825 |
boolean notifyOnExplicitEviction = false; |
| 2826 |
|
| 2827 |
Flowable |
| 2828 |
.range(1, 500_000) |
| 2829 |
.map(i -> i % groups) |
| 2830 |
.doOnCancel(() -> System.out.println("Cancelling upstream")) |
| 2831 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, |
| 2832 |
sizeCap(groups * 2, notifyOnExplicitEviction)) |
| 2833 |
.flatMap(gf -> gf |
| 2834 |
.observeOn(Schedulers.computation()) |
| 2835 |
.filter(v -> true) |
| 2836 |
// .take(10) |
| 2837 |
.take(10, TimeUnit.MILLISECONDS) |
| 2838 |
, flatMapMaxConcurrency) |
| 2839 |
.subscribeWith(new TestSubscriberEx<>()) |
| 2840 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2841 |
.assertTerminated(); // MBE is possible if the async group closing is slow |
| 2842 |
} |
| 2843 |
|
| 2844 |
@Test |
| 2845 |
public void issue6974Part2Case1ObserveOnConditionalHide() { |
| 2846 |
final int groups = 20; |
| 2847 |
|
| 2848 |
// Not completed (Timed out), buffer is too small |
| 2849 |
int groupByBufferSize = groups * 2; |
| 2850 |
int flatMapMaxConcurrency = 2 * groups; |
| 2851 |
boolean notifyOnExplicitEviction = false; |
| 2852 |
|
| 2853 |
Flowable |
| 2854 |
.range(1, 500_000) |
| 2855 |
.map(i -> i % groups) |
| 2856 |
.doOnCancel(() -> System.out.println("Cancelling upstream")) |
| 2857 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, |
| 2858 |
sizeCap(groups * 2, notifyOnExplicitEviction)) |
| 2859 |
.flatMap(gf -> gf |
| 2860 |
.hide() |
| 2861 |
.observeOn(Schedulers.computation()) |
| 2862 |
.filter(v -> true) |
| 2863 |
// .take(10) |
| 2864 |
.take(10, TimeUnit.MILLISECONDS) |
| 2865 |
, flatMapMaxConcurrency) |
| 2866 |
.subscribeWith(new TestSubscriberEx<>()) |
| 2867 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2868 |
.assertTerminated(); // MBE is possible if the async group closing is slow |
| 2869 |
} |
| 2870 |
|
| 2871 |
/* |
| 2872 |
* Disabled: Takes very long. Run it locally only. |
| 2873 |
@Test |
| 2874 |
public void issue6974Part2Case1ObserveOnHideLoop() { |
| 2875 |
for (int i = 0; i < 100; i++) { |
| 2876 |
issue6974Part2Case1ObserveOnHide(); |
| 2877 |
} |
| 2878 |
} |
| 2879 |
*/ |
| 2880 |
|
| 2881 |
static <T> Function<Consumer<Object>, ConcurrentMap<T, Object>> ttlCapGuava(Duration ttl) { |
| 2882 |
return itemEvictConsumer -> |
| 2883 |
CacheBuilder |
| 2884 |
.newBuilder() |
| 2885 |
.expireAfterWrite(ttl) |
| 2886 |
.removalListener(n -> { |
| 2887 |
if (n.getCause() != com.google.common.cache.RemovalCause.EXPLICIT) { |
| 2888 |
try { |
| 2889 |
itemEvictConsumer.accept(n.getValue()); |
| 2890 |
} catch (Throwable throwable) { |
| 2891 |
throw new RuntimeException(throwable); |
| 2892 |
} |
| 2893 |
} |
| 2894 |
}).<T, Object>build().asMap(); |
| 2895 |
} |
| 2896 |
|
| 2897 |
@Test |
| 2898 |
public void issue6982Case1() { |
| 2899 |
final int groups = 20; |
| 2900 |
|
| 2901 |
int groupByBufferSize = 2; |
| 2902 |
int flatMapMaxConcurrency = 200 * groups; |
| 2903 |
|
| 2904 |
// ~50% of executions - Not completed (latch = 1, values = 500000, errors = 0, completions = 0, timeout!, |
| 2905 |
// disposed!) |
| 2906 |
|
| 2907 |
Flowable |
| 2908 |
.range(1, 500_000) |
| 2909 |
.map(i -> i % groups) |
| 2910 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, ttlCapGuava(Duration.ofMillis(10))) |
| 2911 |
.flatMap(gf -> gf.observeOn(Schedulers.computation()), flatMapMaxConcurrency) |
| 2912 |
.test() |
| 2913 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2914 |
.assertNoErrors() |
| 2915 |
.assertComplete(); |
| 2916 |
} |
| 2917 |
|
| 2918 |
/* |
| 2919 |
* Disabled: Takes very long. Run it locally only. |
| 2920 |
@Test |
| 2921 |
public void issue6982Case1Loop() { |
| 2922 |
for (int i = 0; i < 200; i++) { |
| 2923 |
System.out.println("issue6982Case1Loop " + i); |
| 2924 |
issue6982Case1(); |
| 2925 |
} |
| 2926 |
} |
| 2927 |
*/ |
| 2928 |
|
| 2929 |
@Test |
| 2930 |
public void issue6982Case2() { |
| 2931 |
final int groups = 20; |
| 2932 |
|
| 2933 |
int groupByBufferSize = groups * 30; |
| 2934 |
int flatMapMaxConcurrency = groups * 500; |
| 2935 |
// Always : Not completed (latch = 1, values = 14100, errors = 0, completions = 0, timeout!, disposed!) |
| 2936 |
|
| 2937 |
Flowable |
| 2938 |
.range(1, 500_000) |
| 2939 |
.map(i -> i % groups) |
| 2940 |
.groupBy(i -> i, i -> i, false, groupByBufferSize, ttlCapGuava(Duration.ofMillis(10))) |
| 2941 |
.flatMap(gf -> gf.observeOn(Schedulers.computation()), flatMapMaxConcurrency) |
| 2942 |
.test() |
| 2943 |
.awaitDone(5, TimeUnit.SECONDS) |
| 2944 |
.assertNoErrors() |
| 2945 |
.assertComplete(); |
| 2946 |
} |
| 2947 |
|
| 2948 |
/* |
| 2949 |
* Disabled: Takes very long. Run it locally only. |
| 2950 |
@Test |
| 2951 |
public void issue6982Case2Loop() { |
| 2952 |
for (int i = 0; i < 200; i++) { |
| 2953 |
System.out.println("issue6982Case2Loop " + i); |
| 2954 |
issue6982Case2(); |
| 2955 |
} |
| 2956 |
} |
| 2957 |
*/ |
| 2958 |
} |