|
14 | 14 | import java.util.Collections; |
15 | 15 | import java.util.List; |
16 | 16 | import java.util.NoSuchElementException; |
| 17 | +import java.util.concurrent.ExecutorService; |
| 18 | +import java.util.concurrent.Executors; |
17 | 19 | import java.util.concurrent.Flow.Subscriber; |
18 | 20 | import java.util.concurrent.Flow.Subscription; |
19 | 21 | import java.util.concurrent.atomic.AtomicBoolean; |
20 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
21 | 24 | import java.util.concurrent.atomic.AtomicReference; |
22 | 25 | import java.util.function.Consumer; |
23 | 26 |
|
@@ -1037,4 +1040,45 @@ public void rejectGroupByBadPrefetch() { |
1037 | 1040 | assertThrows(IllegalArgumentException.class, |
1038 | 1041 | () -> Multi.createFrom().range(1, 10).group().by(i -> i % 2, 0L)); |
1039 | 1042 | } |
| 1043 | + |
| 1044 | + @Test |
| 1045 | + void testUpstreamRequestsNotBlownOutOfProportion() { |
| 1046 | + ExecutorService executor = Executors.newFixedThreadPool(10); |
| 1047 | + AtomicLong requestCounter = new AtomicLong(0); |
| 1048 | + AtomicLong itemCounter = new AtomicLong(0); |
| 1049 | + AtomicReference<MultiEmitter<? super Integer>> e = new AtomicReference<>(); |
| 1050 | + |
| 1051 | + Multi.createFrom().<Integer> emitter(e::set) |
| 1052 | + .onRequest().invoke(requestCounter::addAndGet) |
| 1053 | + .group().by(i -> i / 10) |
| 1054 | + .onItem().transformToMulti(g -> g.map(i -> g.key() + " : " + i) |
| 1055 | + .emitOn(executor) |
| 1056 | + .invoke(s -> { |
| 1057 | + try { |
| 1058 | + Thread.sleep(100); |
| 1059 | + itemCounter.incrementAndGet(); |
| 1060 | + } catch (InterruptedException ex) { |
| 1061 | + throw new RuntimeException(ex); |
| 1062 | + } |
| 1063 | + })) |
| 1064 | + .merge() |
| 1065 | + .subscribe().with(s -> { |
| 1066 | + }); |
| 1067 | + |
| 1068 | + int itemCount = 100; |
| 1069 | + MultiEmitter<? super Integer> emitter = e.get(); |
| 1070 | + new Thread(() -> { |
| 1071 | + int i = 0; |
| 1072 | + while (true) { |
| 1073 | + if (emitter.requested() > 0) { |
| 1074 | + emitter.emit(i); |
| 1075 | + i++; |
| 1076 | + } |
| 1077 | + } |
| 1078 | + }).start(); |
| 1079 | + |
| 1080 | + await().untilAsserted(() -> assertThat(itemCounter).hasValueGreaterThanOrEqualTo(itemCount)); |
| 1081 | + System.out.println(requestCounter.get()); |
| 1082 | + assertThat(requestCounter.get()).isLessThan(10000L); // this should not blow up |
| 1083 | + } |
1040 | 1084 | } |
0 commit comments