Skip to content

Commit

Permalink
stats: rm races test
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Aug 4, 2022
1 parent 63dd676 commit 5683cb3
Showing 1 changed file with 0 additions and 75 deletions.
75 changes: 0 additions & 75 deletions internal/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"fmt"
"net"
"os"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/golibs/testutil"
Expand Down Expand Up @@ -163,76 +161,3 @@ func TestStatsCollector(t *testing.T) {
}
})
}

func TestStats_races(t *testing.T) {
var r uint32
idGen := func() (id uint32) { return atomic.LoadUint32(&r) }
conf := Config{
UnitID: idGen,
Filename: "./stats.db",
LimitDays: 1,
}

s, err := New(conf)
require.NoError(t, err)

s.Start()
startTime := time.Now()
testutil.CleanupAndRequireSuccess(t, func() (err error) {
s.clear()
s.Close()

return os.Remove(conf.Filename)
})

waitAtomically := func(control *uint32) {
for atomic.LoadUint32(control) == 0 {
}
}
writeFunc := func(wg *sync.WaitGroup, control *uint32, i int) {
e := Entry{
Domain: fmt.Sprintf("example-%d.org", i),
Client: fmt.Sprintf("client_%d", i),
Result: Result(i)%(resultLast-1) + 1,
Time: uint32(time.Since(startTime).Milliseconds()),
}

wg.Done()
waitAtomically(control)

s.Update(e)
}
readFunc := func(wg *sync.WaitGroup, control *uint32) {
wg.Done()
waitAtomically(control)

_, _ = s.getData()
}

const (
roundsNum = 3

writersNum = 10
readersNum = 5
)

for round := 0; round < roundsNum; round++ {
atomic.StoreUint32(&r, uint32(round))

wg := &sync.WaitGroup{}
var control uint32

for i := 0; i < writersNum; i++ {
wg.Add(1)
go writeFunc(wg, &control, i)
}

for i := 0; i < readersNum; i++ {
wg.Add(1)
go readFunc(wg, &control)
}

wg.Wait()
atomic.StoreUint32(&control, 1)
}
}

0 comments on commit 5683cb3

Please sign in to comment.