Skip to content

Commit

Permalink
tests: fix network metrics race condition in tests (#4529)
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy authored Sep 12, 2022
1 parent 22b6101 commit 794a63d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
3 changes: 1 addition & 2 deletions network/wsPeer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package network

import (
"encoding/binary"
"fmt"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestTagCounterFiltering(t *testing.T) {
"networkMessageSentByTag": networkMessageSentByTag,
}
for name, tag := range tagCounterTags {
t.Run(fmt.Sprintf("%s", name), func(t *testing.T) {
t.Run(name, func(t *testing.T) {
require.NotZero(t, len(tag.AllowedTags))
tag.Add("TEST_TAG", 1)
b := strings.Builder{}
Expand Down
9 changes: 4 additions & 5 deletions util/metrics/tagcounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ func (tc *TagCounter) Add(tag string, val uint64) {
var st []uint64
if len(tc.storage) > 0 {
st = tc.storage[len(tc.storage)-1]
//fmt.Printf("new tag %v, old block\n", tag)
}
if tc.storagePos > (len(st) - 1) {
//fmt.Printf("new tag %v, new block\n", tag)
st = make([]uint64, 16)
tc.storagePos = 0
tc.storage = append(tc.storage, st)
}
newtags[tag] = &(st[tc.storagePos])
//fmt.Printf("tag %v = %p\n", tag, newtags[tag])
tc.storagePos++
tc.tags = newtags
tc.tagptr.Store(newtags)
Expand Down Expand Up @@ -155,7 +152,8 @@ func (tc *TagCounter) WriteMetric(buf *strings.Builder, parentLabels string) {
buf.WriteRune('}')
}
buf.WriteRune(' ')
buf.WriteString(strconv.FormatUint(*tagcount, 10))
count := atomic.LoadUint64(tagcount)
buf.WriteString(strconv.FormatUint(count, 10))
buf.WriteRune('\n')
}
}
Expand All @@ -179,6 +177,7 @@ func (tc *TagCounter) AddMetric(values map[string]float64) {
} else {
name = tc.Name + "_" + tag
}
values[sanitizeTelemetryName(name)] = float64(*tagcount)
count := atomic.LoadUint64(tagcount)
values[sanitizeTelemetryName(name)] = float64(count)
}
}

0 comments on commit 794a63d

Please sign in to comment.