MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / testWorkerPool

Function testWorkerPool

pkg/workerpool/workerpool_test.go:126–210  ·  view source on GitHub ↗
(t *testing.T, minWorkers int, maxWorkers int, queueSize int, workerIdleTimeout time.Duration)

Source from the content-addressed store, hash-verified

124}
125
126func testWorkerPool(t *testing.T, minWorkers int, maxWorkers int, queueSize int, workerIdleTimeout time.Duration) {
127 a, ctx := test.New(t)
128 ctx, cancel := context.WithCancel(ctx)
129 defer cancel()
130
131 workCtx := context.WithValue(ctx, "foo", "bar")
132
133 var workToBeDone, workDone, workFailed, duplicatedWork sync.Map
134 handlerCalls := int32(0)
135 handler := func(ctx context.Context, item int) {
136 atomic.AddInt32(&handlerCalls, 1)
137 a.So(ctx, should.HaveParentContextOrEqual, workCtx)
138 if item == -1 {
139 panic("boom")
140 }
141 if _, exists := workDone.LoadOrStore(item, 0); exists {
142 duplicatedWork.Store(item, 0)
143 }
144 }
145
146 wp := workerpool.NewWorkerPool(workerpool.Config[int]{
147 Component: &mockComponent{},
148 Context: ctx,
149 Handler: handler,
150 MinWorkers: minWorkers,
151 MaxWorkers: maxWorkers,
152 QueueSize: queueSize,
153 WorkerIdleTimeout: workerIdleTimeout,
154 })
155
156 totalWork := 100_000
157 expectedHandlerCalls := int32(0)
158 for i := range totalWork {
159 if err := wp.Publish(workCtx, i); err != nil {
160 workFailed.Store(i, 0)
161 } else {
162 workToBeDone.Store(i, 0)
163 expectedHandlerCalls++
164 }
165
166 if rand.Intn(100) < 5 {
167 if err := wp.Publish(workCtx, -1); err == nil {
168 expectedHandlerCalls++
169 }
170 }
171 }
172
173 time.Sleep(testTimeout)
174 cancel()
175 wp.Wait()
176
177 var countDone, countToBeDone, countFailed int
178 workDone.Range(func(k, v any) bool {
179 _, failed := workFailed.Load(k)
180 a.So(failed, should.BeFalse)
181
182 _, toBeDone := workToBeDone.Load(k)
183 a.So(toBeDone, should.BeTrue)

Callers 1

TestWorkerPoolFunction · 0.85

Calls 7

PublishMethod · 0.95
WaitMethod · 0.95
NewFunction · 0.92
NewWorkerPoolFunction · 0.92
RangeMethod · 0.65
FatalfMethod · 0.65
LoadMethod · 0.45

Tested by

no test coverage detected