Fix atomic usage in tests (#6964)

This commit is contained in:
Daniel Nelson 2020-01-31 14:14:44 -08:00 committed by GitHub
parent 6cac2fb388
commit 38bc81e746
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 38 deletions

View File

@ -1,7 +1,6 @@
package models
import (
"sync/atomic"
"testing"
"time"
@ -246,7 +245,7 @@ type TestAggregator struct {
func (t *TestAggregator) Description() string { return "" }
func (t *TestAggregator) SampleConfig() string { return "" }
func (t *TestAggregator) Reset() {
atomic.StoreInt64(&t.sum, 0)
t.sum = 0
}
func (t *TestAggregator) Push(acc telegraf.Accumulator) {
@ -259,7 +258,7 @@ func (t *TestAggregator) Push(acc telegraf.Accumulator) {
func (t *TestAggregator) Add(in telegraf.Metric) {
for _, v := range in.Fields() {
if vi, ok := v.(int64); ok {
atomic.AddInt64(&t.sum, vi)
t.sum += vi
}
}
}

View File

@ -5,9 +5,6 @@ import (
"crypto/tls"
"fmt"
"regexp"
"sort"
"sync"
"sync/atomic"
"testing"
"time"
"unsafe"
@ -230,36 +227,6 @@ func TestParseConfig(t *testing.T) {
require.NotNil(t, tab)
}
func TestThrottledExecutor(t *testing.T) {
max := int64(0)
ngr := int64(0)
n := 10000
var mux sync.Mutex
results := make([]int, 0, n)
te := NewThrottledExecutor(5)
for i := 0; i < n; i++ {
func(i int) {
te.Run(context.Background(), func() {
atomic.AddInt64(&ngr, 1)
mux.Lock()
defer mux.Unlock()
results = append(results, i*2)
if ngr > max {
max = ngr
}
time.Sleep(100 * time.Microsecond)
atomic.AddInt64(&ngr, -1)
})
}(i)
}
te.Wait()
sort.Ints(results)
for i := 0; i < n; i++ {
require.Equal(t, results[i], i*2, "Some jobs didn't run")
}
require.Equal(t, int64(5), max, "Wrong number of goroutines spawned")
}
func TestMaxQuery(t *testing.T) {
// Don't run test on 32-bit machines due to bug in simulator.
// https://github.com/vmware/govmomi/issues/1330

View File

@ -18,8 +18,8 @@ var (
)
func newTrackingID() telegraf.TrackingID {
atomic.AddUint64(&lastID, 1)
return telegraf.TrackingID(lastID)
id := atomic.AddUint64(&lastID, 1)
return telegraf.TrackingID(id)
}
// Metric defines a single point measurement