25
25
import static org .mockito .Mockito .times ;
26
26
27
27
import java .math .BigInteger ;
28
+ import java .nio .ByteBuffer ;
28
29
import java .time .Clock ;
29
30
import java .time .Instant ;
30
31
import java .time .LocalDateTime ;
@@ -127,22 +128,22 @@ void shouldSendMessage() {
127
128
// assert headers
128
129
Header attemptsHeader = producerRecord .headers ().lastHeader (RetryTopicHeaders .DEFAULT_HEADER_ATTEMPTS );
129
130
assertThat (attemptsHeader ).isNotNull ();
130
- assertThat (attemptsHeader .value ()[ 0 ]). isEqualTo ( Integer . valueOf ( 2 ). byteValue () );
131
+ assertThat (ByteBuffer . wrap ( attemptsHeader .value ()). getInt ()). isEqualTo ( 2 );
131
132
Header timestampHeader = producerRecord .headers ().lastHeader (RetryTopicHeaders .DEFAULT_HEADER_BACKOFF_TIMESTAMP );
132
133
assertThat (timestampHeader ).isNotNull ();
133
134
assertThat (new BigInteger (timestampHeader .value ()).longValue ()).isEqualTo (failureTimestamp + 1000L );
134
135
}
135
136
136
137
@ Test
137
- void shouldIncreaseAttempts () {
138
+ void shouldIncreaseAttemptsInLegacyHeader () {
138
139
139
140
// setup
140
141
RuntimeException e = new RuntimeException ();
141
142
ConsumerRecord consumerRecord = new ConsumerRecord (testTopic , 0 , 0 , key , value );
142
- consumerRecord .headers ().add (RetryTopicHeaders .DEFAULT_HEADER_ATTEMPTS , BigInteger .valueOf (1 ).toByteArray ());
143
+ consumerRecord .headers ().add (RetryTopicHeaders .DEFAULT_HEADER_ATTEMPTS , BigInteger .valueOf (127 ).toByteArray ());
143
144
consumerRecord .headers ().add (RetryTopicHeaders .DEFAULT_HEADER_ORIGINAL_TIMESTAMP , this .originalTimestampBytes );
144
145
145
- given (destinationTopicResolver .resolveDestinationTopic (testTopic , 1 , e , originalTimestamp ))
146
+ given (destinationTopicResolver .resolveDestinationTopic (testTopic , 127 , e , originalTimestamp ))
146
147
.willReturn (destinationTopic );
147
148
given (destinationTopic .isNoOpsTopic ()).willReturn (false );
148
149
given (destinationTopic .getDestinationName ()).willReturn (testRetryTopic );
@@ -161,7 +162,41 @@ void shouldIncreaseAttempts() {
161
162
ProducerRecord producerRecord = producerRecordCaptor .getValue ();
162
163
Header attemptsHeader = producerRecord .headers ().lastHeader (RetryTopicHeaders .DEFAULT_HEADER_ATTEMPTS );
163
164
assertThat (attemptsHeader ).isNotNull ();
164
- assertThat (attemptsHeader .value ()[0 ]).isEqualTo (Integer .valueOf (2 ).byteValue ());
165
+ assertThat (attemptsHeader .value ().length ).isEqualTo (4 ); // handled a legacy one byte header ok
166
+ assertThat (ByteBuffer .wrap (attemptsHeader .value ()).getInt ()).isEqualTo (128 );
167
+ }
168
+
169
+ @ Test
170
+ void shouldIncreaseAttemptsInNewHeader () {
171
+
172
+ // setup
173
+ RuntimeException e = new RuntimeException ();
174
+ ConsumerRecord consumerRecord = new ConsumerRecord (testTopic , 0 , 0 , key , value );
175
+ consumerRecord .headers ().add (RetryTopicHeaders .DEFAULT_HEADER_ATTEMPTS ,
176
+ ByteBuffer .wrap (new byte [4 ]).putInt (127 ).array ());
177
+ consumerRecord .headers ().add (RetryTopicHeaders .DEFAULT_HEADER_ORIGINAL_TIMESTAMP , this .originalTimestampBytes );
178
+
179
+ given (destinationTopicResolver .resolveDestinationTopic (testTopic , 127 , e , originalTimestamp ))
180
+ .willReturn (destinationTopic );
181
+ given (destinationTopic .isNoOpsTopic ()).willReturn (false );
182
+ given (destinationTopic .getDestinationName ()).willReturn (testRetryTopic );
183
+ given (destinationTopicResolver .getDestinationTopicByName (testRetryTopic )).willReturn (destinationTopic );
184
+ willReturn (kafkaOperations ).given (destinationTopic ).getKafkaOperations ();
185
+ given (kafkaOperations .send (any (ProducerRecord .class ))).willReturn (listenableFuture );
186
+
187
+ DeadLetterPublishingRecovererFactory factory = new DeadLetterPublishingRecovererFactory (this .destinationTopicResolver );
188
+
189
+ // when
190
+ DeadLetterPublishingRecoverer deadLetterPublishingRecoverer = factory .create ();
191
+ deadLetterPublishingRecoverer .accept (consumerRecord , e );
192
+
193
+ // then
194
+ then (kafkaOperations ).should (times (1 )).send (producerRecordCaptor .capture ());
195
+ ProducerRecord producerRecord = producerRecordCaptor .getValue ();
196
+ Header attemptsHeader = producerRecord .headers ().lastHeader (RetryTopicHeaders .DEFAULT_HEADER_ATTEMPTS );
197
+ assertThat (attemptsHeader ).isNotNull ();
198
+ assertThat (attemptsHeader .value ().length ).isEqualTo (4 );
199
+ assertThat (ByteBuffer .wrap (attemptsHeader .value ()).getInt ()).isEqualTo (128 );
165
200
}
166
201
167
202
@ Test
0 commit comments