wangpengfei
2023-06-02 064c0874e5fd041c4641ef873d1bf72ac98a184d
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
Copyright 2015 The Kubernetes Authors.
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
package workqueue_test
 
import (
    "runtime"
    "sync"
    "sync/atomic"
    "testing"
    "time"
 
    "k8s.io/apimachinery/pkg/util/wait"
    "k8s.io/client-go/util/workqueue"
)
 
func TestBasic(t *testing.T) {
    // If something is seriously wrong this test will never complete.
    q := workqueue.New()
 
    // Start producers
    const producers = 50
    producerWG := sync.WaitGroup{}
    producerWG.Add(producers)
    for i := 0; i < producers; i++ {
        go func(i int) {
            defer producerWG.Done()
            for j := 0; j < 50; j++ {
                q.Add(i)
                time.Sleep(time.Millisecond)
            }
        }(i)
    }
 
    // Start consumers
    const consumers = 10
    consumerWG := sync.WaitGroup{}
    consumerWG.Add(consumers)
    for i := 0; i < consumers; i++ {
        go func(i int) {
            defer consumerWG.Done()
            for {
                item, quit := q.Get()
                if item == "added after shutdown!" {
                    t.Errorf("Got an item added after shutdown.")
                }
                if quit {
                    return
                }
                t.Logf("Worker %v: begin processing %v", i, item)
                time.Sleep(3 * time.Millisecond)
                t.Logf("Worker %v: done processing %v", i, item)
                q.Done(item)
            }
        }(i)
    }
 
    producerWG.Wait()
    q.ShutDown()
    q.Add("added after shutdown!")
    consumerWG.Wait()
}
 
func TestAddWhileProcessing(t *testing.T) {
    q := workqueue.New()
 
    // Start producers
    const producers = 50
    producerWG := sync.WaitGroup{}
    producerWG.Add(producers)
    for i := 0; i < producers; i++ {
        go func(i int) {
            defer producerWG.Done()
            q.Add(i)
        }(i)
    }
 
    // Start consumers
    const consumers = 10
    consumerWG := sync.WaitGroup{}
    consumerWG.Add(consumers)
    for i := 0; i < consumers; i++ {
        go func(i int) {
            defer consumerWG.Done()
            // Every worker will re-add every item up to two times.
            // This tests the dirty-while-processing case.
            counters := map[interface{}]int{}
            for {
                item, quit := q.Get()
                if quit {
                    return
                }
                counters[item]++
                if counters[item] < 2 {
                    q.Add(item)
                }
                q.Done(item)
            }
        }(i)
    }
 
    producerWG.Wait()
    q.ShutDown()
    consumerWG.Wait()
}
 
func TestLen(t *testing.T) {
    q := workqueue.New()
    q.Add("foo")
    if e, a := 1, q.Len(); e != a {
        t.Errorf("Expected %v, got %v", e, a)
    }
    q.Add("bar")
    if e, a := 2, q.Len(); e != a {
        t.Errorf("Expected %v, got %v", e, a)
    }
    q.Add("foo") // should not increase the queue length.
    if e, a := 2, q.Len(); e != a {
        t.Errorf("Expected %v, got %v", e, a)
    }
}
 
func TestReinsert(t *testing.T) {
    q := workqueue.New()
    q.Add("foo")
 
    // Start processing
    i, _ := q.Get()
    if i != "foo" {
        t.Errorf("Expected %v, got %v", "foo", i)
    }
 
    // Add it back while processing
    q.Add(i)
 
    // Finish it up
    q.Done(i)
 
    // It should be back on the queue
    i, _ = q.Get()
    if i != "foo" {
        t.Errorf("Expected %v, got %v", "foo", i)
    }
 
    // Finish that one up
    q.Done(i)
 
    if a := q.Len(); a != 0 {
        t.Errorf("Expected queue to be empty. Has %v items", a)
    }
}
 
// TestGarbageCollection ensures that objects that are added then removed from the queue are
// able to be garbage collected.
func TestGarbageCollection(t *testing.T) {
    type bigObject struct {
        data []byte
    }
    leakQueue := workqueue.New()
    t.Cleanup(func() {
        // Make sure leakQueue doesn't go out of scope too early
        runtime.KeepAlive(leakQueue)
    })
    c := &bigObject{data: []byte("hello")}
    mustGarbageCollect(t, c)
    leakQueue.Add(c)
    o, _ := leakQueue.Get()
    leakQueue.Done(o)
}
 
// mustGarbageCollect asserts than an object was garbage collected by the end of the test.
// The input must be a pointer to an object.
func mustGarbageCollect(t *testing.T, i interface{}) {
    t.Helper()
    var collected int32 = 0
    runtime.SetFinalizer(i, func(x interface{}) {
        atomic.StoreInt32(&collected, 1)
    })
    t.Cleanup(func() {
        if err := wait.PollImmediate(time.Millisecond*100, wait.ForeverTestTimeout, func() (done bool, err error) {
            // Trigger GC explicitly, otherwise we may need to wait a long time for it to run
            runtime.GC()
            return atomic.LoadInt32(&collected) == 1, nil
        }); err != nil {
            t.Errorf("object was not garbage collected")
        }
    })
}