TestQueue_SendCloseRace is a regression test for the race condition between Send and Close. Without proper synchronization, concurrent Send and Close calls could cause a "send on closed channel" panic. Run with: go test -race -run TestQueue_SendCloseRace
(t *testing.T)
| 216 | // calls could cause a "send on closed channel" panic. |
| 217 | // Run with: go test -race -run TestQueue_SendCloseRace |
| 218 | func TestQueue_SendCloseRace(t *testing.T) { |
| 219 | for i := range 100 { |
| 220 | t.Run(fmt.Sprintf("iteration_%d", i), func(t *testing.T) { |
| 221 | // Use large buffer to avoid blocking on channel send while holding RLock |
| 222 | q := New[*testMessage]("test-race", 1000) |
| 223 | q.RegisterHandler(func(ctx context.Context, msg *testMessage) error { |
| 224 | return nil |
| 225 | }) |
| 226 | |
| 227 | var wg sync.WaitGroup |
| 228 | |
| 229 | // Use cancellable context so senders can exit when Close is called |
| 230 | ctx, cancel := context.WithCancel(context.Background()) |
| 231 | |
| 232 | // Start multiple senders |
| 233 | for j := range 10 { |
| 234 | wg.Add(1) |
| 235 | go func(id int) { |
| 236 | defer wg.Done() |
| 237 | for k := range 100 { |
| 238 | q.Send(ctx, &testMessage{ID: id*1000 + k}) |
| 239 | } |
| 240 | }(j) |
| 241 | } |
| 242 | |
| 243 | // Close while senders are still running |
| 244 | go func() { |
| 245 | time.Sleep(time.Microsecond * 10) |
| 246 | cancel() // Cancel context to unblock any waiting senders |
| 247 | q.Close() |
| 248 | }() |
| 249 | |
| 250 | wg.Wait() |
| 251 | }) |
| 252 | } |
| 253 | } |
nothing calls this directly
no test coverage detected