Skip to content

Commit 207afcb

Browse files
authored
Fix MpscIntQueue bug (#16023)
Motivation: The int queue is always sized as a power of two, but the empty value was only set for the specified capacity number of entries. Modification: Fill the full length of the underlying array with the empty entry value. Result: No surprise zero values when the empty value is different from zero.
1 parent 27bfd56 commit 207afcb

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

common/src/main/java/io/netty/util/concurrent/MpscIntQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public MpscAtomicIntegerArrayQueue(int capacity, int emptyValue) {
111111
super(MathUtil.safeFindNextPositivePowerOfTwo(capacity));
112112
if (emptyValue != 0) {
113113
this.emptyValue = emptyValue;
114-
int end = capacity - 1;
114+
int end = length() - 1;
115115
for (int i = 0; i < end; i++) {
116116
lazySet(i, emptyValue);
117117
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.util.concurrent;
17+
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.ValueSource;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
24+
class MpscIntQueueTest {
25+
@ParameterizedTest
26+
@ValueSource(ints = {1, 7, 8, 15, 16, 17})
27+
void mustFillWithSpecifiedEmptyEntry(int size) throws Exception {
28+
MpscIntQueue queue = MpscIntQueue.create(size, -1);
29+
int filled = queue.fill(size, () -> 42);
30+
assertEquals(size, filled);
31+
for (int i = 0; i < size; i++) {
32+
assertEquals(42, queue.poll());
33+
}
34+
assertEquals(-1, queue.poll());
35+
assertTrue(queue.isEmpty());
36+
}
37+
}

0 commit comments

Comments
 (0)