MCPcopy
hub / github.com/cli/cli / TestServiceSampling

Function TestServiceSampling

internal/telemetry/telemetry_test.go:496–614  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

494}
495
496func TestServiceSampling(t *testing.T) {
497 t.Run("sampleRate 0 sends all events", func(t *testing.T) {
498 t.Cleanup(stubDeviceID("test-device"))
499
500 var captured SendTelemetryPayload
501 svc := newService(func(p SendTelemetryPayload) { captured = p }, nil)
502 svc.sampleRate = 0
503 svc.sampleBucket = 99
504
505 svc.Record(ghtelemetry.Event{Type: "test"})
506 svc.Flush()
507
508 require.Len(t, captured.Events, 1)
509 })
510
511 t.Run("sampleRate 100 sends all events regardless of bucket", func(t *testing.T) {
512 t.Cleanup(stubDeviceID("test-device"))
513
514 var captured SendTelemetryPayload
515 svc := newService(func(p SendTelemetryPayload) { captured = p }, nil)
516 svc.sampleRate = 100
517 svc.sampleBucket = 99
518
519 svc.Record(ghtelemetry.Event{Type: "test"})
520 svc.Flush()
521
522 require.Len(t, captured.Events, 1)
523 })
524
525 t.Run("bucket below sampleRate sends events", func(t *testing.T) {
526 t.Cleanup(stubDeviceID("test-device"))
527
528 var captured SendTelemetryPayload
529 svc := newService(func(p SendTelemetryPayload) { captured = p }, nil)
530 svc.sampleRate = 50
531 svc.sampleBucket = 49 // below rate, should be included
532
533 svc.Record(ghtelemetry.Event{Type: "test"})
534 svc.Flush()
535
536 require.Len(t, captured.Events, 1)
537 })
538
539 t.Run("bucket at sampleRate drops events", func(t *testing.T) {
540 t.Cleanup(stubDeviceID("test-device"))
541
542 called := false
543 svc := newService(func(SendTelemetryPayload) { called = true }, nil)
544 svc.sampleRate = 50
545 svc.sampleBucket = 50 // at rate boundary, should be excluded
546
547 svc.Record(ghtelemetry.Event{Type: "test"})
548 svc.Flush()
549
550 assert.False(t, called, "flusher should not be called when bucket >= sampleRate")
551 })
552
553 t.Run("bucket above sampleRate drops events", func(t *testing.T) {

Callers

nothing calls this directly

Calls 10

FlushMethod · 0.95
stubDeviceIDFunction · 0.85
newServiceFunction · 0.85
NewServiceFunction · 0.85
WithSampleRateFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65
RecordMethod · 0.65
LenMethod · 0.65
SetSampleRateMethod · 0.65

Tested by

no test coverage detected