Don't wait for a chan that will never close

This commit is contained in:
Marco Munizaga 2022-07-06 10:01:46 -07:00
parent fe31993a76
commit 09011ebc78
3 changed files with 21 additions and 4 deletions

16
obs/stats_test.go Normal file
View File

@ -0,0 +1,16 @@
package obs_test
import (
"testing"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
"github.com/libp2p/go-libp2p-resource-manager/obs"
)
func TestTraceReporterStartAndClose(t *testing.T) {
rcmgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(rcmgr.DefaultLimits.AutoScale()), rcmgr.WithTraceReporter(obs.StatsTraceReporter{}))
if err != nil {
t.Fatal(err)
}
defer rcmgr.Close()
}

View File

@ -1004,6 +1004,7 @@ func TestResourceManagerWithAllowlist(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer rcmgr.Close()
ableToGetAllowlist := GetAllowlist(rcmgr)
if ableToGetAllowlist == nil {

View File

@ -19,7 +19,7 @@ type trace struct {
ctx context.Context
cancel func()
closed chan struct{}
wg sync.WaitGroup
mx sync.Mutex
done bool
@ -232,7 +232,7 @@ func (t *trace) push(evt TraceEvt) {
}
func (t *trace) backgroundWriter(out io.WriteCloser) {
defer close(t.closed)
defer t.wg.Done()
defer out.Close()
gzOut := gzip.NewWriter(out)
@ -315,7 +315,6 @@ func (t *trace) Start(limits Limiter) error {
}
t.ctx, t.cancel = context.WithCancel(context.Background())
t.closed = make(chan struct{})
if t.path != "" {
out, err := os.OpenFile(t.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
@ -323,6 +322,7 @@ func (t *trace) Start(limits Limiter) error {
return nil
}
t.wg.Add(1)
go t.backgroundWriter(out)
}
@ -350,7 +350,7 @@ func (t *trace) Close() error {
t.done = true
t.mx.Unlock()
<-t.closed
t.wg.Wait()
return nil
}