Fix atomic usage in tests (#6964)
This commit is contained in:
parent
6cac2fb388
commit
38bc81e746
|
@ -1,7 +1,6 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -246,7 +245,7 @@ type TestAggregator struct {
|
||||||
func (t *TestAggregator) Description() string { return "" }
|
func (t *TestAggregator) Description() string { return "" }
|
||||||
func (t *TestAggregator) SampleConfig() string { return "" }
|
func (t *TestAggregator) SampleConfig() string { return "" }
|
||||||
func (t *TestAggregator) Reset() {
|
func (t *TestAggregator) Reset() {
|
||||||
atomic.StoreInt64(&t.sum, 0)
|
t.sum = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestAggregator) Push(acc telegraf.Accumulator) {
|
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) {
|
func (t *TestAggregator) Add(in telegraf.Metric) {
|
||||||
for _, v := range in.Fields() {
|
for _, v := range in.Fields() {
|
||||||
if vi, ok := v.(int64); ok {
|
if vi, ok := v.(int64); ok {
|
||||||
atomic.AddInt64(&t.sum, vi)
|
t.sum += vi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,6 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -230,36 +227,6 @@ func TestParseConfig(t *testing.T) {
|
||||||
require.NotNil(t, tab)
|
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) {
|
func TestMaxQuery(t *testing.T) {
|
||||||
// Don't run test on 32-bit machines due to bug in simulator.
|
// Don't run test on 32-bit machines due to bug in simulator.
|
||||||
// https://github.com/vmware/govmomi/issues/1330
|
// https://github.com/vmware/govmomi/issues/1330
|
||||||
|
|
|
@ -18,8 +18,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func newTrackingID() telegraf.TrackingID {
|
func newTrackingID() telegraf.TrackingID {
|
||||||
atomic.AddUint64(&lastID, 1)
|
id := atomic.AddUint64(&lastID, 1)
|
||||||
return telegraf.TrackingID(lastID)
|
return telegraf.TrackingID(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metric defines a single point measurement
|
// Metric defines a single point measurement
|
||||||
|
|
Loading…
Reference in New Issue