| 106 | } |
| 107 | |
| 108 | func TestBackoff_ErrAndErrCause(t *testing.T) { |
| 109 | cause := errors.New("my cause") |
| 110 | |
| 111 | tests := map[string]struct { |
| 112 | ctx func(*testing.T) context.Context |
| 113 | expectedErr error |
| 114 | expectedErrCause error |
| 115 | }{ |
| 116 | "context deadline exceeded without cause": { |
| 117 | ctx: func(t *testing.T) context.Context { |
| 118 | ctx, cancel := context.WithDeadline(context.Background(), time.Now()) |
| 119 | t.Cleanup(cancel) |
| 120 | |
| 121 | return ctx |
| 122 | }, |
| 123 | expectedErr: context.DeadlineExceeded, |
| 124 | expectedErrCause: context.DeadlineExceeded, |
| 125 | }, |
| 126 | "context deadline exceeded with cause": { |
| 127 | ctx: func(t *testing.T) context.Context { |
| 128 | ctx, cancel := context.WithDeadlineCause(context.Background(), time.Now(), cause) |
| 129 | t.Cleanup(cancel) |
| 130 | |
| 131 | return ctx |
| 132 | }, |
| 133 | expectedErr: context.DeadlineExceeded, |
| 134 | expectedErrCause: cause, |
| 135 | }, |
| 136 | "context is canceled without cause": { |
| 137 | ctx: func(_ *testing.T) context.Context { |
| 138 | ctx, cancel := context.WithCancel(context.Background()) |
| 139 | cancel() |
| 140 | |
| 141 | return ctx |
| 142 | }, |
| 143 | expectedErr: context.Canceled, |
| 144 | expectedErrCause: context.Canceled, |
| 145 | }, |
| 146 | "context is canceled with cause": { |
| 147 | ctx: func(_ *testing.T) context.Context { |
| 148 | ctx, cancel := context.WithCancelCause(context.Background()) |
| 149 | cancel(cause) |
| 150 | |
| 151 | return ctx |
| 152 | }, |
| 153 | expectedErr: context.Canceled, |
| 154 | expectedErrCause: cause, |
| 155 | }, |
| 156 | } |
| 157 | |
| 158 | for testName, testData := range tests { |
| 159 | t.Run(testName, func(t *testing.T) { |
| 160 | b := New(testData.ctx(t), Config{}) |
| 161 | |
| 162 | // Wait until the backoff returns error. |
| 163 | require.Eventually(t, func() bool { |
| 164 | return b.Err() != nil |
| 165 | }, time.Second, 10*time.Millisecond) |