mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 06:25:44 +03:00
Pull request: stats: add races test
Merge in DNS/adguard-home from 4358-stats-races-test to master Squashed commit of the following: commit ac0a9c63a4577264b3f0c9ef1345a6410cb381a9 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 5 16:15:31 2022 +0300 stats: immp test, docs commit 7c7017215c3634d240996fb3317d82e41a5e9005 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 17 15:33:16 2022 +0300 stats: add races test
This commit is contained in:
parent
1fb043768e
commit
e545f3bdb7
1 changed files with 79 additions and 0 deletions
|
@ -1,8 +1,14 @@
|
||||||
package stats
|
package stats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/golibs/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -24,3 +30,76 @@ 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: filepath.Join(t.TempDir(), "./stats.db"),
|
||||||
|
LimitDays: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := New(conf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
s.Start()
|
||||||
|
startTime := time.Now()
|
||||||
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
||||||
|
|
||||||
|
type signal = struct{}
|
||||||
|
|
||||||
|
writeFunc := func(start, fin *sync.WaitGroup, waitCh <-chan unit, 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()),
|
||||||
|
}
|
||||||
|
|
||||||
|
start.Done()
|
||||||
|
defer fin.Done()
|
||||||
|
|
||||||
|
<-waitCh
|
||||||
|
|
||||||
|
s.Update(e)
|
||||||
|
}
|
||||||
|
readFunc := func(start, fin *sync.WaitGroup, waitCh <-chan unit) {
|
||||||
|
start.Done()
|
||||||
|
defer fin.Done()
|
||||||
|
|
||||||
|
<-waitCh
|
||||||
|
|
||||||
|
_, _ = s.getData(24)
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
roundsNum = 3
|
||||||
|
|
||||||
|
writersNum = 10
|
||||||
|
readersNum = 5
|
||||||
|
)
|
||||||
|
|
||||||
|
for round := 0; round < roundsNum; round++ {
|
||||||
|
atomic.StoreUint32(&r, uint32(round))
|
||||||
|
|
||||||
|
startWG, finWG := &sync.WaitGroup{}, &sync.WaitGroup{}
|
||||||
|
waitCh := make(chan unit)
|
||||||
|
|
||||||
|
for i := 0; i < writersNum; i++ {
|
||||||
|
startWG.Add(1)
|
||||||
|
finWG.Add(1)
|
||||||
|
go writeFunc(startWG, finWG, waitCh, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < readersNum; i++ {
|
||||||
|
startWG.Add(1)
|
||||||
|
finWG.Add(1)
|
||||||
|
go readFunc(startWG, finWG, waitCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
startWG.Wait()
|
||||||
|
close(waitCh)
|
||||||
|
finWG.Wait()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue