WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8d20a42

Browse files
committed
Add junit based tests
1 parent 08badb2 commit 8d20a42

12 files changed

+514
-457
lines changed

utils/queue-utils/build.gradle.kts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import groovy.lang.Closure
2-
import org.gradle.kotlin.dsl.extra
32

43
plugins {
54
`java-library`
@@ -16,8 +15,17 @@ java {
1615
}
1716
}
1817

19-
fun AbstractCompile.configureCompiler(javaVersionInteger: Int, compatibilityVersion: JavaVersion? = null, unsetReleaseFlagReason: String? = null) {
20-
(project.extra["configureCompiler"] as Closure<*>).call(this, javaVersionInteger, compatibilityVersion, unsetReleaseFlagReason)
18+
fun AbstractCompile.configureCompiler(
19+
javaVersionInteger: Int,
20+
compatibilityVersion: JavaVersion? = null,
21+
unsetReleaseFlagReason: String? = null
22+
) {
23+
(project.extra["configureCompiler"] as Closure<*>).call(
24+
this,
25+
javaVersionInteger,
26+
compatibilityVersion,
27+
unsetReleaseFlagReason
28+
)
2129
}
2230

2331
listOf(JavaCompile::class.java, GroovyCompile::class.java).forEach { compileTaskType ->
@@ -30,7 +38,8 @@ dependencies {
3038
api(project(":internal-api"))
3139
api(libs.jctools)
3240

33-
testImplementation(libs.junit)
41+
testImplementation(libs.bundles.junit5)
42+
testImplementation(libs.junit.jupiter.params)
3443
testImplementation(libs.slf4j)
3544
}
3645

utils/queue-utils/src/main/java/datadog/common/queue/MpscBlockingConsumerArrayQueueVarHandle.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,19 +591,26 @@ public int fill(Supplier<E> s, int limit) {
591591
}
592592
// pIndex is even (lower bit is 0) -> actual index is (pIndex >> 1), consumer is awake
593593

594-
// we want 'limit' slots, but will settle for whatever is visible to 'producerLimit'
595-
batchLimit =
596-
Math.min(producerLimit, pIndex + shiftedBatchSize); // -> producerLimit >= batchLimit
594+
// Calculate available space (similar to reference implementation)
595+
long available = producerLimit - pIndex;
597596

598597
// Use producer limit to save a read of the more rapidly mutated consumer index.
599598
// Assumption: queue is usually empty or near empty
600-
if (pIndex >= producerLimit) {
599+
if (available < shiftedBatchSize) {
601600
if (!recalculateProducerLimit(mask, pIndex, producerLimit)) {
602601
return 0;
603602
}
604-
batchLimit = Math.min(lpProducerLimit(), pIndex + shiftedBatchSize);
603+
producerLimit = lpProducerLimit();
604+
available = producerLimit - pIndex;
605+
if (available <= 0) {
606+
return 0; // FULL
607+
}
605608
}
606609

610+
// we want 'limit' slots, but will settle for whatever is available
611+
long claimedShifted = Math.min(available, shiftedBatchSize);
612+
batchLimit = pIndex + claimedShifted;
613+
607614
// Claim the index
608615
if (casProducerIndex(pIndex, batchLimit)) {
609616
break;

utils/queue-utils/src/test/java/datadog/common/queue/AbstractQueueTest.groovy

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package datadog.common.queue;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
abstract class AbstractQueueTest<T extends BaseQueue<Integer>> {
11+
abstract T createQueue(int capacity);
12+
13+
protected T queue;
14+
15+
@BeforeEach
16+
void setUp() {
17+
queue = createQueue(8);
18+
}
19+
20+
@Test
21+
void offerAndPollShouldPreserveFIFOOrder() {
22+
queue.offer(1);
23+
queue.offer(2);
24+
queue.offer(3);
25+
26+
assertEquals(1, queue.poll());
27+
assertEquals(2, queue.poll());
28+
assertEquals(3, queue.poll());
29+
assertNull(queue.poll());
30+
}
31+
32+
@Test
33+
void offerShouldReturnFalseWhenQueueIsFull() {
34+
queue.clear();
35+
for (int i = 1; i <= 8; i++) {
36+
queue.offer(i);
37+
}
38+
39+
assertFalse(queue.offer(999));
40+
assertEquals(8, queue.size());
41+
}
42+
43+
@Test
44+
void peekShouldReturnHeadElementWithoutRemovingIt() {
45+
queue.clear();
46+
queue.offer(10);
47+
queue.offer(20);
48+
49+
assertEquals(10, queue.peek());
50+
assertEquals(10, queue.peek());
51+
assertEquals(2, queue.size());
52+
}
53+
54+
@Test
55+
void pollShouldReturnNullWhenEmpty() {
56+
queue.clear();
57+
58+
assertNull(queue.poll());
59+
}
60+
61+
@Test
62+
void sizeShouldReflectCurrentNumberOfItems() {
63+
queue.clear();
64+
queue.offer(1);
65+
queue.offer(2);
66+
67+
assertEquals(2, queue.size());
68+
69+
queue.poll();
70+
queue.poll();
71+
72+
assertEquals(0, queue.size());
73+
}
74+
75+
@Test
76+
void drainShouldConsumeAllAvailableElements() {
77+
queue.clear();
78+
for (int i = 1; i <= 5; i++) {
79+
queue.offer(i);
80+
}
81+
List<Integer> drained = new ArrayList<>();
82+
83+
int count = queue.drain(drained::add);
84+
85+
assertEquals(5, count);
86+
assertEquals(List.of(1, 2, 3, 4, 5), drained);
87+
assertTrue(queue.isEmpty());
88+
}
89+
90+
@Test
91+
void drainWithLimitShouldOnlyConsumeThatManyElements() {
92+
queue.clear();
93+
for (int i = 1; i <= 6; i++) {
94+
queue.offer(i);
95+
}
96+
List<Integer> drained = new ArrayList<>();
97+
98+
int count = queue.drain(drained::add, 3);
99+
100+
assertEquals(3, count);
101+
assertEquals(List.of(1, 2, 3), drained);
102+
assertEquals(3, queue.size());
103+
}
104+
105+
@Test
106+
void remainingCapacityShouldReflectCurrentOccupancy() {
107+
T q = createQueue(4);
108+
q.offer(1);
109+
q.offer(2);
110+
111+
assertEquals(2, q.capacity() - q.size());
112+
113+
q.poll();
114+
115+
assertEquals(3, q.capacity() - q.size());
116+
}
117+
}

utils/queue-utils/src/test/java/datadog/common/queue/MpscArrayQueueVarHandleTest.groovy

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)