99
99
import org .springframework .kafka .event .ConsumerStoppedEvent ;
100
100
import org .springframework .kafka .event .ConsumerStoppedEvent .Reason ;
101
101
import org .springframework .kafka .event .ConsumerStoppingEvent ;
102
+ import org .springframework .kafka .event .ContainerStoppedEvent ;
102
103
import org .springframework .kafka .event .NonResponsiveConsumerEvent ;
103
104
import org .springframework .kafka .listener .ContainerProperties .AckMode ;
104
105
import org .springframework .kafka .listener .ContainerProperties .AssignmentCommitOption ;
@@ -3068,38 +3069,63 @@ public void handleOtherException(Exception thrownException, Consumer<?, ?> consu
3068
3069
container .stop ();
3069
3070
}
3070
3071
3071
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
3072
3072
@ Test
3073
- void testFatalErrorOnAuthenticationException () throws Exception {
3073
+ void testFatalErrorOnAuthenticationException () throws InterruptedException {
3074
3074
ConsumerFactory <Integer , String > cf = mock (ConsumerFactory .class );
3075
- Consumer <Integer , String > consumer = mock (Consumer .class );
3076
- given (cf .createConsumer (eq ("grp" ), eq ("clientId" ), isNull (), any ())).willReturn (consumer );
3077
- given (cf .getConfigurationProperties ()).willReturn (new HashMap <>());
3078
-
3079
- willThrow (AuthenticationException .class )
3080
- .given (consumer ).poll (any ());
3081
-
3082
3075
ContainerProperties containerProps = new ContainerProperties (topic1 );
3083
3076
containerProps .setGroupId ("grp" );
3084
3077
containerProps .setClientId ("clientId" );
3085
3078
containerProps .setMessageListener ((MessageListener ) r -> { });
3086
3079
KafkaMessageListenerContainer <Integer , String > container =
3087
3080
new KafkaMessageListenerContainer <>(cf , containerProps );
3081
+ testFatalErrorOnAuthenticationException (container , cf );
3082
+ }
3083
+
3084
+ @ Test
3085
+ void testFatalErrorOnAuthenticationExceptionConcurrent () throws InterruptedException {
3086
+ ConsumerFactory <Integer , String > cf = mock (ConsumerFactory .class );
3087
+ ContainerProperties containerProps = new ContainerProperties (topic1 );
3088
+ containerProps .setGroupId ("grp" );
3089
+ containerProps .setClientId ("clientId" );
3090
+ containerProps .setMessageListener ((MessageListener ) r -> { });
3091
+ ConcurrentMessageListenerContainer <Integer , String > container =
3092
+ new ConcurrentMessageListenerContainer <>(cf , containerProps );
3093
+ testFatalErrorOnAuthenticationException (container , cf );
3094
+ }
3095
+
3096
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
3097
+ private void testFatalErrorOnAuthenticationException (AbstractMessageListenerContainer container ,
3098
+ ConsumerFactory <Integer , String > cf ) throws InterruptedException {
3099
+
3100
+ Consumer <Integer , String > consumer = mock (Consumer .class );
3101
+ given (cf .createConsumer (eq ("grp" ), eq ("clientId" ),
3102
+ container instanceof ConcurrentMessageListenerContainer ? eq ("-0" ) : isNull (), any ()))
3103
+ .willReturn (consumer );
3104
+ given (cf .getConfigurationProperties ()).willReturn (new HashMap <>());
3105
+
3106
+ willThrow (AuthenticationException .class )
3107
+ .given (consumer ).poll (any ());
3088
3108
3089
3109
AtomicReference <ConsumerStoppedEvent .Reason > reason = new AtomicReference <>();
3090
- CountDownLatch stopped = new CountDownLatch (1 );
3110
+ CountDownLatch consumerStopped = new CountDownLatch (1 );
3111
+ CountDownLatch containerStopped = new CountDownLatch (1 );
3091
3112
3092
3113
container .setApplicationEventPublisher (e -> {
3093
3114
if (e instanceof ConsumerStoppedEvent ) {
3094
3115
reason .set (((ConsumerStoppedEvent ) e ).getReason ());
3095
- stopped .countDown ();
3116
+ consumerStopped .countDown ();
3117
+ }
3118
+ else if (e instanceof ContainerStoppedEvent ) {
3119
+ containerStopped .countDown ();
3096
3120
}
3097
3121
});
3098
3122
3099
3123
container .start ();
3100
3124
try {
3101
- assertThat (stopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3125
+ assertThat (consumerStopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3102
3126
assertThat (reason .get ()).isEqualTo (Reason .AUTH );
3127
+ assertThat (containerStopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3128
+ assertThat (container .isInExpectedState ()).isFalse ();
3103
3129
}
3104
3130
finally {
3105
3131
container .stop ();
@@ -3125,18 +3151,23 @@ void testFatalErrorOnAuthorizationException() throws Exception {
3125
3151
new KafkaMessageListenerContainer <>(cf , containerProps );
3126
3152
3127
3153
AtomicReference <ConsumerStoppedEvent .Reason > reason = new AtomicReference <>();
3128
- CountDownLatch stopped = new CountDownLatch (1 );
3154
+ CountDownLatch consumerStopped = new CountDownLatch (1 );
3155
+ CountDownLatch containerStopped = new CountDownLatch (1 );
3129
3156
3130
3157
container .setApplicationEventPublisher (e -> {
3131
3158
if (e instanceof ConsumerStoppedEvent ) {
3132
3159
reason .set (((ConsumerStoppedEvent ) e ).getReason ());
3133
- stopped .countDown ();
3160
+ consumerStopped .countDown ();
3161
+ }
3162
+ else if (e instanceof ContainerStoppedEvent ) {
3163
+ containerStopped .countDown ();
3134
3164
}
3135
3165
});
3136
3166
3137
3167
container .start ();
3138
- assertThat (stopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3168
+ assertThat (consumerStopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3139
3169
assertThat (reason .get ()).isEqualTo (Reason .AUTH );
3170
+ assertThat (container .isInExpectedState ()).isFalse ();
3140
3171
container .stop ();
3141
3172
}
3142
3173
@@ -3163,6 +3194,7 @@ void testNotFatalErrorOnAuthorizationException() throws Exception {
3163
3194
container .start ();
3164
3195
assertThat (latch .await (10 , TimeUnit .SECONDS )).isTrue ();
3165
3196
container .stop ();
3197
+ assertThat (container .isInExpectedState ()).isTrue ();
3166
3198
}
3167
3199
3168
3200
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -3186,13 +3218,14 @@ void testFatalErrorOnFencedInstanceException() throws Exception {
3186
3218
CountDownLatch stopped = new CountDownLatch (1 );
3187
3219
3188
3220
container .setApplicationEventPublisher (e -> {
3189
- if (e instanceof ConsumerStoppedEvent ) {
3221
+ if (e instanceof ContainerStoppedEvent ) {
3190
3222
stopped .countDown ();
3191
3223
}
3192
3224
});
3193
3225
3194
3226
container .start ();
3195
3227
assertThat (stopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3228
+ assertThat (container .isInExpectedState ()).isFalse ();
3196
3229
container .stop ();
3197
3230
}
3198
3231
0 commit comments