package origin import ( "context" "errors" "fmt" "testing" "time" "github.com/google/uuid" "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" "github.com/cloudflare/cloudflared/logger" tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs" ) func testConfig(logger logger.Service) *TunnelConfig { metrics := TunnelMetrics{} metrics.authSuccess = prometheus.NewCounter( prometheus.CounterOpts{ Namespace: metricsNamespace, Subsystem: tunnelSubsystem, Name: "tunnel_authenticate_success", Help: "Count of successful tunnel authenticate", }, ) metrics.authFail = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: metricsNamespace, Subsystem: tunnelSubsystem, Name: "tunnel_authenticate_fail", Help: "Count of tunnel authenticate errors by type", }, []string{"error"}, ) return &TunnelConfig{Logger: logger, Metrics: &metrics} } func TestRefreshAuthBackoff(t *testing.T) { logger := logger.NewOutputWriter(logger.NewMockWriteManager()) var wait time.Duration timeAfter = func(d time.Duration) <-chan time.Time { wait = d return time.After(d) } s, err := NewSupervisor(testConfig(logger), uuid.New()) if !assert.NoError(t, err) { t.FailNow() } backoff := &BackoffHandler{MaxRetries: 3} auth := func(ctx context.Context, n int) (tunnelpogs.AuthOutcome, error) { return nil, fmt.Errorf("authentication failure") } // authentication failures should consume the backoff for i := uint(0); i < backoff.MaxRetries; i++ { retryChan, err := s.refreshAuth(context.Background(), backoff, auth) assert.NoError(t, err) assert.NotNil(t, retryChan) assert.Equal(t, (1<