forked from Silverfish/proton-bridge
test: Fix deadlock in chToType
Make sure the go routine is cancelled if there is a new event, but no one is reading it after a call to done.
This commit is contained in:
@ -1057,6 +1057,7 @@ func getConnectedUserIDs(t *testing.T, b *bridge.Bridge) []string {
|
|||||||
func chToType[In, Out any](inCh <-chan In, done func()) (<-chan Out, func()) {
|
func chToType[In, Out any](inCh <-chan In, done func()) (<-chan Out, func()) {
|
||||||
outCh := make(chan Out)
|
outCh := make(chan Out)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
go func() {
|
go func() {
|
||||||
defer close(outCh)
|
defer close(outCh)
|
||||||
|
|
||||||
@ -1066,11 +1067,19 @@ func chToType[In, Out any](inCh <-chan In, done func()) (<-chan Out, func()) {
|
|||||||
panic(fmt.Sprintf("unexpected type %T", in))
|
panic(fmt.Sprintf("unexpected type %T", in))
|
||||||
}
|
}
|
||||||
|
|
||||||
outCh <- out
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
|
||||||
|
case outCh <- out:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return outCh, done
|
return outCh, func() {
|
||||||
|
cancel()
|
||||||
|
done()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type eventWaiter struct {
|
type eventWaiter struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user