|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.BDDMockito.willThrow; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | + |
| 24 | +import java.lang.reflect.Method; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.apache.kafka.clients.consumer.Consumer; |
| 29 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter; |
| 33 | +import org.springframework.kafka.listener.adapter.HandlerAdapter; |
| 34 | +import org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter; |
| 35 | +import org.springframework.kafka.support.Acknowledgment; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Gary Russell |
| 39 | + * @since 2.9 |
| 40 | + * |
| 41 | + */ |
| 42 | +public class ListenerErrorHandlerTests { |
| 43 | + |
| 44 | + private static Method test1; |
| 45 | + |
| 46 | + private static Method test2; |
| 47 | + |
| 48 | + static { |
| 49 | + try { |
| 50 | + test1 = TestListener.class.getDeclaredMethod("test1", String.class, Acknowledgment.class); |
| 51 | + test2 = TestListener.class.getDeclaredMethod("test2", List.class, Acknowledgment.class); |
| 52 | + } |
| 53 | + catch (NoSuchMethodException | SecurityException e) { |
| 54 | + throw new IllegalStateException(e); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 59 | + @Test |
| 60 | + void record() throws Exception { |
| 61 | + RecordMessagingMessageListenerAdapter adapter = new RecordMessagingMessageListenerAdapter(getClass(), test1, |
| 62 | + (ManualAckListenerErrorHandler) (msg, ex, cons, ack) -> { |
| 63 | + ack.acknowledge(); |
| 64 | + return null; |
| 65 | + }); |
| 66 | + HandlerAdapter handler = mock(HandlerAdapter.class); |
| 67 | + willThrow(new RuntimeException("test")).given(handler).invoke(any(), any()); |
| 68 | + adapter.setHandlerMethod(handler); |
| 69 | + Acknowledgment ack = mock(Acknowledgment.class); |
| 70 | + adapter.onMessage(mock(ConsumerRecord.class), ack, mock(Consumer.class)); |
| 71 | + verify(ack).acknowledge(); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 75 | + @Test |
| 76 | + void batch() throws Exception { |
| 77 | + BatchMessagingMessageListenerAdapter adapter = new BatchMessagingMessageListenerAdapter(getClass(), test2, |
| 78 | + (ManualAckListenerErrorHandler) (msg, ex, cons, ack) -> { |
| 79 | + ack.acknowledge(); |
| 80 | + return null; |
| 81 | + }); |
| 82 | + HandlerAdapter handler = mock(HandlerAdapter.class); |
| 83 | + willThrow(new RuntimeException("test")).given(handler).invoke(any(), any()); |
| 84 | + adapter.setHandlerMethod(handler); |
| 85 | + Acknowledgment ack = mock(Acknowledgment.class); |
| 86 | + adapter.onMessage(Collections.emptyList(), ack, mock(Consumer.class)); |
| 87 | + verify(ack).acknowledge(); |
| 88 | + } |
| 89 | + |
| 90 | + private static class TestListener { |
| 91 | + |
| 92 | + void test1(String foo, Acknowledgment ack) { |
| 93 | + } |
| 94 | + |
| 95 | + void test2(List<String> foo, Acknowledgment ack) { |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments