mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-11-04 08:04:10 +00:00
The sampler read the counter and then reset it in two steps. Requests recorded between the steps were lost. The sampler now swaps the counter to zero in one atomic step.
44 lines
908 B
Go
44 lines
908 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Pins theorems/Theorems/RpsSample.lean: no recorded request is dropped by sample().
|
|
func TestRPSTracker_SampleKeepsConcurrentRequests(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
tr := NewRPSTracker(ctx)
|
|
tr.Shutdown() // stop the background sampler; sample() is driven by the test
|
|
|
|
const writers, perWriter = 8, 20000
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < writers; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < perWriter; j++ {
|
|
tr.RecordRequest()
|
|
}
|
|
}()
|
|
}
|
|
var sampled int64
|
|
done := make(chan struct{})
|
|
go func() { wg.Wait(); close(done) }()
|
|
loop:
|
|
for {
|
|
select {
|
|
case <-done:
|
|
break loop
|
|
default:
|
|
sampled += tr.sampleCount()
|
|
}
|
|
}
|
|
sampled += tr.sampleCount()
|
|
assert.Equal(t, int64(writers*perWriter), sampled)
|
|
}
|