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 ;
@@ -3049,38 +3050,63 @@ public void testCommitErrorHandlerCalled() throws Exception {
3049
3050
container .stop ();
3050
3051
}
3051
3052
3052
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
3053
3053
@ Test
3054
- void testFatalErrorOnAuthenticationException () throws Exception {
3054
+ void testFatalErrorOnAuthenticationException () throws InterruptedException {
3055
3055
ConsumerFactory <Integer , String > cf = mock (ConsumerFactory .class );
3056
- Consumer <Integer , String > consumer = mock (Consumer .class );
3057
- given (cf .createConsumer (eq ("grp" ), eq ("clientId" ), isNull (), any ())).willReturn (consumer );
3058
- given (cf .getConfigurationProperties ()).willReturn (new HashMap <>());
3059
-
3060
- willThrow (AuthenticationException .class )
3061
- .given (consumer ).poll (any ());
3062
-
3063
3056
ContainerProperties containerProps = new ContainerProperties (topic1 );
3064
3057
containerProps .setGroupId ("grp" );
3065
3058
containerProps .setClientId ("clientId" );
3066
3059
containerProps .setMessageListener ((MessageListener ) r -> { });
3067
3060
KafkaMessageListenerContainer <Integer , String > container =
3068
3061
new KafkaMessageListenerContainer <>(cf , containerProps );
3062
+ testFatalErrorOnAuthenticationException (container , cf );
3063
+ }
3064
+
3065
+ @ Test
3066
+ void testFatalErrorOnAuthenticationExceptionConcurrent () throws InterruptedException {
3067
+ ConsumerFactory <Integer , String > cf = mock (ConsumerFactory .class );
3068
+ ContainerProperties containerProps = new ContainerProperties (topic1 );
3069
+ containerProps .setGroupId ("grp" );
3070
+ containerProps .setClientId ("clientId" );
3071
+ containerProps .setMessageListener ((MessageListener ) r -> { });
3072
+ ConcurrentMessageListenerContainer <Integer , String > container =
3073
+ new ConcurrentMessageListenerContainer <>(cf , containerProps );
3074
+ testFatalErrorOnAuthenticationException (container , cf );
3075
+ }
3076
+
3077
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
3078
+ private void testFatalErrorOnAuthenticationException (AbstractMessageListenerContainer container ,
3079
+ ConsumerFactory <Integer , String > cf ) throws InterruptedException {
3080
+
3081
+ Consumer <Integer , String > consumer = mock (Consumer .class );
3082
+ given (cf .createConsumer (eq ("grp" ), eq ("clientId" ),
3083
+ container instanceof ConcurrentMessageListenerContainer ? eq ("-0" ) : isNull (), any ()))
3084
+ .willReturn (consumer );
3085
+ given (cf .getConfigurationProperties ()).willReturn (new HashMap <>());
3086
+
3087
+ willThrow (AuthenticationException .class )
3088
+ .given (consumer ).poll (any ());
3069
3089
3070
3090
AtomicReference <ConsumerStoppedEvent .Reason > reason = new AtomicReference <>();
3071
- CountDownLatch stopped = new CountDownLatch (1 );
3091
+ CountDownLatch consumerStopped = new CountDownLatch (1 );
3092
+ CountDownLatch containerStopped = new CountDownLatch (1 );
3072
3093
3073
3094
container .setApplicationEventPublisher (e -> {
3074
3095
if (e instanceof ConsumerStoppedEvent ) {
3075
3096
reason .set (((ConsumerStoppedEvent ) e ).getReason ());
3076
- stopped .countDown ();
3097
+ consumerStopped .countDown ();
3098
+ }
3099
+ else if (e instanceof ContainerStoppedEvent ) {
3100
+ containerStopped .countDown ();
3077
3101
}
3078
3102
});
3079
3103
3080
3104
container .start ();
3081
3105
try {
3082
- assertThat (stopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3106
+ assertThat (consumerStopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3083
3107
assertThat (reason .get ()).isEqualTo (Reason .AUTH );
3108
+ assertThat (containerStopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3109
+ assertThat (container .isInExpectedState ()).isFalse ();
3084
3110
}
3085
3111
finally {
3086
3112
container .stop ();
@@ -3106,18 +3132,23 @@ void testFatalErrorOnAuthorizationException() throws Exception {
3106
3132
new KafkaMessageListenerContainer <>(cf , containerProps );
3107
3133
3108
3134
AtomicReference <ConsumerStoppedEvent .Reason > reason = new AtomicReference <>();
3109
- CountDownLatch stopped = new CountDownLatch (1 );
3135
+ CountDownLatch consumerStopped = new CountDownLatch (1 );
3136
+ CountDownLatch containerStopped = new CountDownLatch (1 );
3110
3137
3111
3138
container .setApplicationEventPublisher (e -> {
3112
3139
if (e instanceof ConsumerStoppedEvent ) {
3113
3140
reason .set (((ConsumerStoppedEvent ) e ).getReason ());
3114
- stopped .countDown ();
3141
+ consumerStopped .countDown ();
3142
+ }
3143
+ else if (e instanceof ContainerStoppedEvent ) {
3144
+ containerStopped .countDown ();
3115
3145
}
3116
3146
});
3117
3147
3118
3148
container .start ();
3119
- assertThat (stopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3149
+ assertThat (consumerStopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3120
3150
assertThat (reason .get ()).isEqualTo (Reason .AUTH );
3151
+ assertThat (container .isInExpectedState ()).isFalse ();
3121
3152
container .stop ();
3122
3153
}
3123
3154
@@ -3144,6 +3175,7 @@ void testNotFatalErrorOnAuthorizationException() throws Exception {
3144
3175
container .start ();
3145
3176
assertThat (latch .await (10 , TimeUnit .SECONDS )).isTrue ();
3146
3177
container .stop ();
3178
+ assertThat (container .isInExpectedState ()).isTrue ();
3147
3179
}
3148
3180
3149
3181
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -3167,13 +3199,14 @@ void testFatalErrorOnFencedInstanceException() throws Exception {
3167
3199
CountDownLatch stopped = new CountDownLatch (1 );
3168
3200
3169
3201
container .setApplicationEventPublisher (e -> {
3170
- if (e instanceof ConsumerStoppedEvent ) {
3202
+ if (e instanceof ContainerStoppedEvent ) {
3171
3203
stopped .countDown ();
3172
3204
}
3173
3205
});
3174
3206
3175
3207
container .start ();
3176
3208
assertThat (stopped .await (10 , TimeUnit .SECONDS )).isTrue ();
3209
+ assertThat (container .isInExpectedState ()).isFalse ();
3177
3210
container .stop ();
3178
3211
}
3179
3212
0 commit comments