MCPcopy
hub / github.com/uber-go/zap / TestSamplerConcurrent

Function TestSamplerConcurrent

zapcore/sampler_test.go:170–249  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

168func (*countingCore) Sync() error { return nil }
169
170func TestSamplerConcurrent(t *testing.T) {
171 const (
172 logsPerTick = 10
173 numMessages = 5
174 numTicks = 25
175 numGoroutines = 10
176 tick = 10 * time.Millisecond
177
178 // We'll make a total of,
179 // (numGoroutines * numTicks * logsPerTick * 2) log attempts
180 // with numMessages unique messages.
181 numLogAttempts = numGoroutines * logsPerTick * numTicks * 2
182 // Of those, we'll accept (logsPerTick * numTicks) entries
183 // for each unique message.
184 expectedCount = numMessages * logsPerTick * numTicks
185 // The rest will be dropped.
186 expectedDropped = numLogAttempts - expectedCount
187 )
188
189 clock := ztest.NewMockClock()
190
191 cc := &countingCore{}
192
193 hook, dropped, sampled := makeSamplerCountingHook()
194 sampler := NewSamplerWithOptions(cc, tick, logsPerTick, 100000, SamplerHook(hook))
195
196 stop := make(chan struct{})
197 var wg sync.WaitGroup
198 for i := 0; i < numGoroutines; i++ {
199 wg.Add(1)
200 go func(i int, ticker *time.Ticker) {
201 defer wg.Done()
202 defer ticker.Stop()
203
204 for {
205 select {
206 case <-stop:
207 return
208
209 case <-ticker.C:
210 for j := 0; j < logsPerTick*2; j++ {
211 msg := fmt.Sprintf("msg%v", i%numMessages)
212 ent := Entry{
213 Level: DebugLevel,
214 Message: msg,
215 Time: clock.Now(),
216 }
217 if ce := sampler.Check(ent, nil); ce != nil {
218 ce.Write()
219 }
220
221 // Give a chance for other goroutines to run.
222 runtime.Gosched()
223 }
224 }
225 }
226 }(i, clock.NewTicker(tick))
227 }

Callers

nothing calls this directly

Calls 11

NowMethod · 0.95
NewTickerMethod · 0.95
AddMethod · 0.95
NewMockClockFunction · 0.92
NewSamplerWithOptionsFunction · 0.85
SamplerHookFunction · 0.85
StopMethod · 0.80
makeSamplerCountingHookFunction · 0.70
CheckMethod · 0.65
WriteMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…