From 88b09cf18c3deb5d720ad759242574c2fadfa9bd Mon Sep 17 00:00:00 2001 From: Steven Soroka Date: Wed, 24 Jun 2020 13:29:44 -0400 Subject: [PATCH] fix race --- testutil/accumulator.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/testutil/accumulator.go b/testutil/accumulator.go index 6e5148ef7..870eb9e04 100644 --- a/testutil/accumulator.go +++ b/testutil/accumulator.go @@ -55,6 +55,8 @@ func (a *Accumulator) NMetrics() uint64 { } func (a *Accumulator) GetTelegrafMetrics() []telegraf.Metric { + a.Lock() + defer a.Unlock() metrics := []telegraf.Metric{} for _, m := range a.Metrics { metrics = append(metrics, FromTestMetric(m)) @@ -251,6 +253,8 @@ func (a *Accumulator) SetDebug(debug bool) { // Get gets the specified measurement point from the accumulator func (a *Accumulator) Get(measurement string) (*Metric, bool) { + a.Lock() + defer a.Unlock() for _, p := range a.Metrics { if p.Measurement == measurement { return p, true @@ -261,6 +265,8 @@ func (a *Accumulator) Get(measurement string) (*Metric, bool) { } func (a *Accumulator) HasTag(measurement string, key string) bool { + a.Lock() + defer a.Unlock() for _, p := range a.Metrics { if p.Measurement == measurement { _, ok := p.Tags[key] @@ -271,6 +277,8 @@ func (a *Accumulator) HasTag(measurement string, key string) bool { } func (a *Accumulator) TagSetValue(measurement string, key string) string { + a.Lock() + defer a.Unlock() for _, p := range a.Metrics { if p.Measurement == measurement { v, ok := p.Tags[key] @@ -283,6 +291,8 @@ func (a *Accumulator) TagSetValue(measurement string, key string) string { } func (a *Accumulator) TagValue(measurement string, key string) string { + a.Lock() + defer a.Unlock() for _, p := range a.Metrics { if p.Measurement == measurement { v, ok := p.Tags[key] @@ -323,13 +333,13 @@ func (a *Accumulator) NFields() int { // Wait waits for the given number of metrics to be added to the accumulator. func (a *Accumulator) Wait(n int) { a.Lock() + defer a.Unlock() if a.Cond == nil { a.Cond = sync.NewCond(&a.Mutex) } for int(a.NMetrics()) < n { a.Cond.Wait() } - a.Unlock() } // WaitError waits for the given number of errors to be added to the accumulator.