Skip to content

Commit 7701bf4

Browse files
committed
test(linked): cover free-list reuse and cap-reached branches
The node free list added in this branch introduced two new branches that the existing tests didn't hit — "Offer after Get reuses a recycled node" and "drain past freeCap stops growing the free list". Coverage fell to 98.4% and the 100% gate failed. Add a dedicated sub-test that exercises both paths. No observable behaviour change.
1 parent 7725531 commit 7701bf4

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

linked_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,69 @@ func TestLinked(t *testing.T) {
2222
t.Run("Reset", testLinkedReset)
2323
t.Run("Iterator", testLinkedIterator)
2424
t.Run("MarshalJSON", testLinkedMarshalJSON)
25+
t.Run("NodeRecycling", testLinkedNodeRecycling)
26+
}
27+
28+
// testLinkedNodeRecycling exercises both branches of the internal free
29+
// list: Offer after Get reuses a recycled node, and draining past the
30+
// free list's cap stops growing it. Purely a coverage driver; observable
31+
// behaviour is identical to an unpooled implementation.
32+
func testLinkedNodeRecycling(t *testing.T) {
33+
t.Parallel()
34+
35+
t.Run("OfferReusesPoppedNode", func(t *testing.T) {
36+
t.Parallel()
37+
38+
linkedQueue := queue.NewLinked([]int{1, 2, 3})
39+
40+
got, err := linkedQueue.Get()
41+
if err != nil || got != 1 {
42+
t.Fatalf("get: %d %v", got, err)
43+
}
44+
45+
// This Offer should hit the free-list-has-node path.
46+
if err := linkedQueue.Offer(4); err != nil {
47+
t.Fatalf("offer: %v", err)
48+
}
49+
50+
cleared := linkedQueue.Clear()
51+
expected := []int{2, 3, 4}
52+
53+
if !reflect.DeepEqual(expected, cleared) {
54+
t.Fatalf("expected %v got %v", expected, cleared)
55+
}
56+
})
57+
58+
t.Run("DrainBeyondFreeCap", func(t *testing.T) {
59+
t.Parallel()
60+
61+
// Use a size that exceeds the free-list cap so the cap-reached
62+
// branch of recycle() executes.
63+
const n = 128
64+
65+
linkedQueue := queue.NewLinked[int](nil)
66+
67+
for i := 0; i < n; i++ {
68+
if err := linkedQueue.Offer(i); err != nil {
69+
t.Fatalf("offer %d: %v", i, err)
70+
}
71+
}
72+
73+
for i := 0; i < n; i++ {
74+
got, err := linkedQueue.Get()
75+
if err != nil {
76+
t.Fatalf("get %d: %v", i, err)
77+
}
78+
79+
if got != i {
80+
t.Fatalf("get %d: want %d got %d", i, i, got)
81+
}
82+
}
83+
84+
if !linkedQueue.IsEmpty() {
85+
t.Fatal("queue should be empty")
86+
}
87+
})
2588
}
2689

2790
func testLinkedGet(t *testing.T) {

0 commit comments

Comments
 (0)