MCPcopy
hub / github.com/redis/go-redis / TestWantConnQueue_ConcurrentAccess

Function TestWantConnQueue_ConcurrentAccess

internal/pool/want_conn_test.go:265–324  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

263}
264
265func TestWantConnQueue_ConcurrentAccess(t *testing.T) {
266 q := newWantConnQueue()
267 const numWorkers = 10
268 const itemsPerWorker = 100
269
270 var wg sync.WaitGroup
271
272 // Start enqueuers
273 for i := 0; i < numWorkers; i++ {
274 wg.Add(1)
275 go func() {
276 defer wg.Done()
277 for j := 0; j < itemsPerWorker; j++ {
278 w := &wantConn{
279 ctx: context.Background(),
280 result: make(chan wantConnResult, 1),
281 }
282 q.enqueue(w)
283 }
284 }()
285 }
286
287 // Start dequeuers
288 dequeued := make(chan *wantConn, numWorkers*itemsPerWorker)
289 for i := 0; i < numWorkers; i++ {
290 wg.Add(1)
291 go func() {
292 defer wg.Done()
293 for j := 0; j < itemsPerWorker; j++ {
294 for {
295 if item, ok := q.dequeue(); ok {
296 dequeued <- item
297 break
298 }
299 // Small delay to avoid busy waiting
300 time.Sleep(time.Microsecond)
301 }
302 }
303 }()
304 }
305
306 wg.Wait()
307 close(dequeued)
308
309 // Count dequeued items
310 count := 0
311 for range dequeued {
312 count++
313 }
314
315 expectedCount := numWorkers * itemsPerWorker
316 if count != expectedCount {
317 t.Errorf("dequeued %d items, want %d", count, expectedCount)
318 }
319
320 // Queue should be empty
321 if item, ok := q.dequeue(); ok {
322 t.Errorf("queue should be empty but got item: %v", item)

Callers

nothing calls this directly

Calls 5

newWantConnQueueFunction · 0.85
enqueueMethod · 0.80
dequeueMethod · 0.80
WaitMethod · 0.80
AddMethod · 0.65

Tested by

no test coverage detected