|
| 1 | +package orchestrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/open-source-cloud/devstack/internal/provision" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTransientConnErr(t *testing.T) { |
| 13 | + transient := []string{ |
| 14 | + "connect to shared postgres: read tcp 127.0.0.1:46820->127.0.0.1:45432: read: connection reset by peer", |
| 15 | + "failed to receive message: EOF", |
| 16 | + "dial tcp 127.0.0.1:45432: connect: connection refused", |
| 17 | + "the database system is starting up", |
| 18 | + } |
| 19 | + for _, m := range transient { |
| 20 | + if !transientConnErr(errors.New(m)) { |
| 21 | + t.Errorf("expected transient: %q", m) |
| 22 | + } |
| 23 | + } |
| 24 | + permanent := []string{ |
| 25 | + "password authentication failed for user \"devstack\"", |
| 26 | + "database \"nope\" does not exist", |
| 27 | + "", |
| 28 | + } |
| 29 | + for _, m := range permanent { |
| 30 | + if transientConnErr(errors.New(m)) { |
| 31 | + t.Errorf("expected permanent: %q", m) |
| 32 | + } |
| 33 | + } |
| 34 | + if transientConnErr(nil) { |
| 35 | + t.Error("nil is not transient") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// fakeConn is a throwaway provision.Conn for connector return values. |
| 40 | +type fakeConn struct{} |
| 41 | + |
| 42 | +func (fakeConn) Exec(context.Context, string, ...any) error { return nil } |
| 43 | +func (fakeConn) Exists(context.Context, string, ...any) (bool, error) { return false, nil } |
| 44 | + |
| 45 | +func withNoSleep(t *testing.T) { |
| 46 | + t.Helper() |
| 47 | + orig := sleepFn |
| 48 | + sleepFn = func(ctx context.Context, _ time.Duration) error { |
| 49 | + if ctx.Err() != nil { |
| 50 | + return ctx.Err() |
| 51 | + } |
| 52 | + return nil |
| 53 | + } |
| 54 | + t.Cleanup(func() { sleepFn = orig }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestRetryingPgConnect_EventuallySucceeds(t *testing.T) { |
| 58 | + withNoSleep(t) |
| 59 | + calls := 0 |
| 60 | + base := PgConnector(func(context.Context, string) (provision.Conn, func() error, error) { |
| 61 | + calls++ |
| 62 | + if calls < 3 { |
| 63 | + return nil, nil, errors.New("read: connection reset by peer") |
| 64 | + } |
| 65 | + return fakeConn{}, func() error { return nil }, nil |
| 66 | + }) |
| 67 | + conn, closeFn, err := retryingPgConnect(base)(context.Background(), "dsn") |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("want success after retries, got %v", err) |
| 70 | + } |
| 71 | + if conn == nil || closeFn == nil { |
| 72 | + t.Fatal("want a live conn + close on success") |
| 73 | + } |
| 74 | + if calls != 3 { |
| 75 | + t.Errorf("calls = %d, want 3 (two transient failures then success)", calls) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestRetryingPgConnect_FailsFastOnPermanent(t *testing.T) { |
| 80 | + withNoSleep(t) |
| 81 | + calls := 0 |
| 82 | + base := PgConnector(func(context.Context, string) (provision.Conn, func() error, error) { |
| 83 | + calls++ |
| 84 | + return nil, nil, errors.New("password authentication failed") |
| 85 | + }) |
| 86 | + _, _, err := retryingPgConnect(base)(context.Background(), "dsn") |
| 87 | + if err == nil { |
| 88 | + t.Fatal("want the permanent error surfaced") |
| 89 | + } |
| 90 | + if calls != 1 { |
| 91 | + t.Errorf("calls = %d, want 1 (no retry on a permanent error)", calls) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestRetryingPgConnect_BudgetBounded(t *testing.T) { |
| 96 | + withNoSleep(t) |
| 97 | + // Drive a synthetic clock so the 30s budget elapses without real waiting. |
| 98 | + origNow := nowFn |
| 99 | + tick := time.Unix(0, 0) |
| 100 | + nowFn = func() time.Time { tick = tick.Add(5 * time.Second); return tick } |
| 101 | + t.Cleanup(func() { nowFn = origNow }) |
| 102 | + |
| 103 | + calls := 0 |
| 104 | + base := PgConnector(func(context.Context, string) (provision.Conn, func() error, error) { |
| 105 | + calls++ |
| 106 | + return nil, nil, errors.New("connection refused") |
| 107 | + }) |
| 108 | + _, _, err := retryingPgConnect(base)(context.Background(), "dsn") |
| 109 | + if err == nil { |
| 110 | + t.Fatal("want the transient error surfaced after the budget elapses") |
| 111 | + } |
| 112 | + if calls < 2 { |
| 113 | + t.Errorf("calls = %d, want ≥2 (retried before giving up)", calls) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestRetryingPgConnect_HonorsContextCancel(t *testing.T) { |
| 118 | + withNoSleep(t) |
| 119 | + ctx, cancel := context.WithCancel(context.Background()) |
| 120 | + cancel() |
| 121 | + calls := 0 |
| 122 | + base := PgConnector(func(context.Context, string) (provision.Conn, func() error, error) { |
| 123 | + calls++ |
| 124 | + return nil, nil, errors.New("connection reset by peer") |
| 125 | + }) |
| 126 | + _, _, err := retryingPgConnect(base)(ctx, "dsn") |
| 127 | + if err == nil { |
| 128 | + t.Fatal("want an error when ctx is already cancelled") |
| 129 | + } |
| 130 | + if calls != 1 { |
| 131 | + t.Errorf("calls = %d, want 1 (a cancelled ctx stops the retry loop)", calls) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestRetryingPgConnect_NilPassthrough(t *testing.T) { |
| 136 | + if retryingPgConnect(nil) != nil { |
| 137 | + t.Error("retryingPgConnect(nil) must be nil so the default connector is used downstream") |
| 138 | + } |
| 139 | +} |
0 commit comments