(t *testing.T)
| 168 | func (*countingCore) Sync() error { return nil } |
| 169 | |
| 170 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…