package server_test import ( "testing" "time" "github.com/stretchr/testify/require" "sneak.berlin/go/webhooker/internal/server" ) // TestSentryFlushBudget covers the clamp that keeps the Sentry flush // from spending the tail hooks' share of the fx stop budget. // sentry.Flush ignores the stop context, so without the clamp a // stalled flush adds its whole timeout on top of the HTTP drain. func TestSentryFlushBudget(t *testing.T) { t.Parallel() tests := []struct { name string remaining time.Duration want time.Duration }{ { name: "full drain leaves only the reserve", remaining: server.TailHookReserve, want: 0, }, { name: "expired budget", remaining: -time.Second, want: 0, }, { name: "sliver above the reserve is not worth it", remaining: server.TailHookReserve + 10*time.Millisecond, want: 0, }, { name: "partial flush when some room is left", remaining: server.TailHookReserve + time.Second, want: time.Second, }, { name: "capped at the nominal timeout", remaining: time.Hour, want: 2 * time.Second, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() require.Equal( t, tt.want, server.SentryFlushBudget(tt.remaining), ) }) } }