Skip to content

Commit 54e629e

Browse files
MINOR: Add test for Assignment immutability (#21068)
The server-side assignors rely on receiving immutable assignments, so that they can distinguish between changed and unchanged assignments. Add a test to check that Assignment maps are immutable, otherwise they will be modified in place by assignors. Reviewers: David Jacot <[email protected]>
1 parent 70be719 commit 54e629e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/Assignment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class Assignment implements MemberAssignment {
4242
public Assignment(
4343
Map<Uuid, Set<Integer>> partitions
4444
) {
45+
// Assignments are used as input to assignors, which expect to receive immutable
46+
// assignment maps, otherwise they will be modified in place.
4547
this.partitions = Collections.unmodifiableMap(Objects.requireNonNull(partitions));
4648
}
4749

group-coordinator/src/test/java/org/apache/kafka/coordinator/group/modern/AssignmentTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package org.apache.kafka.coordinator.group.modern;
1818

1919
import org.apache.kafka.common.Uuid;
20+
import org.apache.kafka.coordinator.group.assignor.AssignorHelpers;
2021
import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
2122

2223
import org.junit.jupiter.api.Test;
2324

2425
import java.util.ArrayList;
2526
import java.util.Arrays;
27+
import java.util.HashMap;
2628
import java.util.List;
2729
import java.util.Map;
2830
import java.util.Set;
@@ -31,6 +33,7 @@
3133
import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkTopicAssignment;
3234
import static org.junit.jupiter.api.Assertions.assertEquals;
3335
import static org.junit.jupiter.api.Assertions.assertThrows;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
3437

3538
public class AssignmentTest {
3639

@@ -39,6 +42,16 @@ public void testPartitionsCannotBeNull() {
3942
assertThrows(NullPointerException.class, () -> new Assignment(null));
4043
}
4144

45+
@Test
46+
public void testPartitionsImmutable() {
47+
// Assignments are used as input to assignors, which expect to receive immutable
48+
// assignment maps, otherwise they will be modified in place.
49+
Map<Uuid, Set<Integer>> partitions = new HashMap<>();
50+
partitions.put(Uuid.randomUuid(), Set.of(1, 2, 3));
51+
Assignment assignment = new Assignment(partitions);
52+
assertTrue(AssignorHelpers.isImmutableMap(assignment.partitions()));
53+
}
54+
4255
@Test
4356
public void testAttributes() {
4457
Map<Uuid, Set<Integer>> partitions = mkAssignment(

0 commit comments

Comments
 (0)