MCPcopy
hub / github.com/keploy/keploy / TestSendToOutChanDropSampling

Function TestSendToOutChanDropSampling

pkg/agent/proxy/syncMock/syncMock_test.go:917–944  ·  view source on GitHub ↗

TestSendToOutChanDropSampling validates the sampling rule: the first drop logs, and subsequent drops only log every sendDropSampleRate entries. Without the sampling, a stuck consumer would flood the log and further starve the producer — the specific anti-pattern the windows-redirector work surfaced.

(t *testing.T)

Source from the content-addressed store, hash-verified

915// surfaced. Uses direct atomic writes to avoid 200ms * N real
916// timeouts in the test.
917func TestSendToOutChanDropSampling(t *testing.T) {
918 t.Parallel()
919
920 mgr := &SyncMockManager{
921 buffer: make([]*models.Mock, 0, defaultMockBufferCapacity),
922 }
923 core, logs := observer.New(zap.ErrorLevel)
924 mgr.SetLogger(zap.New(core))
925
926 // Simulate the drop counter hitting values around the
927 // sampling boundary without paying the per-drop wall-clock
928 // cost. We reach directly into dropCount because the whole
929 // point of this test is the logging cadence, not the send
930 // path's timing.
931 for i := uint64(1); i <= 2050; i++ {
932 n := mgr.dropCount.Add(1)
933 if n == 1 || n%sendDropSampleRate == 0 {
934 mgr.dropLogger().Error("test-sampled-drop",
935 zap.Uint64("dropsSoFar", n),
936 )
937 }
938 }
939
940 // Expected emits: n=1, n=1024, n=2048 → 3 entries.
941 if got := logs.Len(); got != 3 {
942 t.Fatalf("expected 3 sampled drop logs (n=1, 1024, 2048), got %d", got)
943 }
944}
945
946// TestSetLoggerNilFallsBackToNop ensures the drop path never
947// panics even if the host process never calls SetLogger or

Callers

nothing calls this directly

Calls 5

SetLoggerMethod · 0.95
dropLoggerMethod · 0.95
AddMethod · 0.80
NewMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected