| 1 | package remote |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestBackoffDelayCeilingGrowsAndCaps(t *testing.T) { |
| 9 | p := BackoffPolicy{Initial: time.Second, Factor: 2, Max: 60 * time.Second} |
| 10 | want := []time.Duration{ |
| 11 | 1 * time.Second, |
| 12 | 2 * time.Second, |
| 13 | 4 * time.Second, |
| 14 | 8 * time.Second, |
| 15 | 16 * time.Second, |
| 16 | 32 * time.Second, |
| 17 | 60 * time.Second, // 64 capped |
| 18 | 60 * time.Second, |
| 19 | } |
| 20 | for i, w := range want { |
| 21 | if got := p.delay(i); got != w { |
| 22 | t.Errorf("delay(%d) = %v, want %v", i, got, w) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestBackoffDefaults(t *testing.T) { |
| 28 | var p BackoffPolicy |
| 29 | if p.delay(0) != time.Second { |
| 30 | t.Errorf("default initial = %v", p.delay(0)) |
| 31 | } |
| 32 | if p.delay(100) != 60*time.Second { |
| 33 | t.Errorf("default cap = %v", p.delay(100)) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestClassifyDialError(t *testing.T) { |
| 38 | authy := classifyDialError(errString("ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain")) |
| 39 | if !isAuthErr(authy) { |
| 40 | t.Errorf("auth failure not classified as ErrAuthFailed: %v", authy) |
| 41 | } |
| 42 | transient := classifyDialError(errString("dial tcp 10.0.0.1:22: connect: connection refused")) |
| 43 | if isAuthErr(transient) { |
| 44 | t.Errorf("transient error misclassified as auth: %v", transient) |
| 45 | } |
| 46 | if classifyDialError(nil) != nil { |
| 47 | t.Error("nil error should classify to nil") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | type errString string |
| 52 | |
| 53 | func (e errString) Error() string { return string(e) } |
| 54 | |
| 55 | func isAuthErr(err error) bool { |
| 56 | type iser interface{ Is(error) bool } |
| 57 | if x, ok := err.(iser); ok { |
| 58 | return x.Is(ErrAuthFailed) |
| 59 | } |
| 60 | return false |
| 61 | } |
| 62 |