@@ -97,6 +97,7 @@ public void testRoundRobinWithKeyBytes() {
97
97
assertEquals (10 , partitionCount .get (2 ).intValue ());
98
98
}
99
99
100
+ @ SuppressWarnings ("deprecation" )
100
101
@ Test
101
102
public void testRoundRobinWithNullKeyBytes () {
102
103
final String topicA = "topicA" ;
@@ -113,6 +114,10 @@ public void testRoundRobinWithNullKeyBytes() {
113
114
Partitioner partitioner = new RoundRobinPartitioner ();
114
115
for (int i = 0 ; i < 30 ; ++i ) {
115
116
int partition = partitioner .partition (topicA , null , null , null , null , testCluster );
117
+ // Simulate single-message batches
118
+ partitioner .onNewBatch (topicA , testCluster , partition );
119
+ int nextPartition = partitioner .partition (topicA , null , null , null , null , testCluster );
120
+ assertEquals (partition , nextPartition , "New batch creation should not affect the partition selection" );
116
121
Integer count = partitionCount .get (partition );
117
122
if (null == count )
118
123
count = 0 ;
@@ -126,5 +131,42 @@ public void testRoundRobinWithNullKeyBytes() {
126
131
assertEquals (10 , partitionCount .get (0 ).intValue ());
127
132
assertEquals (10 , partitionCount .get (1 ).intValue ());
128
133
assertEquals (10 , partitionCount .get (2 ).intValue ());
129
- }
134
+ }
135
+
136
+ @ SuppressWarnings ("deprecation" )
137
+ @ Test
138
+ public void testRoundRobinWithNullKeyBytesAndEvenPartitionCount () {
139
+ final String topicA = "topicA" ;
140
+ final String topicB = "topicB" ;
141
+
142
+ List <PartitionInfo > allPartitions = asList (new PartitionInfo (topicA , 0 , NODES [0 ], NODES , NODES ),
143
+ new PartitionInfo (topicA , 1 , NODES [1 ], NODES , NODES ), new PartitionInfo (topicA , 2 , NODES [2 ], NODES , NODES ),
144
+ new PartitionInfo (topicB , 0 , NODES [0 ], NODES , NODES ), new PartitionInfo (topicA , 3 , NODES [0 ], NODES , NODES ));
145
+ Cluster testCluster = new Cluster ("clusterId" , asList (NODES [0 ], NODES [1 ], NODES [2 ]), allPartitions ,
146
+ Collections .emptySet (), Collections .emptySet ());
147
+
148
+ final Map <Integer , Integer > partitionCount = new HashMap <>();
149
+
150
+ Partitioner partitioner = new RoundRobinPartitioner ();
151
+ for (int i = 0 ; i < 40 ; ++i ) {
152
+ int partition = partitioner .partition (topicA , null , null , null , null , testCluster );
153
+ // Simulate single-message batches
154
+ partitioner .onNewBatch (topicA , testCluster , partition );
155
+ int nextPartition = partitioner .partition (topicA , null , null , null , null , testCluster );
156
+ assertEquals (partition , nextPartition , "New batch creation should not affect the partition selection" );
157
+ Integer count = partitionCount .get (partition );
158
+ if (null == count )
159
+ count = 0 ;
160
+ partitionCount .put (partition , count + 1 );
161
+
162
+ if (i % 5 == 0 ) {
163
+ partitioner .partition (topicB , null , null , null , null , testCluster );
164
+ }
165
+ }
166
+
167
+ assertEquals (10 , partitionCount .get (0 ).intValue ());
168
+ assertEquals (10 , partitionCount .get (1 ).intValue ());
169
+ assertEquals (10 , partitionCount .get (2 ).intValue ());
170
+ assertEquals (10 , partitionCount .get (3 ).intValue ());
171
+ }
130
172
}
0 commit comments