Skip to content

Commit c9f98ce

Browse files
committed
Merge #2450 into 3.4.2
2 parents c14a763 + 2ff31b3 commit c9f98ce

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

reactor-core/src/main/java/reactor/core/publisher/FluxGroupBy.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ public K key() {
515515
volatile boolean outputFused;
516516

517517
int produced;
518+
boolean isFirstRequest = true;
518519

519520
UnicastGroupedFlux(K key,
520521
Queue<V> queue,
@@ -572,7 +573,16 @@ void drainRegular(Subscriber<? super V> a) {
572573
if (e != 0) {
573574
GroupByMain<?, K, V> main = parent;
574575
if (main != null) {
575-
main.s.request(e);
576+
if (this.isFirstRequest) {
577+
this.isFirstRequest = false;
578+
long toRequest = e - 1;
579+
580+
if (toRequest > 0) {
581+
main.s.request(toRequest);
582+
}
583+
} else {
584+
main.s.request(e);
585+
}
576586
}
577587
if (r != Long.MAX_VALUE) {
578588
REQUESTED.addAndGet(this, -e);
@@ -751,7 +761,16 @@ void tryReplenish() {
751761
produced = 0;
752762
GroupByMain<?, K, V> main = parent;
753763
if (main != null) {
754-
main.s.request(p);
764+
if (this.isFirstRequest) {
765+
this.isFirstRequest = false;
766+
p--;
767+
768+
if (p > 0) {
769+
main.s.request(p);
770+
}
771+
} else {
772+
main.s.request(p);
773+
}
755774
}
756775
}
757776
}

reactor-core/src/test/java/reactor/core/publisher/FluxGroupByTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,37 @@ public void twoGroupsLongAsyncMergeHidden() {
374374
.assertComplete();
375375
}
376376

377+
@Test
378+
public void twoGroupsLongAsyncMergeHidden2() {
379+
ForkJoinPool forkJoinPool = new ForkJoinPool();
380+
381+
for (int j = 0; j < 100; j++) {
382+
AssertSubscriber<Long> ts = AssertSubscriber.create();
383+
AtomicLong dropped = new AtomicLong();
384+
385+
Hooks.onNextDropped(__ -> dropped.incrementAndGet());
386+
try {
387+
final int total = 100_000;
388+
Flux.range(0, total)
389+
.groupBy(i -> (i / 2d) * 2d, 42)
390+
.flatMap(it -> it.take(1)
391+
.hide(), 2)
392+
.publishOn(Schedulers.fromExecutorService(forkJoinPool), 2)
393+
.count()
394+
.subscribe(ts);
395+
396+
ts.await(Duration.ofSeconds(50));
397+
398+
ts.assertValues(total - dropped.get())
399+
.assertNoError()
400+
.assertComplete();
401+
402+
} finally {
403+
Hooks.resetOnNextDropped();
404+
}
405+
}
406+
}
407+
377408
@Test
378409
public void twoGroupsConsumeWithSubscribe() {
379410
ForkJoinPool forkJoinPool = new ForkJoinPool();

0 commit comments

Comments
 (0)