mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
Unsubscribing closes the channel after dropping it from the map. The fan-out copied the channel and sent after releasing the lock, so the close could land first and panic the process. The send now happens while the lock is still held.
24 lines
412 B
Go
24 lines
412 B
Go
package server
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestLogSinkUnsubscribeDuringAddDoesNotPanic(t *testing.T) {
|
|
s := &logSinkT{cap: 8, subs: map[chan LogLine]struct{}{}, out: io.Discard}
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 32; i++ {
|
|
_, unsub := s.subscribe()
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
unsub()
|
|
}()
|
|
s.add(LogLine{Text: "line"})
|
|
}
|
|
wg.Wait()
|
|
s.add(LogLine{Text: "after"})
|
|
}
|