1010import java .util .concurrent .TimeUnit ;
1111import java .util .concurrent .atomic .AtomicInteger ;
1212import java .util .stream .IntStream ;
13- import java .util .stream .Stream ;
1413
1514import jakarta .enterprise .context .ApplicationScoped ;
1615
2625
2726public class ThrottledConcurrencyTest extends KafkaCompanionTestBase {
2827
28+ int partitions = 10 ;
29+ int concurrency = 10 ;
30+ int recordsParPartition = 100 ;
31+
2932 @ Test
3033 public void testUnorderedParallelProcessing () {
3134 // Create topic and produce messages with different keys
32- companion .topics ().createAndWait (topic , 5 );
35+ companion .topics ().createAndWait (topic , partitions );
3336
3437 // Produce messages to different partitions
3538 companion .produceStrings ()
36- .fromRecords (IntStream .range (0 , 10 ).boxed ().flatMap (i -> Stream .of (
37- new ProducerRecord <>(topic , 0 , "A" , "A-" + i ),
38- new ProducerRecord <>(topic , 1 , "B" , "B-" + i ),
39- new ProducerRecord <>(topic , 2 , "C" , "C-" + i ),
40- new ProducerRecord <>(topic , 3 , "D" , "D-" + i ),
41- new ProducerRecord <>(topic , 4 , "E" , "E-" + i ))).toList ())
39+ .fromRecords (IntStream .range (0 , recordsParPartition ).boxed ().flatMap (i -> IntStream .range (0 , partitions ).boxed ()
40+ .map (p -> new ProducerRecord <>(topic , p , "key-" + p , "value-" + i )))
41+ .toList ())
4242 .awaitCompletion ();
4343
4444 MapBasedConfig config = kafkaConfig ("mp.messaging.incoming.unordered" )
@@ -49,18 +49,20 @@ public void testUnorderedParallelProcessing() {
4949 .with ("key.deserializer" , StringDeserializer .class .getName ())
5050 .with ("commit-strategy" , "throttled" )
5151 .withPrefix ("" )
52- .with ("smallrye.messaging.worker.my-pool.max-concurrency" , 5 );
52+ .with ("smallrye.messaging.worker.my-pool.max-concurrency" , concurrency );
5353
5454 UnorderedParallelConsumer app = runApplication (config , UnorderedParallelConsumer .class );
5555 long start = System .currentTimeMillis ();
5656 System .out .println ("Started processing messages " + start );
5757
5858 // Wait for all messages to be processed
59- await ().atMost (1 , TimeUnit .MINUTES ).untilAsserted (() -> assertThat (app .received ()).hasSize (50 ));
59+ await ().atMost (2 , TimeUnit .MINUTES )
60+ .untilAsserted (() -> assertThat (app .received ())
61+ .hasSize (partitions * recordsParPartition ));
6062 long duration = System .currentTimeMillis () - start ;
6163 System .out .println ("Processing duration: " + duration + " ms" );
6264
63- assertThat (app .maxConcurrency ()).isEqualTo (5 );
65+ assertThat (app .maxConcurrency ()).isEqualTo (concurrency );
6466 }
6567
6668 @ ApplicationScoped
@@ -93,16 +95,13 @@ public int maxConcurrency() {
9395 @ Test
9496 public void testOrderedByKeyProcessing () {
9597 // Create topic and produce messages with different keys
96- companion .topics ().createAndWait (topic , 5 );
98+ companion .topics ().createAndWait (topic , partitions );
9799
98100 // Produce messages to different keys
99101 companion .produceStrings ()
100- .fromRecords (IntStream .range (0 , 10 ).boxed ().flatMap (i -> Stream .of (
101- new ProducerRecord <>(topic , 0 , "A" , "A-" + i ),
102- new ProducerRecord <>(topic , 1 , "B" , "B-" + i ),
103- new ProducerRecord <>(topic , 2 , "C" , "C-" + i ),
104- new ProducerRecord <>(topic , 3 , "D" , "D-" + i ),
105- new ProducerRecord <>(topic , 4 , "E" , "E-" + i ))).toList ())
102+ .fromRecords (IntStream .range (0 , recordsParPartition ).boxed ().flatMap (i -> IntStream .range (0 , partitions ).boxed ()
103+ .map (p -> new ProducerRecord <>(topic , p , "key-" + p , "value-" + i )))
104+ .toList ())
106105 .awaitCompletion ();
107106
108107 MapBasedConfig config = kafkaConfig ("mp.messaging.incoming.key-ordered" )
@@ -112,25 +111,27 @@ public void testOrderedByKeyProcessing() {
112111 .with ("value.deserializer" , StringDeserializer .class .getName ())
113112 .with ("key.deserializer" , StringDeserializer .class .getName ())
114113 .with ("throttled.processing-order" , "ordered_by_key" )
115- .with ("commit-strategy" , "throttled" );
114+ .with ("commit-strategy" , "throttled" )
115+ .withPrefix ("" )
116+ .with ("smallrye.messaging.worker.my-pool.max-concurrency" , concurrency );
116117
117118 OrderedByKeyParallelConsumer app = runApplication (config , OrderedByKeyParallelConsumer .class );
118119 long start = System .currentTimeMillis ();
119120 System .out .println ("Started processing messages " + start );
120121
121122 // Wait for all messages to be processed
122- await ().atMost (1 , TimeUnit .MINUTES ).untilAsserted (() -> assertThat (app .received ()).hasSize (50 ));
123+ await ().atMost (2 , TimeUnit .MINUTES )
124+ .untilAsserted (() -> assertThat (app .received ())
125+ .hasSize (partitions * recordsParPartition ));
123126 long duration = System .currentTimeMillis () - start ;
124127 System .out .println ("Processing duration: " + duration + " ms" );
125128
126129 // With ordered-by-key, messages with the same key should be processed sequentially
127130 // So max concurrent processing per key should be 1
128- assertThat (app .keyMaxCounter ("A" )).hasValue (1 );
129- assertThat (app .keyMaxCounter ("B" )).hasValue (1 );
130- assertThat (app .keyMaxCounter ("C" )).hasValue (1 );
131- assertThat (app .keyMaxCounter ("D" )).hasValue (1 );
132- assertThat (app .keyMaxCounter ("E" )).hasValue (1 );
133- assertThat (app .maxConcurrency ()).isEqualTo (5 );
131+ for (int i = 0 ; i < partitions ; i ++) {
132+ assertThat (app .keyMaxCounter ("key-" + i )).hasValue (1 );
133+ }
134+ assertThat (app .maxConcurrency ()).isEqualTo (partitions );
134135 }
135136
136137 @ ApplicationScoped
@@ -177,16 +178,13 @@ public int maxConcurrency() {
177178 @ Test
178179 public void testOrderedByPartitionProcessing () {
179180 // Create topic with 2 partitions
180- companion .topics ().createAndWait (topic , 5 );
181+ companion .topics ().createAndWait (topic , partitions );
181182
182183 // Produce messages to different partitions
183184 companion .produceStrings ()
184- .fromRecords (IntStream .range (0 , 10 ).boxed ().flatMap (i -> Stream .of (
185- new ProducerRecord <>(topic , 0 , "key-" + i , "p0-" + i ),
186- new ProducerRecord <>(topic , 1 , "key-" + i , "p1-" + i ),
187- new ProducerRecord <>(topic , 2 , "key-" + i , "p2-" + i ),
188- new ProducerRecord <>(topic , 3 , "key-" + i , "p3-" + i ),
189- new ProducerRecord <>(topic , 4 , "key-" + i , "p4-" + i ))).toList ())
185+ .fromRecords (IntStream .range (0 , recordsParPartition ).boxed ().flatMap (i -> IntStream .range (0 , partitions ).boxed ()
186+ .map (p -> new ProducerRecord <>(topic , p , "key-" + p , "value-" + i )))
187+ .toList ())
190188 .awaitCompletion ();
191189
192190 MapBasedConfig config = kafkaConfig ("mp.messaging.incoming.partition-ordered" )
@@ -196,25 +194,27 @@ public void testOrderedByPartitionProcessing() {
196194 .with ("value.deserializer" , StringDeserializer .class .getName ())
197195 .with ("key.deserializer" , StringDeserializer .class .getName ())
198196 .with ("throttled.processing-order" , "ordered_by_partition" )
199- .with ("commit-strategy" , "throttled" );
197+ .with ("commit-strategy" , "throttled" )
198+ .withPrefix ("" )
199+ .with ("smallrye.messaging.worker.my-pool.max-concurrency" , concurrency );
200200
201201 OrderedByPartitionParallelConsumer app = runApplication (config , OrderedByPartitionParallelConsumer .class );
202202 long start = System .currentTimeMillis ();
203203 System .out .println ("Started processing messages " + start );
204204
205205 // Wait for all messages to be processed
206- await ().atMost (1 , TimeUnit .MINUTES ).untilAsserted (() -> assertThat (app .received ()).hasSize (50 ));
206+ await ().atMost (2 , TimeUnit .MINUTES )
207+ .untilAsserted (() -> assertThat (app .received ())
208+ .hasSize (partitions * recordsParPartition ));
207209 long duration = System .currentTimeMillis () - start ;
208210 System .out .println ("Processing duration: " + duration + " ms" );
209211
210212 // With ordered-by-partition, messages from the same partition should be processed sequentially
211213 // So max concurrent processing per partition should be 1
212- assertThat (app .partitionMaxCounter (0 )).hasValue (1 );
213- assertThat (app .partitionMaxCounter (1 )).hasValue (1 );
214- assertThat (app .partitionMaxCounter (2 )).hasValue (1 );
215- assertThat (app .partitionMaxCounter (3 )).hasValue (1 );
216- assertThat (app .partitionMaxCounter (4 )).hasValue (1 );
217- assertThat (app .maxConcurrency ()).isEqualTo (5 );
214+ for (int i = 0 ; i < partitions ; i ++) {
215+ assertThat (app .partitionMaxCounter (i )).hasValue (1 );
216+ }
217+ assertThat (app .maxConcurrency ()).isEqualTo (partitions );
218218 }
219219
220220 @ ApplicationScoped
0 commit comments