|
| 1 | +/* |
| 2 | + * Copyright 2021 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.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | + |
| 24 | +import org.apache.kafka.common.TopicPartition; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Gary Russell |
| 29 | + * @since 2.6.5 |
| 30 | + * |
| 31 | + */ |
| 32 | +public class ConsumerAwareRebalanceListenerTests { |
| 33 | + |
| 34 | + @Test |
| 35 | + void nonConsumerAwareTestAssigned() { |
| 36 | + AtomicBoolean called = new AtomicBoolean(); |
| 37 | + new ConsumerAwareRebalanceListener() { |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onPartitionsAssigned(Collection<TopicPartition> partitions) { |
| 41 | + called.set(true); |
| 42 | + } |
| 43 | + |
| 44 | + }.onPartitionsAssigned(null, null); |
| 45 | + assertThat(called.get()).isTrue(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void nonConsumerAwareTestAssignedThrows() { |
| 50 | + AtomicBoolean called = new AtomicBoolean(); |
| 51 | + new ConsumerAwareRebalanceListener() { |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onPartitionsAssigned(Collection<TopicPartition> partitions) { |
| 55 | + called.set(true); |
| 56 | + throw new RuntimeException(); |
| 57 | + } |
| 58 | + |
| 59 | + }.onPartitionsAssigned(null, null); |
| 60 | + assertThat(called.get()).isTrue(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void nonConsumerAwareTestRevoked() { |
| 65 | + AtomicBoolean called = new AtomicBoolean(); |
| 66 | + new ConsumerAwareRebalanceListener() { |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onPartitionsRevoked(Collection<TopicPartition> partitions) { |
| 70 | + called.set(true); |
| 71 | + } |
| 72 | + |
| 73 | + }.onPartitionsRevokedBeforeCommit(null, null); |
| 74 | + assertThat(called.get()).isTrue(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void nonConsumerAwareTestRevokedThrows() { |
| 79 | + AtomicBoolean called = new AtomicBoolean(); |
| 80 | + new ConsumerAwareRebalanceListener() { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onPartitionsRevoked(Collection<TopicPartition> partitions) { |
| 84 | + called.set(true); |
| 85 | + throw new RuntimeException(); |
| 86 | + } |
| 87 | + |
| 88 | + }.onPartitionsRevokedBeforeCommit(null, null); |
| 89 | + assertThat(called.get()).isTrue(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void nonConsumerAwareTestLost() { |
| 94 | + AtomicBoolean called = new AtomicBoolean(); |
| 95 | + new ConsumerAwareRebalanceListener() { |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onPartitionsLost(Collection<TopicPartition> partitions) { |
| 99 | + called.set(true); |
| 100 | + } |
| 101 | + |
| 102 | + }.onPartitionsLost(null, null); |
| 103 | + assertThat(called.get()).isTrue(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void nonConsumerAwareTestLostThrows() { |
| 108 | + AtomicBoolean called = new AtomicBoolean(); |
| 109 | + new ConsumerAwareRebalanceListener() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onPartitionsLost(Collection<TopicPartition> partitions) { |
| 113 | + called.set(true); |
| 114 | + throw new RuntimeException(); |
| 115 | + } |
| 116 | + |
| 117 | + }.onPartitionsLost(null, null); |
| 118 | + assertThat(called.get()).isTrue(); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments