mirror of
https://github.com/wader/fq.git
synced 2024-12-23 13:22:58 +03:00
568afff3f0
Make sure there is a top cancel function before calling it. Fixes panic caused when interrupting decode before interp context has been pushed. Also cleanup confusing naming a bit. Thanks @pldin601 for reporting
36 lines
625 B
Go
36 lines
625 B
Go
package ctxstack_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/wader/fq/internal/ctxstack"
|
|
)
|
|
|
|
func TestCancelBeforePush(t *testing.T) {
|
|
// TODO: nicer way to test trigger before any push
|
|
waitTriggerFn := make(chan struct{})
|
|
triggerCh := make(chan struct{})
|
|
waitCh := make(chan struct{})
|
|
hasTriggeredOnce := false
|
|
|
|
ctxstack.New(func(stopCh chan struct{}) {
|
|
if hasTriggeredOnce {
|
|
close(stopCh)
|
|
close(waitCh)
|
|
return
|
|
}
|
|
|
|
close(waitTriggerFn)
|
|
<-triggerCh
|
|
|
|
hasTriggeredOnce = true
|
|
})
|
|
|
|
// wait for trigger func to be called
|
|
<-waitTriggerFn
|
|
// make trigger func return and cancel
|
|
close(triggerCh)
|
|
|
|
<-waitCh
|
|
}
|