github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/strategy_exponential_backoff_test.go (about) 1 package actor 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestExponentialBackoffStrategy_setFailureCount(t *testing.T) { 11 cases := []struct { 12 n string 13 ft time.Duration 14 fc int 15 expected int 16 }{ 17 {n: "failure outside window; increment count", ft: 11 * time.Second, fc: 10, expected: 1}, 18 {n: "failure inside window; increment count", ft: 9 * time.Second, fc: 10, expected: 11}, 19 } 20 21 for _, tc := range cases { 22 t.Run(tc.n, func(t *testing.T) { 23 s := &exponentialBackoffStrategy{backoffWindow: 10 * time.Second} 24 rs := &RestartStatistics{[]time.Time{}} 25 for i := 0; i < tc.fc; i++ { 26 rs.failureTimes = append(rs.failureTimes, time.Now().Add(-tc.ft)) 27 } 28 29 s.setFailureCount(rs) 30 assert.Equal(t, tc.expected, rs.FailureCount()) 31 }) 32 } 33 } 34 35 func TestExponentialBackoffStrategy_IncrementsFailureCount(t *testing.T) { 36 rs := NewRestartStatistics() 37 s := &exponentialBackoffStrategy{backoffWindow: 10 * time.Second} 38 39 s.setFailureCount(rs) 40 s.setFailureCount(rs) 41 s.setFailureCount(rs) 42 43 assert.Equal(t, 3, rs.FailureCount()) 44 } 45 46 func TestExponentialBackoffStrategy_ResetsFailureCount(t *testing.T) { 47 rs := NewRestartStatistics() 48 for i := 0; i < 10; i++ { 49 rs.failureTimes = append(rs.failureTimes, time.Now().Add(-11*time.Second)) 50 } 51 s := &exponentialBackoffStrategy{backoffWindow: 10 * time.Second, initialBackoff: 1 * time.Second} 52 53 s.setFailureCount(rs) 54 55 assert.Equal(t, 1, rs.FailureCount()) 56 }