Repository navigation
Expand file tree
/
Copy pathqueue_test.go
More file actions
159 lines (152 loc) · 4.34 KB
/
Copy pathqueue_test.go
File metadata and controls
159 lines (152 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package grpcproc
import (
"slices"
"testing"
"testing/synctest"
"time"
)
func TestQueue(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
q := newQueue[int](true)
if _, ok := q.tryPop(); ok || q.oldestStamp() != 0 {
t.Fatal("empty")
}
before := time.Now().UnixNano()
for i := 1; i <= 3; i++ {
q.push(i)
}
// Before the consumer takes a batch, the oldest is when the first came.
first := q.oldestStamp()
if q.len() != 3 || first < before || first > time.Now().UnixNano() {
t.Fatalf("len %d oldest %d", q.len(), first)
}
if v, _ := q.tryPop(); v != 1 || q.oldestStamp() != first || !q.tookBatch() {
t.Fatalf("pop %d oldest %d", v, q.oldestStamp())
}
q.push(4) // lands in the producers' buffer while the consumer holds 2, 3
for want := 2; want <= 4; want++ {
if v, ok := q.tryPop(); !ok || v != want {
t.Fatalf("pop %d: %d %v", want, v, ok)
}
}
if q.len() != 0 || q.oldestStamp() != 0 {
t.Fatal("drained queue reports items")
}
// Buffers are reused: a queue that is kept up with stops allocating.
if n := testing.AllocsPerRun(100, func() { q.push(1); q.tryPop() }); n != 0 {
t.Fatalf("%v allocs per push and pop", n)
}
// What the consumer swapped in but did not pop, and what producers
// queued since, are both returned once the queue closes.
q.push(5)
q.push(6)
q.tryPop()
q.push(7)
rest := append(q.taken(), q.close()...)
if len(rest) != 2 || rest[0] != 6 || rest[1] != 7 || q.len() != 0 {
t.Fatalf("rest %v, len %d", rest, q.len())
}
if q.push(8) {
t.Fatal("push after close")
}
// drain hands over batches.
d := newQueue[int](false)
d.push(1)
d.push(2)
if b := d.drain(); len(b) != 2 || d.len() != 0 {
t.Fatalf("batch %v", b)
}
d.push(3)
if b := d.drain(); len(b) != 1 || b[0] != 3 {
t.Fatalf("batch %v", b)
}
})
}
// putBack returns items to the front of an open queue, and refuses a closed
// one.
func TestQueuePutBack(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
q := newQueue[int](false)
q.push(3)
if !q.putBack([]int{1, 2}) || q.len() != 3 {
t.Fatal("put back refused")
}
if got := q.drain(); !slices.Equal(got, []int{1, 2, 3}) {
t.Fatalf("got %v", got)
}
q.close()
if q.putBack([]int{4}) {
t.Fatal("put back into a closed queue")
}
})
}
// A queue sealed once its consumer has everything refuses what comes later,
// and one that still holds items stays open.
func TestQueueSealIfEmpty(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
q := newQueue[int](false)
q.push(1)
if q.sealIfEmpty() {
t.Fatal("sealed with an item waiting")
}
q.drain()
if !q.sealIfEmpty() || q.push(2) {
t.Fatal("a sealed queue took an item")
}
})
}
// A bound refuses what is offered under it once the queue holds as much as
// it allows, by items or by bytes, counting everything offered until the
// consumer releases it: taking it is not enough.
func TestQueueBound(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
q := newQueue[int](false)
two := bound{items: 2}
for i := range 2 {
if ok, full := q.offer(i, 10, two); !ok || full {
t.Fatalf("offer %d refused", i)
}
}
if ok, full := q.offer(2, 10, two); ok || !full {
t.Fatal("a full queue took an item")
}
// No bound: taken, and counted.
if ok, _ := q.offer(3, 10, bound{}); !ok {
t.Fatal("an offer with no bound refused")
}
if items, bytes := q.holding(); items != 3 || bytes != 30 {
t.Fatalf("holding %d, %d bytes", items, bytes)
}
q.drain()
if ok, _ := q.offer(4, 10, two); ok {
t.Fatal("room before the consumer released anything")
}
q.release(2, 20)
if ok, _ := q.offer(4, 10, two); !ok {
t.Fatal("no room after a release")
}
if items, bytes := q.holding(); items != 2 || bytes != 20 {
t.Fatalf("holding %d, %d bytes", items, bytes)
}
// The item that reaches the bytes goes in; the next waits for room.
b := newQueue[int](false)
kb := bound{bytes: 1000}
for _, size := range []int64{600, 600} {
if ok, _ := b.offer(0, size, kb); !ok {
t.Fatalf("%d bytes refused", size)
}
}
if ok, full := b.offer(0, 1, kb); ok || !full {
t.Fatal("took an item past the bytes")
}
b.release(1, 600)
if ok, _ := b.offer(0, 1, kb); !ok {
t.Fatal("no room after a release")
}
// Closed is not full.
b.close()
if ok, full := b.offer(0, 0, bound{}); ok || full {
t.Fatal("a closed queue")
}
})
}