MCPcopy Create free account
hub / github.com/cli/cli / TestServiceSampling

Function TestServiceSampling

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

Source from the content-addressed store, hash-verified

430}
431
432func TestServiceSampling(t *testing.T) {
433 tests := []struct {
434 name string
435 sampleRate int
436 sampleBucket byte
437 wantPayloads int
438 }{
439 {
440 name: "sampleRate 0 sends all events",
441 sampleRate: 0,
442 sampleBucket: 99,
443 wantPayloads: 1,
444 },
445 {
446 name: "sampleRate 100 sends all events regardless of bucket",
447 sampleRate: 100,
448 sampleBucket: 99,
449 wantPayloads: 1,
450 },
451 {
452 name: "bucket below sampleRate sends events",
453 sampleRate: 50,
454 sampleBucket: 49,
455 wantPayloads: 1,
456 },
457 {
458 name: "bucket at sampleRate drops events",
459 sampleRate: 50,
460 sampleBucket: 50,
461 wantPayloads: 0,
462 },
463 {
464 name: "bucket above sampleRate drops events",
465 sampleRate: 1,
466 sampleBucket: 50,
467 wantPayloads: 0,
468 },
469 }
470 for _, tt := range tests {
471 t.Run(tt.name, func(t *testing.T) {
472 // Given a configured sample rate and a deterministic sampling bucket
473 t.Cleanup(stubDeviceID("test-device"))
474 var payloads []SendTelemetryPayload
475 svc := NewService(func(p SendTelemetryPayload) { payloads = append(payloads, p) }, WithSampleRate(tt.sampleRate))
476 // Fix the random bucket so sampling boundaries can be asserted through delivery.
477 svc.(*service).sampleBucket = tt.sampleBucket
478 svc.Record(ghtelemetry.Event{Type: "test"})
479
480 // When the invocation finishes
481 svc.Finish()
482
483 // Then the sampling policy determines whether a payload is delivered
484 assert.Len(t, payloads, tt.wantPayloads)
485 })
486 }
487}
488
489func TestServiceReducingSampleRateExcludesInvocation(t *testing.T) {

Callers

nothing calls this directly

Calls 7

FinishMethod · 0.95
stubDeviceIDFunction · 0.85
NewServiceFunction · 0.85
WithSampleRateFunction · 0.85
RunMethod · 0.65
RecordMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected