|
5 | 5 | "errors" |
6 | 6 | "testing" |
7 | 7 |
|
| 8 | + "github.com/databricks/databricks-sdk-go/apierr" |
8 | 9 | "github.com/databricks/databricks-sdk-go/experimental/mocks" |
9 | 10 | "github.com/databricks/databricks-sdk-go/service/workspace" |
10 | 11 | "github.com/stretchr/testify/assert" |
@@ -47,3 +48,87 @@ func TestRetryOnTimeout_NonRetriableError(t *testing.T) { |
47 | 48 | }) |
48 | 49 | assert.ErrorIs(t, err, expected) |
49 | 50 | } |
| 51 | + |
| 52 | +func TestRetryOn504_noError(t *testing.T) { |
| 53 | + wantErr := error(nil) |
| 54 | + wantRes := (*workspace.ObjectInfo)(nil) |
| 55 | + wantCalls := 1 |
| 56 | + |
| 57 | + w := mocks.NewMockWorkspaceClient(t) |
| 58 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 59 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(wantRes, wantErr) |
| 60 | + |
| 61 | + gotCalls := 0 |
| 62 | + gotRes, gotErr := RetryOn504(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 63 | + gotCalls += 1 |
| 64 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 65 | + }) |
| 66 | + |
| 67 | + assert.ErrorIs(t, gotErr, wantErr) |
| 68 | + assert.Equal(t, gotRes, wantRes) |
| 69 | + assert.Equal(t, gotCalls, wantCalls) |
| 70 | +} |
| 71 | + |
| 72 | +func TestRetryOn504_errorNot504(t *testing.T) { |
| 73 | + wantErr := errors.New("test error") |
| 74 | + wantRes := (*workspace.ObjectInfo)(nil) |
| 75 | + wantCalls := 1 |
| 76 | + |
| 77 | + w := mocks.NewMockWorkspaceClient(t) |
| 78 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 79 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(wantRes, wantErr) |
| 80 | + |
| 81 | + gotCalls := 0 |
| 82 | + gotRes, gotErr := RetryOn504(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 83 | + gotCalls += 1 |
| 84 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 85 | + }) |
| 86 | + |
| 87 | + assert.ErrorIs(t, gotErr, wantErr) |
| 88 | + assert.Equal(t, gotRes, wantRes) |
| 89 | + assert.Equal(t, gotCalls, wantCalls) |
| 90 | +} |
| 91 | + |
| 92 | +func TestRetryOn504_error504ThenFail(t *testing.T) { |
| 93 | + wantErr := errors.New("test error") |
| 94 | + wantRes := (*workspace.ObjectInfo)(nil) |
| 95 | + wantCalls := 2 |
| 96 | + |
| 97 | + w := mocks.NewMockWorkspaceClient(t) |
| 98 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 99 | + call := api.GetStatusByPath(mock.Anything, mock.Anything).Return(nil, apierr.ErrDeadlineExceeded) |
| 100 | + call.Repeatability = 1 |
| 101 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(wantRes, wantErr) |
| 102 | + |
| 103 | + gotCalls := 0 |
| 104 | + gotRes, gotErr := RetryOn504(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 105 | + gotCalls++ |
| 106 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 107 | + }) |
| 108 | + |
| 109 | + assert.ErrorIs(t, gotErr, wantErr) |
| 110 | + assert.Equal(t, gotRes, wantRes) |
| 111 | + assert.Equal(t, gotCalls, wantCalls) |
| 112 | +} |
| 113 | + |
| 114 | +func TestRetryOn504_error504ThenSuccess(t *testing.T) { |
| 115 | + wantErr := error(nil) |
| 116 | + wantRes := &workspace.ObjectInfo{} |
| 117 | + wantCalls := 2 |
| 118 | + |
| 119 | + w := mocks.NewMockWorkspaceClient(t) |
| 120 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 121 | + call := api.GetStatusByPath(mock.Anything, mock.Anything).Return(nil, apierr.ErrDeadlineExceeded) |
| 122 | + call.Repeatability = 1 |
| 123 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(wantRes, wantErr) |
| 124 | + |
| 125 | + gotCalls := 0 |
| 126 | + gotRes, gotErr := RetryOn504(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 127 | + gotCalls++ |
| 128 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 129 | + }) |
| 130 | + |
| 131 | + assert.ErrorIs(t, gotErr, wantErr) |
| 132 | + assert.Equal(t, gotRes, wantRes) |
| 133 | + assert.Equal(t, gotCalls, wantCalls) |
| 134 | +} |
0 commit comments