Skip to content

Commit 0398600

Browse files
authored
fix: reset condition in burst sampler (#711) (#712)
1 parent 1869fa5 commit 0398600

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

sampler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *BurstSampler) inc() uint32 {
9090
now := TimestampFunc().UnixNano()
9191
resetAt := atomic.LoadInt64(&s.resetAt)
9292
var c uint32
93-
if now > resetAt {
93+
if now >= resetAt {
9494
c = 1
9595
atomic.StoreUint32(&s.counter, c)
9696
newResetAt := now + s.Period.Nanoseconds()

sampler_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !binary_log
12
// +build !binary_log
23

34
package zerolog
@@ -103,3 +104,34 @@ func BenchmarkSamplers(b *testing.B) {
103104
})
104105
}
105106
}
107+
108+
func TestBurst(t *testing.T) {
109+
sampler := &BurstSampler{Burst: 1, Period: time.Second}
110+
111+
t0 := time.Now()
112+
now := t0
113+
mockedTime := func() time.Time {
114+
return now
115+
}
116+
117+
TimestampFunc = mockedTime
118+
defer func() { TimestampFunc = time.Now }()
119+
120+
scenario := []struct {
121+
tm time.Time
122+
want bool
123+
}{
124+
{t0, true},
125+
{t0.Add(time.Second - time.Nanosecond), false},
126+
{t0.Add(time.Second), true},
127+
{t0.Add(time.Second + time.Nanosecond), false},
128+
}
129+
130+
for i, step := range scenario {
131+
now = step.tm
132+
got := sampler.Sample(NoLevel)
133+
if got != step.want {
134+
t.Errorf("step %d (t=%s): expect %t got %t", i, step.tm, step.want, got)
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)