(t *testing.T)
| 263 | } |
| 264 | |
| 265 | func 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) |
nothing calls this directly
no test coverage detected