1
1
/*
2
- * Copyright 2019-2022 the original author or authors.
2
+ * Copyright 2019-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
import static org .mockito .Mockito .times ;
28
28
import static org .mockito .Mockito .verify ;
29
29
30
+ import java .util .ArrayList ;
30
31
import java .util .Arrays ;
32
+ import java .util .HashMap ;
31
33
import java .util .List ;
34
+ import java .util .Map ;
32
35
import java .util .concurrent .atomic .AtomicBoolean ;
33
36
import java .util .concurrent .atomic .AtomicReference ;
34
37
35
38
import org .apache .kafka .clients .consumer .Consumer ;
36
39
import org .apache .kafka .clients .consumer .ConsumerGroupMetadata ;
37
40
import org .apache .kafka .clients .consumer .ConsumerRecord ;
41
+ import org .apache .kafka .clients .consumer .ConsumerRecords ;
38
42
import org .apache .kafka .common .TopicPartition ;
39
43
import org .junit .jupiter .api .Test ;
40
44
import org .mockito .InOrder ;
@@ -100,8 +104,8 @@ void testClassifier() {
100
104
}
101
105
102
106
@ Test
103
- void testBatchBackOff () {
104
- AtomicReference < ConsumerRecord <?, ?>> recovered = new AtomicReference <>();
107
+ void testBackOffNoBatchRecover () {
108
+
105
109
@ SuppressWarnings ("unchecked" )
106
110
KafkaOperations <String , String > template = mock (KafkaOperations .class );
107
111
given (template .isTransactional ()).willReturn (true );
@@ -118,37 +122,25 @@ void testBatchBackOff() {
118
122
ConsumerRecord <String , String > record1 = new ConsumerRecord <>("foo" , 0 , 0L , "foo" , "bar" );
119
123
ConsumerRecord <String , String > record2 = new ConsumerRecord <>("foo" , 1 , 1L , "foo" , "bar" );
120
124
List <ConsumerRecord <String , String >> records = Arrays .asList (record1 , record2 );
125
+ Map <TopicPartition , List <ConsumerRecord <String , String >>> map = new HashMap <>();
126
+ records .forEach (rec -> map .computeIfAbsent (new TopicPartition (rec .topic (), rec .partition ()),
127
+ tp -> new ArrayList <>()).add (rec ));
128
+ ConsumerRecords <String , String > consumerRecords = new ConsumerRecords <>(map );
121
129
IllegalStateException illegalState = new IllegalStateException ();
122
130
@ SuppressWarnings ("unchecked" )
123
131
Consumer <String , String > consumer = mock (Consumer .class );
124
132
given (consumer .groupMetadata ()).willReturn (new ConsumerGroupMetadata ("foo" ));
125
133
MessageListenerContainer container = mock (MessageListenerContainer .class );
126
134
given (container .isRunning ()).willReturn (true );
127
- processor .process ( records , consumer , container , illegalState , false , EOSMode .V2 );
128
- processor .process ( records , consumer , container , illegalState , false , EOSMode .V2 );
135
+ processor .processBatch ( consumerRecords , records , consumer , container , illegalState , false , EOSMode .V2 );
136
+ processor .processBatch ( consumerRecords , records , consumer , container , illegalState , false , EOSMode .V2 );
129
137
verify (backOff , times (2 )).start ();
130
138
verify (execution .get (), times (2 )).nextBackOff ();
131
139
processor .clearThreadState ();
132
- processor .process ( records , consumer , container , illegalState , false , EOSMode .V2 );
140
+ processor .processBatch ( consumerRecords , records , consumer , container , illegalState , false , EOSMode .V2 );
133
141
verify (backOff , times (3 )).start ();
134
142
}
135
143
136
- void testEarlyExitBackOff () {
137
- DefaultAfterRollbackProcessor <String , String > processor = new DefaultAfterRollbackProcessor <>(
138
- new FixedBackOff (1 , 10_000 ));
139
- @ SuppressWarnings ("unchecked" )
140
- Consumer <String , String > consumer = mock (Consumer .class );
141
- ConsumerRecord <String , String > record1 = new ConsumerRecord <>("foo" , 0 , 0L , "foo" , "bar" );
142
- ConsumerRecord <String , String > record2 = new ConsumerRecord <>("foo" , 1 , 1L , "foo" , "bar" );
143
- List <ConsumerRecord <String , String >> records = Arrays .asList (record1 , record2 );
144
- IllegalStateException illegalState = new IllegalStateException ();
145
- MessageListenerContainer container = mock (MessageListenerContainer .class );
146
- given (container .isRunning ()).willReturn (false );
147
- long t1 = System .currentTimeMillis ();
148
- processor .process (records , consumer , container , illegalState , true , EOSMode .V2 );
149
- assertThat (System .currentTimeMillis () < t1 + 5_000 );
150
- }
151
-
152
144
@ Test
153
145
void testNoEarlyExitBackOff () {
154
146
DefaultAfterRollbackProcessor <String , String > processor = new DefaultAfterRollbackProcessor <>(
0 commit comments